Sei sulla pagina 1di 48

Perl -101

Objectives

To introduce Perl Scripting Language


To introduce the concept of regular expressions and pattern
matching
To introduce various file operations in Perl

Linux User Group at San Jose State


University

Agenda
Part 1 (1:30PM to 2:45PM)
Introduction to Perl
Types of Variables
I/O Functions
Operators
String and formatting strings
Selectional, iterational and
miscellaneous control statements
Data Structures
Arrays and various operations on arrays
Hashes and various operations on
hashes
Functions:
Subroutines
Command line arguments

Part 2 (2:50PM to 3:45PM)


Regular Expressions:
Regular Expressions
Meta characters
Character classes
Pattern matching
File IO:
Reading, Writing text files
Various file I/O modes
Misc:
Installing modules
Questions and Answers

Linux User Group at San Jose State


University

What is PERL and where PERL is being


used?

PERL is scripting language


PERL is mostly used for extracting information from text
files and generating various reports based on that
PERL is also used for developing web applications using CGI
standards
It is also used for developing scripts of automated testing,
system administration etc.
It is one of the most popular scripting language

Linux User Group at San Jose State


University

Perl Introduction

PERL Practical Extraction and Report Language


Developed by Larry Wall in 1987
Originally designed for reading text files and preparing reports
based on that information
Combines features of C ,sed, awk and sh
Gives unlimited data size
Recursion of unlimited depth
Sophisticated pattern matching techniques
Used for web programming
Widely used for system administration
Wide range of third party modules
Linux User Group at San Jose State
University

The PERL Interpreter

Perl Interpreter
Converts the scripts in to a parse tree and executes
immediately
Internally Perl maintains a byte code for execution
Known as interpreter/ compiler

Linux User Group at San Jose State


University

PERL - Variables

Scalar Variables
Simple variables containing an element, either a number or a
string
Defined by $ symbol

Array
List of scalar variables
Defined by @ symbol

Hashes
Similar to arrays
Each item is identified by a key value
Defined by % symbol

Linux User Group at San Jose State


University

PERL Scalar Variables


Defined by $ Symbol
Stores either a number or a String
No declaration required, creation is done when a variable is
referred first in the program,known as auto vivification
Strings and Numbers are interchangeable
Variable names are case sensitive
Variable name can have alphabets, numbers and underscore
(_)
Should not start with a number
Variables starting with _ is special

Linux User Group at San Jose State


University

PERL - Lists

Collection of scalar data, Arrays and Hashes


Represents a logical group of items
Scalar data inside ( and ) separated using , represents a list
A set of operators works on list called as list operator
E.g. print
print (1,2,3) displays 123

Linux User Group at San Jose State


University

PERL Display Functions

print
Unformatted output statement which can print a string, Number or a
List
Default outputs to STDOUT, can be redirected to STDERR or a File
Returns true if successful else false [1 represents true]
print List
print FILEHANDLE List
printf
Formatted output statement
By defaults outputs to STDOUT. Can be redirected to STDERR or a File
printf Format, List
printf FILEHANDLE, Format, List
warn
Outputs a message on the STDERR stream (unbuffered)
Used to trace error when STDOUT is redirected
Program is neither terminated nor an exception is thrown
warn Message
Linux User Group at San Jose State
University

10

PERL - Operators

Assignment Operator
Auto increment and decrement operators
Additive operators
Multiplicative operators
Exponentiation Operator
Relational Operators
Logical Operators
Range Operator
Binding Operator
Arrow operator
File I/O Operator

Linux User Group at San Jose State


University

11

PERL Operators contd

Assignment Operator (=)


Auto increment and decrement operator (++, --)
Additive Operator (+, -, .)
Addition & Subtraction operator
Concatenation Operator (.)
Multiplicative Operators
Multiplication & Division
Repetition Operator (x)
Exponential Operator

Linux User Group at San Jose State


University

12

PERL operators contd

Relational Operators
Operating on Numeric data
<, <=, >, >=, ==, !=,
- Operating on String data
lt
less than E.g. ($Myname lt $YourName)
gt
greater than
le
less than or equal to
ge
greater than or equal to
eq
equal to
ne
not equal to
cmp
compare (returns -1, 0 , 1)
- Logical Operator
AND - && or and
OR - || or or
NOT - not
Linux User Group at San Jose State
University

13

PERL Operators contd

Range Operator (..)


Returns a list of sequence of values
print (1..50);
print (a..z);
Bind Operator (=~, !~)
Used for binding scalar expressions to a pattern match
Arrow Operator (->)
Used to retrieve elements from a reference
File Operator (<File_Handle>)
Used for File I/O Operations
<> represents <STDIN>

Note: Bind, Arrow and File operators will be explained later

Linux User Group at San Jose State


