Introduction To Perl Susan Lukose Introduction to Perl • Practical Extraction and Report Language • Easy to learn and use Perl Variables • Scalar : An individual value (number or string). A scalar variable is preceded by $. $answer=42; $pi=3.14; $pet=“dog”; $product=$answer*$pi; $mystring=“ I have pet ” .$pet; • Array : An ordered list of scalars, accessed by scalar’s position in the list. A array variable is preceded by @. @home=(“couch”, “chair”, “table”); $home[0]=“couch”; $home[1]=“chair”; $home[2]=“table”; • Hash : An unordered list of scalars, accessed by some string value that is associated with each scalar. A hash variable is preceded by %. %longday= ( “sun”=>”Sunday”, “mon”=>”Monday”, “tue”=>”Tuesday”, ); $longday{“mon”}; will have the value Monday • Subroutine : A callable chunck of perl code. A subroutine variable is preceded by &. sub mysub { command; } &mysub; To call the subroutine Operators • Assignment Operator: $var = "string"; Puts the string into $var $var = 5; Puts a number into $var $var = $var. "string"; Appends string to $var • • Comparison Operator: Comparison Numeric String Return Value Equal == eq True if $a is equal to $b Not equal != ne True if $a is not equal to $b Less than < lt True if $a is less than $b Greater than > gt True if $a is greater than $b Comparison <=> cmp 0 if equal, 1 if $ a is greater, -1 if $b is greater Logical Operator: AND,OR and NOT are regularly used in if() statements if($first && $second){....;} if($first || $second){....;} if($first && ! $second{....;} means that $first must be true but $second must be false. • File Test Operator: -f $file -d $file -r $file -x $file -w $file -e $file It's a plain file It's a directory Readable file Executable file Writable file File exists Branching and Looping • if if(condition){ command; }elsif(condition){ command; }else{ command; } • while while(condition){ command; } • for for($variable=0;$variable<condition;$variable++) { command; } • foreach for $info (@data){ print $info,"\n"; } Regular Expression A regular expression is an abstract formulation of a string making it possible to do complex string comparisons, selection, replacements and parsing. • =~ : This operator appears between the string var you are comparing. $string =~ m/Bill Clinton/; #return true if var $string contains the name of the president $string =~ s/Bill Clinton/Al Gore/; #replace the president with the vice president • !~ : Just like =~, except negated. • / : This is the usual delimiter for the text part of a regular expression. $string =~ m/Bill Clinton/; #return true if var $string contains the name of the president $string =~ s/Bill Clinton/Al Gore/; #replace the president with the vice president • m : The match operator. $string =~ m/Bill Clinton/; $string =~ /Bill Clinton/; • s #return true if var $string contains the name of the president #same result as previous statement : The substitution operator. $string =~ s/Bill Clinton/Al Gore/; #replace the president with the vice president • ^ : This is the "beginning of line" symbol. $string =~ m/^Bill Clinton/; • $ : This is the "end of line" symbol. $string =~ m/Bill Clinton$/; • i #true only when "Bill Clinton" is the first text in the string #true only when "Bill Clinton" is the last text in the string : This is the "case insensitivity" operator when used immediately after the closing delimiter. $string =~ m/Bill Clinton/i; #true when $string contains "Bill Clinton" or BilL ClInToN" Regular Expression • Wildcards . Match any character \w Match "word" character (alphanumeric plus "_") \W Match non-word character \s Match whitespace character \S Match non-whitespace character \d Match digit character \D Match non-digit character \t Match tab \n Match newline \r Match return • Repetitions * Match 0 or more times + Match 1 or more times ? Match 1 or 0 times {n} Match exactly n times {n,} Match at least n times {n,m} Match at least n but not more than m times $string =~ m/\s*rem/i; #true if the first printable text is rem or REM Perl Build-in Functions Get all upper case with: Get only first letter uppercase: Get all lowercase: Get only first letter lowercase: Get the length of a string: Extract 5-th to 10-th characters from a string: Remove line ending: Remove last character: Show position of substring in string: Show position of last substring in string: Print a string Splits up a string and places it into an array $name = uc($name); $name = ucfirst($name); $name = lc($name); $name = lcfirst($name); $size = length($string); $part = substr($whole,4,5); chomp($var); chop($var); $pos = index($string,$substring); $pos = rindex($string,$substring); print (“My name is ”.$name); $info = "Caine:Michael:Actor:14, Leafy Drive"; @personal = split(/:/, $info); which has the same overall effect as @personal = ("Caine", "Michael", "Actor", "14, Leafy Drive"); Examples • Print Hello World #!/usr/bin/perl print ("Hello World\n"); • variables #!/usr/bin/perl $a=10; $b=$a*100; print ("Product of ".$a." times 100 is ".$b."\n"); • Comparisons #!/usr/bin/perl $str1="product"; if($str1 eq "sum") { $a=10; $b=$a+100; print ("Sum of ".$a." and 100 is ".$b."\n"); } elsif($str1 eq "product") { $a=10; $b=$a*100; print ("Product of ".$a." times 100 is ".$b."\n"); } Examples • Print file names of the current directory #!/usr/bin/perl use Cwd; $dirtoget=getcwd; opendir(IMD, $dirtoget) || die("Cannot open directory"); @thefiles= readdir(IMD); closedir(IMD); foreach $f (@thefiles) { print ($f."\n"); } • Reading a file and looking for a match in the content #!/usr/bin/perl $data_file="student.txt"; open(DAT, $data_file) || die("Could not open file!"); @raw_data=<DAT>; close(DAT); foreach $stu (@raw_data) { if($stu =~/unix/i) { print($stu."\n"); } } Examples • Print perlfile names of the current directory #!/usr/bin/perl use Cwd; $dirtoget=getcwd; opendir(IMD, $dirtoget) || die("Cannot open directory"); @thefiles= readdir(IMD); closedir(IMD); foreach $f (@thefiles) { if($f=~/(.+).(pl$)/i && $f!~/^\./i) { print ($1."\n"); } }