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)