University

14

PERL Standard Input

Input operator
<STDIN> or <>
Read input from the user including \n from the user
chomp operator can be used for removing the \n character
By default input is stored in a default variable $_
chomp
Usually used to discard the \n character at the end of input
string

Linux User Group at San Jose State


University

15

PERL - Strings

Sequence of characters, each character is an 8 bit value


Single quoted string
Escape sequence except \\ & \ are not interpreted so
Double quoted string
All escape sequences are interpreted so
Concatenation Operator
. can be used for concatenating string values
Repetition Operator
x makes as many number of concatenated copies of string

Linux User Group at San Jose State


University

16

PERL String Functions

Converting to lower case using lc STRING


Converting to upper case using uc STRING
Converting initial case to lower using lcfirst STRING
Converting initial case to upper using ucfirst STRING
Getting string length using length (STRING)
Returns the length of the string
If STRING is omitted, it returns the length of the string stored in
$_

Linux User Group at San Jose State


University

17

PERL String Functions


contd

Searching string using index (STRING, SUBSTRING,


[POSITION])
returns the index of the first occurrence of the SUBSTRING in
STRING on or after POSITION
If POSITION is ignored, it starts searching from the first location
returns -1 if not found
rindex can be used to find the index of the last occurrence of
the SUBSTRING in STRING on or before POSITION

Linux User Group at San Jose State


University

18

PERL String Functions


contd

Extracting/Replacing substring using


substr STRING,OFFSET,[LENGTH],
REPLACEMENT
Extracts and returns the substring from OFFSET to
LENGTH+OFFSET
If LENGTH is omitted, extraction starts from OFFSET to end of
STRING
If LENGTH is negative, extraction omits that much number of
characters from the end
If OFFSET is negative extraction starts from end of the string
and moves back wards
If REPLACEMENT is specified, extracted substring will be
replaced by REPLACEMENT

Linux User Group at San Jose State


University

19

PERL Control Structures

Branching Statements
if
if else
if elsif
Looping Statements
while / until
do while/ until
for
foreach
Miscellaneous Control Statements
last, next, redo
Expression modifiers
exit, die

Linux User Group at San Jose State


University

20

PERL Control Structures


contd
if statement
if ( condition ) {
#True Block
}
If - else statement
if( condition ) {
#True Block
}else {
#False Block
}
if elsif statement
If( condition1) {
#True block for Condition1
}elsif(condition2) {
#True block for Condition2
}else {
#False block
}
Linux User Group at San Jose State
University

21

PERL Control Structures


contd

while loop
while (condition) {
#while block statements
}
Control comes out of the loop when the condition is FALSE

until loop
until (condition) {
#until block statements
}
Control comes out of the loop when the condition is TRUE

Linux User Group at San Jose State


University

22

PERL Control Structures


contd

do while loop
do{
#while block statements
}while (condition)
Control comes out of the loop when the condition is FALSE

do until loop
do {
#until block statements
}until (condition)
Control comes out of the loop when the condition is TRUE

Linux User Group at San Jose State


University

23

PERL Control Structures


contd

for
for(init expr ; condition ; increment/decrement ) {
#for block
}
foreach
mostly used when iteration has to be done for different values
in a list
foreach Variable (LIST) {
#Block of Statements
}

Linux User Group at San Jose State


University

24

PERL Control Structures


contd

continue
flow control statement follows while or foreach loop
continue block will be executed after every successful
completion of an iteration
next
used to continue with the next iteration of the loop by stopping
current iteration
cannot be used in side do-while/do-until loop
if continue block is present it will also be executed
redo
restarts the loop without evaluating the condition again
continue block is not executed, if present
last
exits the loop
continue block is not executed, if present

Linux User Group at San Jose State


University

25

PERL Control Structures


contd

exit
used to terminate the execution of a script
exit 0 denotes exit on success and exit 1 denotes exit on error
cannot display error messages while exiting
$Choice=<STDIN>
if ($Choice==4) { exit 0};
die
displays the error message to STDERR stream
and terminates the execution of a script
copy($TargetFile,$SourceFile) or die File cannot be copied

Linux User Group at San Jose State


University

26

PERL - Arrays
Set of scalar values
Dynamically grows/shrinks when ever required
Can store dissimilar data types
Prefixed with @ symbol
Un-initialized locations and locations beyond the end of array
will be undef
Can be initialized using
List
qw operator
Repetition operator (X)
Range operator (..)
Individual locations

Linux User Group at San Jose State


University

27

PERL Arrays contd

Creation using Range operator


@EmpNumber = (1001..1200) ;
- Can be used if array has to be initialized by a range of
elements

Creation using Repetition operator


@Number = (0) X 10;
- Can be used if array has to be initialized by same value in all
locations

