Introduction Scalar Variables and Lists Arrays and Hashes Operators and Precedence Conditional statements and Loops Regular Expressions Subroutines File and Directory Handling Contents History DevelopedbyLarryWall in 1987 Derives from the ubiquitous C programming language andtoa lesser extent fromsed, awk, theUnixShell. PERL was originally designed under Unix, but now also runsunder all OS(I ncludingWindows). Introduction What is PERL? I nterpreted language that is optimized for string manipulation, I /O and system tasks. Why PERL? Speed of development Don't have to compile create object file and then execute. Power of flexibility of a high programming language. Easy to use, freely available and portable. Makes easy jobs easy, without making hard jobs impossible. Beginning with Perl perldoc perl gives the list of manual pages as a part of everyPerl installation. perldoc h givesthebrief summaryof options available. perl v gives theversionof thePerl theuser is using. To create a Perl program, only a text editor and the perl interpreter arerequired. Perl fileendswith.pl (simple.pl) Beginning with Perl (Contd..) ExecutionCommand: perl filename.pl, or ./filename.pl When Unix has to execute Perl Script It first looks for #!(Shebang) , it executes the remainder of the line and passesthenameof thescript toit as an argument. #! /usr/bin/perl or #!/usr/local/bin/perl is the command usedtorunthePerl I nterpreter. So to start a script we need to add abovelineas the first line tomakePerl script executable. Beginning with Perl (Contd..) The core of Perl is Perl I nterpreter the engine that actually interprets, compiles, andrunsPerl scripts. All Perl programs gothroughtwophases: a compile phase where the syntax is checked and the source code, including any modules used, is converted intobytecode. a run-time phase where the bytecode is processed into machineinstructionsandexecuted. Man Pages Man command used to read the documentation. Command Description perl Overview(top level) perldelta Changes since last version perlfaq Frequently asked questions perltoc Table of contents for Perl documentation perlsyn Perl Syntax perlop Operators and precedence perlre Perl Regular Expression perlfunc Built in functions perlsub Subroutines perlvar Predefined Variables Basic Syntax #is usedfor commentingtheline. All statementsshouldendwith;. $_isthespecial variablecalleddefault variable. Perl is casesensitive. Perl programiscompiledandrunin a singleoperation. Example #! /usr/local/bin/perl #Directs to perl interpreter on the system. print Welcome to perl; #Prints a message on the output. This Program displays: Welcome to perl Example: #! /usr/local/bin/perl c Print welcome to perl; This Program displays: Syntax ok Simple Programs Basic Options -c : Check syntaxandexit without executingthescript. -v : Printstheversionof perl executable. -w : Printswarnings -e : Used to enter and execute a line of script on the commandline Standard Files STDIN :I t is a normal input channel for thescript. STDOUT :I t is an normal output channel. STDERR :I t is thenormal output channel for errors. Example #! /usr/local/bin/perl w print Enter the Text; $input = <STDI N> ; #Reads the input and stores in the variable input Chomp(); #will remove new line character. Print entered text =$input ; #Prints the input on the command line\ This Program displays: Enter the Text Perl is awesome #Perl will read this Perl is awesome\ n, by default it will add \ n character to your entered text. So use chomp entered text =Perl is awesome Standard Files (Contd..) Variables Variables areusedtorefer datawhichis heldas value. Perl defines three basic data types: scalars, arrays, and hashes. Scalars: Holdsasinglevalueit may bea string,number or reference. Begin with $, followed by a letter then by letters, digits or underscores. Example : $var =1 #integer $var = Hello_world #string $var=2.65 #Decimal number $3var = 123 #Error, Shouldnt start with #number I nterpolation takes place only in double quotation marks. Example #! /usr/local/bin/perl w $x = 12 ; #Assign the value to the variable print Value of x is $x ; #Prints the output This Program displays: Value of x is $x #Single quotation will not interpolate #(no processing is done) the values Example #! /usr/local/bin/perl w $x = 12 ; #Assign the value to the variable print Value of x is $x ; #Prints the output This Program displays: Value of x is 12 #Double quotation interpolates the values. #(Variable is replaced by its content ) Variable Interpolation Integers I ntegers are usually expressed as decimal(10) but can be specifiedinseveral different formats. 234 decimal integer 0765 octal integer 0b1101 binaryinteger 0xcae hexadecimal integer Converting a number from one base to another base can be doneusing sprintffunction. Variables of different base can be displayed using printf function Example #! /usr/local/bin/perl w $bin = 0b1010; $hex = sprint f %x, $bin; $oct = sprint f %o ,45; print binary =$bin \ n hexa =$hex \ n octal =$oct; This Program displays: binary= 1010 hexa = a octal = 55 Integers (Contd..) Example #! /usr/local/bin/perl w $x = 98 ; print f ( Value in decimal =%d\ n, $x ) ; print f ( Value in octal=%o\ n, $x ) ; print f ( Value in binary =%b\ n, $x ) ; print f ( Value in hexadecimal=%x\ n, $x ) ; This Program displays: Value in decimal =98 Value in octal =142 Value in binary =1100010 Value in hexadecimal =62 Integers (Contd..) Escape Sequence Description \b Backspace \e escape \f Form feed \l Forces the next letter into lowercase \L All following letters are lower case \u Forces the next letter into upper case \U All following letters are upper case \r Carriage Return \v Vertical Tab Escaped Sequences Character strings that are enclosed in double quotes accept escapesequencesfor special characters. The escape sequences consist of a backslash (\ ) followed by oneor morecharacters Built-in functions Function Description chomp( ) The chomp() function will remove (usually) any newline character from the end of a string. The reason we say usually is that it actually removes any character that matches the current value of $/ (the input record separator), and $/ defaults to a newline. Ex :chomp($text); Chop( ) The chop() function will remove the last character of a string (or group of strings) regardless of what that character is. Ex:chop($text) Chr () Returns the character represented by that number in the character set Ex: chr(65 ) gives A. Ord() Returns the ASCII numeric value of the character specified by expression. Ex:ord(A) gives 65. List isa groupof scalar usedtoinitializearrayor hash. The elements of a list can be numbers, strings or any other typesof scalar data. Each element of thePerl lists can be accessed by a numerical index. The elements of a list are enclosed in a pair of round parenthesis andaregenerallyseparatedbycommas. Lists Example: $var = welcome #normal variable $var2=(12,24,kacper, $var,36.48) #first list #first list contains 5elements and two of are strings(kacper, welcome) $var3 = (12,24,Kacper ,$var ,36.48) #secondlist #secondlist contains 5 elements and two of are strings(kacper, $var) Lists (Contd..) Flexible way of defining list is qw (quote word) operator which helps avoiding to too many quotation marks, but be cautious if whitespacesarethere. Example #! /usr/local/bin/perl w print (sachin ,dravid, ganguly, kumble , \ n); print qw(sachin dravid ganguly kumble); print \ n; print (sachin, dravid , ganguly, anil kumble ,\ n); print( k, a, qw(c p e r ),t , e, c , h ); print \ n; print (sachin ,dravid, ganguly, kumble )[1]; This Program displays: Sachindravidgangulykumble Sachindravidgangulykumble Sachindravidgangulyanil kumble kacpertech dravid Lists (Contd..) Lists (Contd..) Difference between list and array(or hash) is, array is a variablethat can beinitializedwithalist. Range Operator Defined by symbol .. Used to create a list from a range of letters or numbers. Example : Print (2 .. 4 ** a .. d ) This program displays: 234**abcd List functions A list is joined into a string using join function. Example: Print join( ,(perl ,is ,a, scripting ,language)); This program displays: Perl is a scripting language A string is splited into a list using split function. Example: Print (split | , perl) ; This program displays: P| e| r| l Lists (Contd..) List functions (Cont..) map evaluates expression or block for each element of list. Example: Print join(, ,(map lc, A, B, C)); This program displays a, b, c grep returns a sublist of a list for which a specific criterion is true. Example: Print grep(!/x/ , a, b, x, d); This program displays: abd Lists (Contd..) Arrays Anonedimensional orderedlist of scalar variables. Array provides dynamic storage for a list ,and so can be shrunk ,grown,andmanipulatedbyalteringvalues. Representedusing@(at)symbol. Arraywithout anameis calleda list. Elements of an array are accessed using the index number (first element has indexzero, next has one, andsoon) Eachelement inan array is a scalar. $#arrayholdsthelast indexvalueinthearray. Arrays (Contd..) ( ) represents theemptylist. Example: @arr= (perl ,2 , 5.143 ); print @arr; This program displays: perl 2 5.143 #displays all the elements Example: @num = (1,2,3,4,5) ; print \ @num has ($#num + 1) elements; This program displays @num has 5 elements #Displays (last index number i.e, 4 + 1) which is #the length of array Example #! /usr/local/bin/perl w @a =(a .. z) ; @len1 =@a; #Assign the array to the variable @len2 =scalar (@arr); #using scalar method Print length of a =$len1 \ n ; Print length of a =$len2; This Program displays: length of a = 26 length of a = 26 Example #! /usr/local/bin/perl w @arr = ( one ,2 ,three ,4.4); $arr[2] = kacper; #second element(three) is replaced by print @arr; new element(kacper) This Program displays: one 2 kacper 4.4 Arrays (Contd..) Array Methods push Pushfunction addsavalueor valuestotheendof an array. Example: @num = (1,2,3,4,5) ; Push (@num , 6) ; #pushes 6 at the end of array Print @num ; This program displays: 123456 #Displays all the elements of array. pop Pop function gets a value or values from an array. Example: @num = (1,2,3,4,5) ; Pop (@num) ; #Removes the last element of an array Print @num ; This program displays: 1234 #Displays all the elements of array. Array Methods (Contd..) unshift unshift function adds a value or values at the start of an array. Example: @num = (1,2,3,4,5) ; unshift (@num , 6) ; #Adds 6 at the beginning of array Print @num ; This program displays: 612345 #Displays all the elements of array. Array Methods (Contd..) shift shift function shifts off the first value of the array. Example: @num = (1,2,3,4,5) ; $x = shift(@num) ; #Shifts the first element of an array Print $x ; This program displays: 2,3,4,5 #Displays the value stored in x. Array Methods (Contd..) map Array processingmethodconverts onearraytoanother. Syntax: mapExpression(or Block) , list Runsanexpression on eachelement of an array(likeloop) Locallyassigns$_ as an alias tothecurrent arrayitem. Example: @small = qw( one ,two, three) ; @caps = map (uc ,@small); #uc returns an upper case version print ( @val) ; This program displays: ONE TWO THREE #Displays in upper case Array Methods (Contd..) map (Contd..) Example: @num = (65, 66,67 ,68) ; @num2 = map(2*$_ , @num); #multiplies each element by 2 @char = map(chr $_ ,@num); #chr returns the character represented by that number print @num2 \ n @char\ n; This program displays: 130 132 134 136 A B C D Array Methods (Contd..) ArraySlice Array slice is a section of an array. Example: @num = (1 ,2 ,3 ,4 ,5) ; @val = @num[0 ,1]; #Array slice of first two element of @num print join (, , @val) ; This program displays: 1 ,2 #Displays first two elements of @num. Array Methods (Contd..) ArraySplice Array splicing means adding elements from a list to the array. Example: @num = (1 ,2 ,3 ,4 ,5) ; @val =(6 , 7); splice(@num,4 ,0 , @val) ; #Adds the element of @val to @num Print join( ,,@num); This program displays: 1 ,2 ,3 ,4 , 5 , 6 , 7 #Displays all the elements of @num after #splicing. Array Methods (Contd..) sort Sorts the elements in the ASCI I order. Defines the global variables $a and $b by default ,using these we can specify our own sort. Example: @str = qw(sachin dravid ganguly kumble) ; @val =( 56,13,45,11); @str_sort 1 =sort ( @str); @val_sort1 =sort (@val); print @str_sort 1 \ n; Print @val_sort1 ; This program displays: dravid ganguly kumble sachin 11 13 45 56 Array Methods (Contd..) sort (Contd..) Example: #!usr /local/bin/perl @str = qw(sachin dravid ganguly kumble) ; @val =( 56,13,45,11); @str_sort2 = sort($a cmp $b); #sorted in alphabetical order @str_rev = sort($b cmp $a); #sorted in reverse order @val_sort2 = sort($a <=> $b); #sorted in ascending order @val_rev = sort($b , < = > $a); #sorted in descending order print @str_sort2 \ t @str_rev\ n; print @val_sort2 \ t val_rev \ n; This program displays: dravid ganguly kumble sachin sachin kumble ganguly dravid 11 13 45 56 56 45 13 11 Array Methods (Contd..) join Perl join function is used to concatenate the elements of an array or a list into a string, using a separator given by a scalar variable value. Syntax : $string = join (EXPR, LI ST); Example: #!usr/local/bin/perl @arr = ("mukesh ,"anil ,"prem ,"ratan"); $arr = join " \ t", @arr; print "business Tycoons: $arr\ n"; print join "-CEO\ t", @arr, "\ n; This program displays: business Tycoons: mukesh anil prem ratan mukesh-CEO anil-CEO prem-CEO ratan-CEO Array Methods (Contd..) ArrayReversal Reverse function is used to reverse the elements of an array. Example: @num = (1 ,2 ,3 ,4 ,5) ; @rev =reverse @num ; #Reversing the elements of @num Print join( ,, @rev) ; This program displays: 5 ,4 ,3 ,2 ,1 #Displays all the elements of @rev. spilt Split() function is the opposite of join function. Syntax :LI ST = split(/PATTERN/, EXPR, LI MI T) Array Methods (Contd..) LI ST represents a list, array or hash that is returned by thesplit function PATTERN usually is a regular expression but could be a singlecharacter or a string EXPR is the string expression that will be split into an arrayor a list. LI MI T is the maximumnumber of fields the EXPR will be split into Example: !usr/local/bin/perl $string=gandhi-ind-nehru-ind-sastri-ind-kalam-ind; @colors =split('ind', $string); print @colors,"\ n"; This programdisplays: gandhi--nehru--sastri--kalam- Hashes Anassociativearrayideal for handlingattribute/valuepair. Lists and arrays areordered and accessed by index ,hashes areorderedandaccessed by specifiedkey. Representedusing%symbol. First element in each row is called a Key and the second element isaValueassociatedwiththat key. Example: %coins = (quarter,25, dime,5); or %coins = ( quarter => 25 , dime => 5); Key Value Hashes (Contd..) Hah values can be any scalar ,just like an array ,but hash keyscan onlybestrings. Example: Printing the hash. #!usr/local/bin/perl %hash1 = ( one => 1 ,two => 2 ,three =>3 ,four =>4); print %hash1; #we cant use print %hash1; print @{[hash1]}\ n; @temp = %hash1; Print @temp; This program displays: three3one1two2four4 three 3 one 1 two 2 four 4 three 3 one 1 two 2 four 4 The print order determined by howthe Perl chooses to store internally. Hashes (Contd..) Hashcan haveonlyscalarsas values. {}areusedtoaccessindividual elementsof thehash. Example: #!usr/local/bin/perl %hash1 = ( one => 1 ,two => 2 ,three =>3 ,four =>4); $ele = $hash1(three); #single key, use scalar @mul_ele = @hash1(four ,one); #multiple key ,use array print single element =$ele ; print multiple elements =@mul_ele; This program displays: single element =3 multiple elements = 4 1 Hashes (Contd..) keys function can beused to find the no. of keys and list of entries inahash. values function can beused to find the no. of values list of values inahash. Example: #!usr/local/bin/perl %hash1 = ( one => 1 ,two => 2 ,three =>3 ,four =>4); $ele = $hash1(three); #single key, use scalar @mul_ele = @hash1(four ,one); #multiple key ,use array print single element =$ele ; print multiple elements =@mul_ele; This program displays: single element =3 multiple elements = 4 1 Manipulating Hashes Toaddor changethevalue keywecan dolikethis $hash1{three} =PERL . It will overwrite the previous value if already existing. Otherwise it is added as a newkey. undef function is used to remove the value of the key, but key will still exists. Example: undef $hash1{two}; delete function is used to remove the value as well as key fromthe hash. Example :delete$hash1{four }; Hash Sorting Hashes are not ordered and we must not rely on the order in which we added the hash items Perl uses internally its own waytostoretheitems. We can sort hashes either by key or value ,using sort function. Example: Sort by key %data = ( sachin => 10, dravid => 19, dhoni => 7, rohit => 45 ); foreach $key(sort (keys(%data))) { print \ t$key \ t $data{$key};} This program displays: dhoni 7 dravid 19 rohit 45 sachin 10 Hash Sorting (contd..) Sort function returns least (or greatest) element among all elementsinthefirst iteration. Example: Sort by value %data = ( sachin => 10, dravid => 19, dhoni => 7, rohit => 45 ); foreach $key (sort{$data {$a}<= >$data{$b}}keys %data) { print \ t $key \ t\ t $data{$key}\ n; } This program displays: dhoni 7 sachin 10 dravid 19 rohit 45 I n the above example first values are compared(using sort{$data {$a}<=>$data{$b}}), found least value and that isassignedtokey(usingkeys%data) in everyiteration Operators Operators can bebroadlydividedinto4types. Unaryoperator which takes one operand. Example: not operator i.e. ! Binaryoperator which take two operands Example: addition operator i.e. + Ternaryoperator which take three operands. Example: conditional operator i.e. ?: List operator which take list operands Example: print operator Arithmetic Operators Operator Description + Adds two numbers - Subtracts two numbers * Multiplies two numbers / Divides two numbers ++ Increments by one.(same like C) -- Decrements by one % Gives the remainder (10%2 gives five) ** Gives the power of the number. Print 2**5 ; #prints 32. Shift Operators shift operators manipulateinteger values as binary numbers, shifting their bits one to the left and one to the right respectively. Operator Description << Left Shift Print 2 >>3 ; left shift by three positions, prints 8 >> Right Shift Print 42 >>2; #right shift by two positions, prints 10 x Repetition Operator. Ex: print hi x 3; Output : hihihi Ex2: @array = (1, 2, 3) x 3; #array contains(1,2,3,1,2,3,1,2,3) Ex3 :@arr =(2)x80 #80 element array of value 2 Logical Operators Logical operators represented by either symbols or names. These two sets are identical in operation, but have different precedence. The ! operator has a much higher precedence than even &&and | | . The not, and, or, and xor operators have the lowest precedence of all Perl's operators, with not being the highest of thefour Operator Description && or AND Return True if operands are both True || or OR Return True if either operand is True XOR Return True if only one operand is True ! or NOT (Unary) Return True of operand is False Bitwise Operators Bitwise operators treat their operands as binary values and performa logical operation between the corresponding bits of eachvalue. Operator Description & Bitwise AND | Bitwise OR ^ Bitwise XOR ~ Bitwise NOT Comparison Operators The comparison operators are binary, returning a value basedona comparisonof theexpression Operator Description < Lessthan > Greaterthan == Equality <= Lessthan or equal >= Greaterthan or equal <= > It does not return a Boolean value. It returns -1 if left is less than right 0 if left is equal to right 1 if left is greater than right != Inequality operator Comparison Operators on strings String Description eq Return True if operands are equal le Return True if left operand is less than right ge Return True if left operand is greater or equal to right gt Return True if left operand is less than or equal to right gt Return True if left operand is greater than right cmp It does not return a Boolean value. It returns -1 if left is less than right 0 if left is equal to right 1 if left is greater than right ne Return True if operands are not equal .(dot) Concatenation operator. It takes two strings and joins them Ex: print System .Verilog It prints SystemVerilog. Binding operator The binding operator ,=~,binds a scalar expression into a patternmatch. Stringoperations likes/// ,m//,tr///work with$_ by default. By using these operators you can work on scalar variable other than $_ . The valuereturned from=~is the return valueof the regular expression function, returns undef if matchfailed. The !~operator performs a logical negation of the returned valuefor conditional expressions, that is1for failureand '' for successinbothscalar andlist contexts. Conditional Statements if Statement if keyword to execute a statement block based on the evaluation of an expression or to choose between executing one of two statement blocks based on the evaluation of an expression Example :- $firstVar = 2; if ($var == 1) { print we are in first if \ n; } elsif( $var ==2) {print we are in second if \ n; } else { print we are in third if\ n; } This program displays: we are in second if Conditional Statements (Contd..) until Loops Until loops are used to repeat a block of statements while some conditionis false. Example :- do until loop $firstVar = 10; do { print("inside: firstVar = $firstVar\ n"); $firstVar++; }until ($firstVar < 2); print("outside: firstVar = $firstVar\ n"); Conditional Statements (Contd..) do-until Loops Example :- until loop $firstVar = 10; until ($firstVar < 20) { print("inside: firstVar = $firstVar\ n"); $firstVar++; }; print("outside: firstVar = $firstVar\ n"); This programdisplays: outside: firstVar = 10 Conditional Statements (Contd..) for Loops Example : - for loops for ($firstVar = 0; $firstVar < 100; $firstVar++) { print("inside: firstVar = $firstVar\ n"); } This programwill display: inside: firstVar =0 inside: firstVar =1 ... inside: firstVar =98 inside: firstVar =99 Conditional Statements (Contd..) foreachLoops The foreach statement is used solely to iterate over the elements of an array. It is very handy for finding the largest element, printing the elements, or simply seeing if a given value is a member of an array. Example :- foreach loop @array = (1..5, 5..10); print("@array\ n"); foreach (@array) { $_ = ** " if ($_ == 5); } print("@array\ n"); This program displays: 1 2 3 4 5 5 6 7 8 9 10 1 2 3 4 ** ** 6 7 8 9 10 J ump Keywords The last Keyword The last keyword is used to exit froma statement block. Example :- last @array = ("A".."Z"); for ($index = 0; $index < @array; $index++) { if ($array[$index] eq "T") { { last } } print("$index\ n"); This program displays: 19 J ump Keywords (Cont..) The next Keyword The next keyword use to skip the rest of the statement block and start the next iteration. Example : - next keyword @array = (0..9); print("@array\ n"); for ($index = 0; $index < @array; $index++) { if ($index == 3 | | $index == 5) { next; } $array[$index] = "*"; } print("@array\ n"); This program displays: 0 1 2 3 4 5 6 7 8 9 * * * 3 * 5 * * * * J ump Keywords (Cont..) The redo Keyword The redo keyword causes Perl to restart the current statement block. Example :- redo print("What is your name? "); $name = <STDI N>; chop($name); if (! length($name)) { print("Msg: Zero length input. Please try again\ n"); redo; } print("Thank you, " . uc($name) . "\ n"); } Regular Expressions Regular expression(regexps) is simply a stringthat describes the pattern (example for pattern ,to find files in a directory whichendswith.svi.e. ls*.sv) Usedfor findingand extracting patterns withinthetext. The role of regexp engine is to take a search pattern and applyit tothesuppliedtext. Thefollowingoperatorsuseregular expressions. MatchingOperator (m//) SubstitutionOperator(s///) Transliteration(Translation) Operator(tr///) The Matching Operator (m//) The matching operator (m//) is used to find patterns in strings. Example : #!usr/local/bin/perl $_ = success is a progressive journey; $var = success is not a destination; I f( /success/) { #the initial m is optional print String success Found; } I f ( $var =~ /destination/) { print String destination Found; } This program displays: String success Found String destination Found When regular expression is enclosed in slashes(/success/), $_ is tested against theregular expression ,returning TRUE if thereisa match, falseotherwise Finding multiple matches Example : #!usr/local/bin/perl $txt =winn-ers see ga-in, lose-rs see pa-in while ($txt =~ m/-/g) { print Found another -\ n ;} This program displays: found another - found another - found another - The Matching Operator (Contd..) The Substitution Operator (s///) Thesubstitutionoperator (s///) isusedtochangestrings. Syntax:LVALUE =~s/PATTERN/REPLACEMENT/ The return value of an s/// operation (in scalar and list contexts alike) is thenumber of times it succeeded (which can bemorethan onceif usedwiththe/gmodifier). On failure, since it substituted zero times, it returns false (""), whichisnumericallyequivalent to0. I f PATTERN is a null string, the last successfully executed regular expressionisusedinstead Example : #!usr/local/bin/perl $text = winners see gain, losers see pain; $test =~s/winners/WI NNERS/; print $text; This program displays: WI NNERS see gain, losers see pain Example : #!usr/local/bin/perl @arr = qw(sachin dravid ganguly sachin); foreach(@arr){ #for(@arr) and s/sachin/10/g for @arr s/sachin/10/; } do same thing print "\ n@arr; This program displays: 10 dravid ganguly 10 The Substitution Operator (Contd..) Example : #!usr/local/bin/perl @old = qw(sachin-bharat dravid-bharat ganguly-bharat kumble-ind); for (@new = @old) {s/bharat/india/ } print "@olds\ n; print "@new\ n"; This program displays: sachin-bharat dravid-bharat ganguly-bharat kumble-ind sachin-india dravid-india ganguly-india kumble-ind The Substitution Operator (Contd..) Using Modifiers with m// and s/// Modifier Description g(m//g or s///g) Works globally to perform all possible operations. i Ignores alphabetic case x Ignores white space in pattern and allows comments. gc Doesnt reset the search position after a failed match s Lets the . Character match newlines. m Lets ^ and $ match embedded \n characters e Evaluate right hand side as an expression o Compiles the pattern only once The Translation Operator (tr///) Syntax: LVALUE =~tr/SEARCHLI ST/REPLACEMENTLI ST/ I t scans a string, character by character, and replaces each occurrence of a character found in SEARCHLI ST (which is not a regular expression)with the corresponding character fromREPLACEMENTLI ST I t returnsthenumber of charactersreplacedor deleted. I f nostringis specified viathe =~or !~operator, the$_ string isaltered. Example : #!usr/local/bin/perl $text = winners see gain, losers see pain; $count = ($test =~tr/e/E/); print $text; Print \ n no..of replacements =$count; This program displays: winnErs sEE gain,losErs sEE pain no..of replacements =6 The Translation Operator (Contd..) Modifier Description c (tr///c) Complements the search list. d Deletes unreplaced characters s Deletes duplicate replaced characters Example : #!usr/local/bin/perl $text = winners see gain, losers see pain; $count = ($test =~tr/e/E/c); print $text; #except e all other characters Print \ n no..of replacements =$count; #are replaced. This program displays: EEEEeEEEEeeEEEEEEEEEeEEEEeeEEEEE no..of replacements =26 The Translation Operator (Contd..) Different Pattern Delimiters I f thepattern contains lots of slash characters(/) ,we can also usedifferent pattern delimiter withthepattern. Example : #!usr/local/bin/perl $var = "winners / see / gain,losers / see pain"; I f( $var =~ m| see| ) { #match with pipes print String see Found\ n; } I f ( $var =~ m ?gain?) { #match with question marks. print String gain Found; } This program displays: String see Found String gain Found Perl also allows paired characters like braces and brackets.viz {},( ), <>,[ ]. Ex: $var =~s{gain}{GAI N}; The Parts of regular Expressions I n general regular expression can be made up of following parts. Characters Character Classes Alternative MatchPatterns Quantifiers Assertions Characters I n regular expression any single character matches itself, unless it isa metacharacterswithspecial meaning. Beside normal characters, Perl defines special characters that youcan usein regular expression. These character must start with backslash.(Otherwise Perl treatsit as a normal character). Character Description . (period) Used to match any single character except newline character Ex :$var1 = ~ /r.n/ # will match run , ran, ron Characters (Contd..) character Description \d It is equivalent to [0 - 9] Matches any digit. Ex1 : $var =~ /\d/ # Will match any digit. \D It is equivalent to [^0 - 9] Matches any non-digit. Ex1 : $var =~ /\D/ # Will match any non-digit. Characters (Contd..) character Description \w It is equivalent to [0-9a-zA-Z_] Matches a word character allowable in Perl variable name. i.e. Match any 'word' or alphanumeric character, which is the set of all upper and lower case letters, the numbers 0..9 and the underscore character _ Ex :if ( $var =~ /\w/) \W It is equivalent to [^0-9a-zA-Z_] Matches any non-word characters. Inverse of \w Ex :if ($var =~ /\W) Characters (Contd..) character Description \s It is equivalent to [ \t\n\r] Matches any white space character. i.e. a space ,a tab ,a newline ,a return Ex :if ($var =~ /\s/) \S It is equivalent to [^ \t\n\r] Matches any non-white space character. Ex :if ($var =~ /\S/) Characters (Contd..) character Description \Q Quote(disable) pattern metacharacters until \E found. Ex:#usr/local/bin/perl $var = success is not a *; If($var =~/*/){ print found in 1st if ; } If($var ~=/\Q*\E) { print found in 2nd if ; } It will display : found in 2nd if \E End case modification. Characters (Contd..) character Description \U Change the following characters to upper case until a \E sequence is encountered. Ex:$var = SUCCESS is not a * ; If($var =~/success/){ print found in 1st if ; } If($var ~=/\Usuccess\E) { print found in 2nd if ; } It will display: found in 2nd if \L Change the following characters to lower case until a \E sequence is encountered. Same like \U Characters (Contd..) character Description \u Change the next character to uppercase. Ex:#usr/local/bin/perl $var = SUCCESS is not a *; If($var =~/\us/){ print found only s ; } If($var ~=/\usu/) { print found su ; } It will display : found only s \l Change the next character to lower case. Character Classes A character class allows a set of possible characters, rather than just a single character, to match at a particular point in aregular expression. Character classes are denoted by brackets [...], with theset of characterstobepossiblymatchedinside. Matchesoneoccurrenceof anycharacter insidethebracket Ex 1: $var =~/w[aoi]nder/ #will match wander, wonder, winder Character Classes (Contd..) I f you use ^as the first character in a(if you use ^outside the character class[ ] it works as anchor) character class, then that character class matches any character not in the class. Ex1:$var =~/w[^aoi]nder #will look for wfollowedbysomething that is #noneof aor oor i. Alternative Match Patterns Alternative Match Pattern means that you can specify a seriesof alternatives for a patternusing| toseparatethem. | (called alternation) is equivalent to an or in regular expression. I t is usedtogiveachance. Ex: $var2 =~/hope| trust/ #will match either hopeor trust Alternatives are checked from left to right, so the first alternativethat matchesistheonethatsused. Grouping Alternatives Grouping[ ( ) ] allows parts of a regular expression to be treatedasa singleunit. Parts of a regular expression are grouped by enclosing them inparentheses. Used togroup similar terms by their common characters and onlyspecifiedthedifferences. Example: $var2 =~/(while| for)loop/ #will matcheither while loopor for loop Grouping Alternatives (Contd..) The pairs of parentheses are numbered fromleft to right by thepositionsof theleft parentheses. Perl places thetext that is matched by theregular expression in the first pair of parentheses into the variable $1, and the text matched by the regular expression in the second pair of parentheses into$2,andsoon. Example : #!usr/local/bin/perl my $text= "Testing"; if ($text =~ /((T| N)est(ing| er))/) { print " \ $1 = $1 \ t \ $2 = $2 \ t \ $3 = $3 \ n "; This program displays: $1 = Testing $2 = T $3 = ing Grouping Alternatives (Contd..) Therearethreepairsof parenthesesintheaboveexample. The first one is that which surrounds the whole regular expression, hence $1 evaluates to the whole matched text, whichisTesting. The match caused by the second pair of parentheses (T| N), whichisT, is assignedto$2. The third pair of parentheses (ing| er) causes $3 to be assignedthevalueing. Quantifiers Quantifiers says how many times something may match, instead of thedefault of matchingjust once. You can use quantifier tospecify that a pattern must match a specificnumber of times. Quantifiers in a regular expression are like loops in a program. Quantifiers (Contd..) character Description * It indicates that the string Immediately to the left should be matched zero or more times in order to be evaluated as a true. Ex1 : $var =~ /st*/ # Will match for the strings like st, sttr, sts , star, son . The regexp a* will search for a followed by either a or any other character. It matches all strings which contain the character a. + It indicates that the string Immediately to the left should be matched one or more times in order to be evaluated as a true. Ex:$var =~ /st*/ # Will match for the strings like st, sttr, sts ,star , but not son. Quantifiers (Contd..) character Description { } It indicates that how many times the string immediately to the left should be matched. {n} should match exactly n times. {n,} should match at least n times {n, m} Should match at least n times but not more than m times. Ex : $var =~ /mn{2,4}p/ # will match mnnp, mnnnp, mnnnnp . ? It indicates that the string Immediately to the left should be matched zero or one times in order to be evaluated as a true. Ex : $var =~ /st?r/ # will match either star or sttr. $var = ~/comm?a/ # will match either coma or comma Quantifiers (Contd..) Quantifiers are greedy by default, which means they will trytomatchas muchas theycan. Ex:$str =theyareplayers, arent they? $str =~s/.*are/were/; print $str; I t will print :werent they? Perl will use the *. Preceding are to match all the characters upto thelast areinthestr. Making Quantifiers Less Greedy To make Quantifiers less greedy that is ,to match the minimumnumber of times possible you followthequantifier witha? *? Matches zero or more times. +? Matchesoneor moretimes. ?? Matcheszeroor onetimes. {n}? Matchesn times. {n,}? Matchesat least n times {m,n} Matchesat least n timesbut morethan m times. Example : #!usr/local/bin/perl $text = They are players ,arent they?; $text =~s/.*?are/were/; print $text; This program displays: Were players ,arent they? Example : #!usr/local/bin/perl $txt = no, these are the documents, over there.; $txt = ~ s/the(.*?)e/those/; print $txt; This program displays: no, those are the documents, over there Making Quantifiers Less Greedy (Contd.) Assertions Assertions (also called anchors) used to match conditions withinastring, not actual data. Assertions are zero width because they do not consume any characterswhentheymatch. Anchor Description ^(caret) Appears at the beginning of the pattern and finds for a match at beginning of the line Ex : $var =~ /^su/ # Will match the strings those are starting with su i.e. . sun, success, super .. Assertions (Contd..) character Description $ Appears at the end of the pattern and finds for a match at end of the line Ex : $var =~ /at$/ # Will match the strings those ends with at i.e. . cat, rat , beat \Z Matches only at the end of a string, or before a new line at the end. It matches at the end of the match text, before the newline if any is present. Otherwise it is the same as \z. \z Matches only at the end of string. Assertions (Contd..) The differencebetween ^ and \ Ais that when you usethe m multiline-modifier, ^ matches the beginning of every line, but \ A retains its original meaning and matches only at theverybeginningof thewholestring. Character Description \A Matches only at the beginning of a string.(Similar to ^) \G It applies when we use a regular expression to produce multiple matches using the g pattern modifier. It re-anchors the regular expression at the end of the previous match, so that previously matched text takes no part in further matches. Works only with /g . Assertions (Contd..) WordBoundaries \ bmatchesona wordboundary. This occurs whenever a word character is adjacent to a non- word character . I t is equivalent to \ w\ W| \ W\ w. Within character classes \ b represents backspace rather than a word boundary, just as it normally does in any double- quoted string. Assertions (Contd..) WordBoundaries \ B matchesona non-wordboundary. This occurs whenever two word characters or two non-word charactersfall adjacent toeachother. I t is equivalent to \ w\ w| \ W\ W. WordBoundaries Example : #!usr/local/bin/perl $text = "one, ****, three, four"; #* is not a word $text1 = "one,****,three, four"; #character foreach ($text =~ /\ b\ w+\ b/g){ print $_, "\ t"; } print "\ n using \ \ B\ n"; foreach ($text1 =~ /\ B\ w+\ B/g){ print $_, "\ t"; } This program displays: one three four using \ B n hre ou Assertions (Contd..) Regular Expressions-Examples(1) character Description $var =~m/^\./ Will match for dot(.) at the beginning of the statement. ^ Used to match at the beginning of the line ,dot is a meta character so it has to preceded by \ $var =~ /\w+/ Will match a word, a nonempty sequence of alphanumeric characters and underscores such as trust , 12bar8 and kac_per. $var =~/start\s*end/ The strings start and end optionally separated by any amount of white space (spaces, tabs, newlines). Regular Expressions-Examples(2) character Description $var =~/o\.m/ It will match exactly o.m $var =~ /blue(colo(ur|r))/ Will match either bluecolor or bluecolour $var =~ s/\s+$// Removes(trims) the Trailing white space. Regular Expressions-Examples(3) character Description $var =~ s/^\s+// Removes(trims) the leading white space. Ex:$txt = trust in god; $txt ==~ s/^\s+// Print $txt; It will print : trust in god $var =~ m/(\d+)/ Will match complete first number. Ex :$txt = "day = 86400s or 1440 mor 24 h"; if($txt =~ m/(\d+)/){ print "\n\nFirst Number is $1"; } It will print : First Number is 86400 Subroutines Subroutine is a separate body of code designed to performa particular task. I t is sameas functioninC language. The I dea behind subroutines is that old programmingdictum divideandconquer. Subroutines allow you to divide your code into manageable parts, whichmakesoverall programmingeasier tohandle. Perl allows you to create subroutines using the sub control structure Example : #!usr/local/bin/perl $v1 =36; large_small(); #subroutine call before definition , parentheses must sub large_small{ if($v1 >40) { print "value is bigger than 40\ n"; } else{ print "value is smaller than 40\ n"; } } $v1 =45; large_small; #subroutine call after definition , parentheses are #optional This program displays: value is smaller than 40 value is bigger than 40 Subroutines (Contd..) Scope of variable Perl variableshaveglobal packagescopebydefault. When we change a variable value in subroutine, well be changingit intherest of theprogramby mistakeas well. Wecan create a variables that areentirelylocal tosubroutine byusingkeywordlocalor my. They can have same name as global variable and not affect thoseglobal variablesat all. Example : #!usr/local/bin/perl $v1 =36; incr(); print "value of v1 =$v1\ n"; sub incr { my $var =$v1; print "value before incrementing =$var\ n"; $var++; print "value after incrementing =$var\ n"; } This program displays: value before incrementing =36 value after incrementing =37 value of v1 =36 Scope of variable (Contd..) Parameters and Arguments You can pass values to subroutine by placing in parentheses. (ex: incr($v1);) When you pass values to a subroutine ,those values are storedina special arraynamed @_. Beside accepting passed values, subroutines can also return valuesusingreturnkeyword. Example : #!usr/local/bin/perl $sum =add(10,20); print "sum =$sum\ n; sub add { ($val1, $val2) = @_; return $val1+$val2; } This program displays: sum =30 Perl returns the last value in a sub routine so you can omit thereturnkeyword. I ntheaboveexample$val1+$val2gives thesame. Parameters and Arguments (Contd..) Parameters and Arguments (Contd..) Different waysof readingargumentspassedtosubroutine Subadd { $val1=$_[0]; $val2=$_[1]; } Subadd { $val1=shift@_ ; (or) shift ; $val2=shift@_ ; (or) shift ; } I na subroutine,shift uses@_ by default soyoucan use shift directly. Recursion Recursion happens when a subroutine calls itself, either directly, or indirectly, viaanother subroutine Example : #!usr/local/bin/perl0 $fact = fact(6); print "factorial of given number =$fact\ n; sub fact { local $val =shift(@_); if($var ==0) { return 1; } elsif($val==1) { return $val ; } else{ return $val*fact($val-1); }} This program displays: factorial of given number =720 PassingLists Example : #!usr/local/bin/perl @small= qw(sachin dravid ganguly ); @big =case_convert(@small); print "@big"; sub case_convert { @low =@_; @caps =map(uc ,@low); } This program displays: SACHI N DRAVI D GANGULY Subroutines Examples(1) Nestedsubroutines Example: #!usr/local/bin/perl call(); sub call{ display(); sub display { print "you are in inner subroutine\ n" ; } print "you are in outer subroutine\ n" ; } print "you are in main\ n" ; This programdisplays: you are in inner subroutine you are in outer subroutine you are in main Subroutines Examples(2) I n general passing arrays or hashes flattens their elements into one long list, so its a problemif you want to send two or moredistinct arraysor hashes. To preserve integrity, you can pass references to arrays or hashesinstead. References can be created by using a backslash (\ ) operator ona variable. I t is similar toaddress-of (&)operator in C. \ $var Scalar Reference \ @arr Array Reference \ %hash Hash Reference Pass by Reference Pass by Reference (Contd..) I f you pass \ $a (a reference to the $a scalar variable) to a subroutine, then in the subroutine the variablethat receives that parameter receives a reference (or a "pointer") pointingtothe$ascalar Dereferencingreferences(Usingprefixes$, @,% ,->) $$ref_var Scalar Dereference $$ref_arr Array Dereference #array is copied into scalar( ref_arr) $$ref_hash HashDereference Example: #!usr/local/bin/perl @arr =qw(America England france); print "before: arr =" . join(', ', @arr) . "\ n"; change(\ @arr); print "after: arr =" . join(', ', @arr) . "\ n; subchange { my$ref_arr =shift; $$ref_arr[0]="China"; @{$ref_arr}[1] ="I ndia"; # {} creates a block. $ref_arr ->[2] ="J apan"; # ->called arrow operator. } This programdisplays: before: arr =America, England, france after: arr =China, I ndia, J apan Pass by Reference (Contd..) Returning by Reference I f you return two arrays normally, their values are flattened into one long list. I f you return references to arrays, you can deferencethosearraysandreachtheoriginal arrays. Example : #!usr/local/bin/perl sub get_strings{ @str1 = qw(Asia Austrelia Africa); @str2 =qw(Brown White Black); return \ @str1 ,\ @str2; } ($ref_str1 ,$ref_str2) =get_strings; print "@$ref_str1 \ n"; print "@$ref_str2 \ n"; This program displays: Asia Austrelia Africa Brown White Black You can get lot of material on references that comes with Perl( perldoc perl) File Handling A filehandle is nothing more than a nickname for the files youintendtousein your PERL scriptsandprograms. Filehandles are a connection between our program and an external datasource Filehandles inPerl area distinct datatype. STDI N or standard input represents the default input filehandleandusuallyconnectedtothekeyboard. STDOUT or Standard output represents the default output filehandle and usually connected to the console device(screen) File Handling (Contd..) STDERR or Standard error is the default output error filehandle andusuallyconnectedtoscreen. Openingafile Toopena file,usetheopenfunction. Syntax: openFI LEHANDLE ,MODE,LI ST openFI LEHANDLE ,EXPR openFI LEHANDLE The open function takes a filename and creates thehandle for it. File Handling (Contd..) Opening a file (Contd..) The open function returns a true(nonzero) value if successful otherwise it returns undefined value. The filehandle will create in either case but if the call to openfails, thefilehandlewill beunopenedandunassigned. I f the open fails the reason is stored in special variable $!,whichproducesamessageinstringcontext. File handling is most error prone ,so use open and die together. Ex: open (HANDLE, $filename) or die "Can't open $filename: $!\ n"; File Handling (Contd..) Openingafile(Contd..) openunderstandstotal sixmodes. MODE Symbol Description Read < Open file handle for read access only. Ex :open FILHND <$file; This is the default mode and so the < prefix is usually optional Write > Open the file for write access only. Ex :open FILHND >$file; If the file doesnt exist then it is created and opened. If the file does exist then it overwrite the existing contents File Handling (Contd..) Openingafile(Contd..) MODE Symbol Description Append >> Open the file for write access only. Ex :open FILHND >>$file; If the file doesnt exist then it is created and opened. If the does exists then it appends that file. Read- update +< Open the file for read and write access. Ex :open FILHND +<$file; If the file does not exist then the open fails. If the file does exist then it overwrite(contents are preserved for reading) the existing contents. File Handling (Contd..) Openingafile(Contd..) MODE Symbol Description Write- update >+ Open the file for read and write access. Ex :open FILHND >+$file; If the file doesnt exist then it is created. If the file does exist then it is truncated and its existing contents are lost.(usually used for opening a new file) Append- update >>+ Open the file for read and write access only. Ex :open FILHND >>+$file; If the file doesnt exist then it is created and opened. If the file does exist then both read and write commence from the end of the file. ReadingLines Example: exam.txt :: winners dont do different things. winners do things differently.. success is not a destination. Perl Script: #!usr/local/bin/perl open FI LE, "exam.txt" or die$!; $lineno; while(<FI LE>){ print $lineno++," \ t"; print "$_"; } This programdisplays: 0 winners dont do different things. 1 winners do things differently. 2 success is not a destination. File Handling (Contd..) File Handling (Contd..) <> iscalledreadlineor diamondoperator. I n the above example while (<FI LE>) is equivalent to while(defined($_=<FI LE>). Above statement will reads a line fromfile and assigns it to $_andchecks whether it isdefinedor not. I f it is not defined ,probably at the end of the file so it will comesout of theloop. File Handling (Contd..) Perl provides aspecial handlecalledARGV. I t reads thefiles fromthe commandline and opens themall if specified. I t will read from Standard input(STDI N) if nothing is specified on thecommandline. I f you don't specify anything in the angle brackets, whatever isin@ARGV is usedinstead. Commandlinepassing Example: exam.txt :: winners dont do different things. success is not a destination. Perl Script: #!usr/local/bin/perl $match =do; while(){ I f(/$match/) { print FOUND\ n; } else { printNOT FOUND; } } This programdisplays: FOUND NOT FOUND File Handling Examples(1) Commandlinepassing Example: #!/usr/local/bin/perl print "Filename: "; my $infile=<>; chomp $infile; print "New name: "; my$outfile=<>; chomp $outfile; open I N, $infile; open OUT, ">$outfile"; print OUT <I N>; closeI N; # syntax :: close<filehandle> closeOUT; This programdisplays: perl filename.pl exam.txt Filename: exam.txt Newname:copy_exam.txt #it will create this file. File Handling Examples(2) Commandlinepassing Example:- if (open (LOGFI LE, ">>message.log")) { print LOGFI LE ("This is message number 3.\ n"); print LOGFI LE ("This is message number 4.\ n"); close(LOGFI LE); #! closefunction } This programdisplays: This is message number 1. This is message number 2 This is message number 3. This is message number 4. File Handling Examples(3) print, printf, and write Functions print function writes to the filespecified, or to the current default fileif nofileis specified. Ex: print ("Hello, there!\ n"); print OUTFI LE ("Hello, there!\ n"); write function uses a print format tosend formatted output tothefilethat is specifiedor tothecurrent default file. Ex: write(CD_REPORT); Directories Handling print function writes to the filespecified, or to the current default fileif nofileis specified. Ex: print ("Hello, there!\ n"); print OUTFI LE ("Hello, there!\ n"); write function uses a print format tosend formatted output tothefilethat is specifiedor tothecurrent default file. Ex: write(CD_REPORT); Directories Handling (Contd..) Tocreatea newdirectory, call thefunction mkdir. Syntax:mkdir (dirname, permissions); Ex: mkdir ("/u/public /newdir ", 0777); Toset a directory to bethe current workingdirectory, usethe function chdir. Syntax: chdir (dirname); Ex:chdir ("/u/public/newdir"); Directories Handling (Contd..) To open the directory (already existing) ,use the function opendir Syntax: opendir (dirvar, dirname); Ex: opendir (DI R, "/u/kacper/mydir"); Toclosean openeddirectory, usetheclosedirfunction Syntax: chdir (dirname); Ex: closedir (mydir); ThanQ