2. History
⢠Creator, Maintainer, Chief Architect â Larry
Wall
⢠Practical Extraction and Report Language
⢠Pathologically Eclectic Rubbish Lister
⢠Pearl
⢠Features from C, awk, tcl/tk
3. Basic
⢠Use any editor to write a Perl program
⢠Extension is .pl
⢠Run in Unix as $perl <filename>
⢠Make it executable and run as
$./<filename>
4. Hello World!
⢠Always the first line
#!/usr/local/bin/perl is #!<pathtoperl>
print âHello Worldnâ; ⢠print prints to the
standard output
⢠print can also be
used for printing
into files
5. Standard Input/Output
⢠Get the input from the user using <STDIN>
â $x = <STDIN> gets the input from the user
⢠Print to the standard output
â print $x prints the value of $x
â print âhello â,âworldâ,ânâ prints hello world and newline
character
â print âhello â.âworldâ.ânâ also prints hello world and
newline character
â So what's the difference?!?!
7. Scalar Variables
⢠Basic kind
⢠Can hold both numerics and strings and
interchangeable
â Eg.: $temp = âhiâ
â $temp = 9
⢠Starts with â$â symbol followed by a letter
and then by letters, numbers or
underscores
⢠Case sensitive
8. Numbers
⢠Integers and Floats
⢠Internally, Perl computes with double float
⢠Integer Literals
â 25
â 013 and 13 are different!!!!
⢠Float Literals
â 1.3
â -13e-19 == -1.3E-19
10. String Literals
⢠Single quoted
â Anything inside the quotation has no special
meaning except ' and
â 'hey'
â 'heytwazzup' is heytwazzup
⢠Double quoted
â Some characters have special meanings
â âheytwazzupâ is hey wazzup
12. Number <--> String Operators
⢠Careful with the Operators!
⢠(1+1) x 3 = 222
⢠âaâ + âbâ is not an error
⢠Be CAREFUL!
13. Assignment Operators
⢠Assignment $LHS = $RHS
â The value on the right is assigned to the left
â $x = ($y = 13)
â $x = $y = 13
⢠$x and $y has the value 13
⢠Binary Assignment
â If the variable in LHS and RHS are same
â $x = $x + 13 ď§ď¨ $x += 5
â Similarly, for other binary operators
14. Auto [Increment, Decrement]
⢠Similar to C
⢠For both integers and float
⢠++ operator adds 1 to its operand
⢠-- operator subtracts 1 from its operand
⢠$x = $y++ is different from $x = ++$y
15. Chop and Chomp
⢠Chop
â Removes and returns the last character from the
input
â $x = âhuhnâ
â chop ($x) makes $x = âhuhâ
â chop ($x) makes $x = âhuâ
⢠Chomp
â Removes only the ânâ from the input
â $x = âhuhnâ;
â chomp ($x) makes $x = âhuhâ
â chomp ($x) makes $x = âhuhâ
16. Array
⢠List is ordered scalar data
⢠Array holds list
⢠No limits
⢠Array variable name starts with @
â @var1
⢠Individual elements can be accessed
using $
â $var1[0] is the first element
17. Array Examples
⢠List literals
â (1,2,3)
â (âhelloâ,1,1.2)
â ($x+$y,10)
â List constructor
⢠(1..5) is (1,2,3,4,5)
⢠Array
â @a = (âheyâ,âhowâ,âareâ,âyouâ)
18. Array Functions
⢠Sort
â @x = sort (@y) will sort the array y and store it
in x
⢠@x = sort (âbâ,âaâ,âcâ) will make @x = (âaâ,âbâ,âcâ)
⢠@x = sort (3,12,4,15) will make @x = (12,14,3,4)!!
⢠Sort by number
â @x = sort {$a <=> $b} (3,12,4,15) will make @x
= (3,4,12,15)
19. Array Functions (cont.)
⢠Reverse reverses the order of the
elements in the array
â @x = reverse (3,2,8) will make @x = (8,2,3)
⢠Chomp removes the ânâ from all the
elements of the array
â @x = chomp (âhellonâ,âheynâ) will make @x =
(âhelloâ,âheyâ)
20. Regular Expressions
⢠Useful and Powerful string manipulation
functions
⢠RE is a pattern to be matched against a
string
⢠The regular expression is contained within
slashes and the matching operator is =~
21. Is it easy?!?
⢠To find a pattern âhahahaâ in a string $x
â $x =~ /hahaha/
â If the above statement is true then âhahahaâ is
present in $x
22. Regular Expression Characters
⢠Some special regular expression
characters
â . Single Character except newline
â ^ Beginning of line
â $ End of line
â * Zero or more of the last character
â + One of more of the last character
â ? Zero or one of the last character
24. Some more symbols
⢠Square brackets
â To match any one character inside the bracket
â Inside the bracket â^â indicates not
â And â-â indicates between
⢠Parenthesis
â To group characters together
⢠â|â
â Either or
26. Substitution
⢠$varname =~ s/old/new
â The regular expression old will be replaced by
new
⢠$varname =~ s/old/new/g
â All the old regular expressions will be replaced
by new
27. Split
⢠Splits a string based on the regular
expression given
â @parts = split (/<regExp>/, $x)
â Eg.: $x = 1:2:3:4
â @parts = split (/:/, $x)
â @parts = (1,2,3,4)