Creation of array with dissimilar elements


@Details=(1001,John,3400.50);

Linux User Group at San Jose State


University

28

PERL Arrays contd

Length of the array


$#<array name> stores the last index of the array
$#<array name> +1 will give the number of elements
Emptying an array
$#Emp = -1;
OR
@Emp = ( );
OR
@Emp = undef;
Looping through an Array using foreach statement
converting scalar into an array using split function
Sorting an Array using sort @<array_name>
Reversing an Array using reverse @<array_name>
Linux User Group at San Jose State
University

29

PERL Arrays contd

Pushing and Popping elements


push (<Array>, <Value>/<List>)
Will add the element to the end of array
Array expands automatically
pop (<Array>)
Will remove the element from the end of the array
Array shrinks automatically
Shifting and Unshifting
unshift (<Array>, <Value>/<List>)
Will add the element/list to the front of the array & returns
number of elements
Array expands automatically
shift (<Array>)
Will remove the element from front of the array
Array shrinks automatically
Linux User Group at San Jose State
University

30

PERL Hashes

Hashes
Unordered collection of values
Each element is linked to a key, which uniquely identifies that
element
Any element from the hash can be retrieved, added, deleted using
the key
A hash variable is prefixed with %
Also called as associative arrays
Accessing elements
Elements can be retrieved using key
$RetrievedElement = $Hash{Key};
Adding new elements
$Hash{ new key}= new element
Changing the existing elements
$Hash{key} = new element
Linux User Group at San Jose State
University

31

PERL Hashes contd

Deleting key / value pairs


delete ($Hash{Key})

Getting all keys or all values


keys (%hash)
values (%hash)

Does a key exist??


Using exists keyword

Looping through hash elements


using foreach and keys

Linux User Group at San Jose State


University

32

Perl more data structures

Combination of arrays and hashes


Arrays of Arrays
Hashes of Arrays
Arrays of Hashes
Hashes of Hashes
and so on

http://perldoc.perl.org/perldsc.html

Linux User Group at San Jose State


University

33

PERL Regular Expressions


A regular expression, often called a pattern in Perl, is a
template that either matches or doesn't match a given
string.
Using simple patterns:
To compare a pattern (regular expression) to the contents of
$_, simply put the pattern between a pair of forward slashes
(/)
$_ = perl script";
if (/perl/) {
print "It matched!\n";
}

Note: $_ is the default variable


Linux User Group at San Jose State
University

34

PERL Regular Expression


contd

Metacharacters
Dot (.) is a wildcard character - it matches any single character
except a newline (which is represented by "\n")
/b.t/ will match bat, bet, bit, but etc. It will not match bt or boat
etc.
A backslash in front of any metacharacter makes it non-special.
/3\.14159/ doesn't have a wildcard character.
Star (*) is a wildcard character it matches the preceding character
zero or more times
/perl\t*script/ matches any number of tab characters between perl
and script. i.e. it matches perl\tscript" with one tab, or
"perl\t\tscript" with two tabs, or perl\t\t\tscript" with three tabs,
or even perlscript" with nothing in between at all.
Linux User Group at San Jose State
University

35

PERL Regular Expression


contd

Metacharacters
Plus (+) is to match the preceding character one or more times
/perl +script/ matches if perl and script are separated only by
spaces. This pattern won't match perlscript, since the plus
matches only if there are one or more spaces between the two
words.
Question mark ("?") means that the preceding character is optional
/perl ? script/ matches it perl and script are separated by one space
or no space. This will match perl script as well as perlscript.
Parentheses ("( )") may be used for grouping
/(perl)+/ matches strings like perlperlperl
The vertical bar (|), often pronounced "or" means that either the left
side may match, or the right side
/unix|perl/ will match any string that mentions unix or perl
Linux User Group at San Jose State
University

36

PERL Regular Expression


contd

Character class
a list of possible characters inside square brackets ([]),
matches any single character from within the class.
[abcwxyz] may match any one of the seven characters.
[a-zA-Z] may match any of the alphabets (both uppercase
and lowercase).
A caret (^) at the start of the character class negates it.
[^def] will match any single character except d, e or
f.
[\d] or [0-9] will match any digit
[\w] is a shortcut for any "word" character:
[\w] or [A-Za-z0-9_] will match any word
\s is a shortcut for white space character

Linux User Group at San Jose State


University

37

PERL Regular Expression


Contd

Matches with m//


m// (pattern match) operator allows you to choose any pair of
delimiters to quote the contents
m(perl), m<perl>, m{perl}, or m[perl]
Case Insensitive Matching with /i
To make a case-insensitive pattern match, so that you can match
PERL as easily as perl or Perl, use the /i modifier:
Binding Operator =~
Matching against $_ is merely the default; the binding operator
(=~) tells Perl to match the pattern on the right against the string
on the left, instead of matching against $_
Substitution with s///
s/// is the search and replace feature. This simply replaces
whatever part of a variable matches a pattern with a replacement
string.
If the match fails, nothing happens, and the variable is untouched
Linux User Group at San Jose State
University

38

PERL Regular Expression


Contd

Global Replacements with /g


s/// will make just one replacement, even if others are possible.
The /g modifier tells s/// to make all possible replacements
A fairly common use of a global replacement is to collapse
whitespace; that is, to turn any arbitrary whitespace into a
single space

Linux User Group at San Jose State


University

39

PERL - Subroutines

Section of the script that performs a specific task


Values can be passed to subroutine so that it performs the task on
these values
Values passed to the subroutines are called arguments
Return value is send back by the subroutine
@_ is an array where the argument values are stored during
subroutine call
Subroutine definitions can be placed anywhere in your program
text

Linux User Group at San Jose State


University

40

PERL Subroutine contd

Subroutine Declaration
sub MySubroutine()
MySubroutine accepts
sub MySubroutine($)
MySubroutine accepts
sub MySubroutine($$)
MySubroutine accepts
sub MySubroutine(@)
MySubroutine accepts
sub MySubroutine(%)
MySubroutine accepts
sub MySubroutine($@)
MySubroutine accepts
argument
sub MySubroutine($;$)
MySubroutine accepts
argument is optional

no arguments
one scalar argument
2 scalar arguments
an array as an argument
a hash reference as an argument
a scalar value and an array as
2 arguments, where the second

Linux User Group at San Jose State


University

41

PERL Subroutine contd

Argument passing & Returning Values


Arguments passed from the calling function are stored in an
array @_
Default arguments
Default arguments are specified in the prototype, separated by ;
from mandatory parameters
Scope of a variable
Default scope of all variables are global
Scope can be changed by using my or local keyword
Changing the scope of a variable using my
Restricts the scope of a variable to the block in which it is defined
It is not visible to the subroutine called from the enclosing block
Changing the scope of a variable using local
Restricts the scope of a variable to the block in which it is defined
It is visible to the subroutine called from the enclosing block
Linux User Group at San Jose State
University

42

PERL Subroutine contd

Reference Variable
Reference variable store the reference (address) of the
variables
Variables can be directly accessed using reference variables
Reference to a scalar
Command line arguments
Command line arguments are stored in a built in array @ARGV
when a perl script is executed

Linux User Group at San Jose State


University

43

PERL File I/O

Data that outlives the program


needs to be stored
in a permanent storage
so that it can be referred later on

Example
word processing application might save the text in a linked list
or some other data structure when the application is running,
but when the application is terminated the contents of the
linked list need to stored in a file

Linux User Group at San Jose State


University

44

PERL File I/O contd

Opening a File
open FILEHANDLE, FileName
Returns true (1) if successfully opened, false (0) otherwise
open (MyFileHandle,MyData.txt)
File will be opened for input if file name is prefixed by <
open (MyFileHandle,<MyData.txt)
File will be opened for output if the file name is prefixed by >
If the file exists contents will be erased
If the file doesnt exists new file will be created
open (MyFileHandle,>MyData.txt)
File will be opened for appending if the the file name is prefixed by
>>
If the file doesnt exist new file will be created
open (MyFileHandle,>>MyData.txt)
Linux User Group at San Jose State
University

45

PERL File I/O contd

Opening a File contd


File will be opened for read & write if the file name is prefixed by +> or
+<
+> will erase the existing contents and open the file for read and
write
+< must be used if the file has to be opened for updating the
existing contents
open (MyFileHandle, +<MyData.txt)

Handling Errors in File Open


An alternate statement can be attached to the open statement using or
The alternate statement will be executed if opening a file fails
open FileHandle,FileName or {Alternate Statement}

Closing a File
Closing a file will transfer the un buffered data to the file
Returns true(1) if successful, false(0) otherwise
close FileHandle
Linux User Group at San Jose State
University

46

PERL File I/O contd

Reading from a File using <> (angle) operator


<FileHandle> returns the next line of input from the file
If <> alone is specified, input is taken from STDIN
Returns null when end of file is reached
Reading from file using readline
readline (FileHandle) returns one line of data from the file
Undefined value will be returned if end of file is reached
Read from file using getc
getc(FileHandle) returns a character from the file
Undefined value will be returned if end of file is reached
Writing unformatted records into a file using print
print FileHandle Message
Returns true if successful, false otherwise
print FIleHandle Message or Alternate statement
Returns false and executes alternate statement if unsuccessful,
true otherwise
Linux User Group at San Jose State
University

47

Questions

Linux User Group at San Jose State


University

48

Potrebbero piacerti anche