Download presentation
Presentation is loading. Please wait.
Published byColin May Modified over 9 years ago
1
Introduction To Perl Susan Lukose
2
Introduction to Perl Practical Extraction and Report Language Easy to learn and use
3
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
4
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: 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 It's a plain file -d $file It's a directory -r $file Readable file -x $file Executable file -w $file Writable file -e $file File exists ComparisonNumericStringReturn Value Equal==eqTrue if $a is equal to $b Not equal!=neTrue if $a is not equal to $b Less than<ltTrue if $a is less than $b Greater than>gtTrue if $a is greater than $b Comparison cmp0 if equal, 1 if $ a is greater, -1 if $b is greater
5
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"; }
6
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/; #return true if var $string contains the name of the president $string =~ /Bill Clinton/; #same result as previous statement s: 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/; #true only when "Bill Clinton" is the first text in the string $: This is the "end of line" symbol. $string =~ m/Bill Clinton$/; #true only when "Bill Clinton" is the last text in the string i: 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"
7
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
8
Perl Build-in Functions Get all upper case with: $name = uc($name); Get only first letter uppercase: $name = ucfirst($name); Get all lowercase: $name = lc($name); Get only first letter lowercase: $name = lcfirst($name); Get the length of a string: $size = length($string); Extract 5-th to 10-th characters from a string: $part = substr($whole,4,5); Remove line ending: chomp($var); Remove last character: chop($var); Show position of substring in string: $pos = index($string,$substring); Show position of last substring in string:$pos = rindex($string,$substring); Print a string print (“My name is ”.$name); Splits up a string and places it into an array$info = "Caine:Michael:Actor:14, Leafy Drive"; @personal = split(/:/, $info); which has the same overall effect as @personal = ("Caine", "Michael", "Actor", "14, Leafy Drive");
9
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"); }
10
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= ; close(DAT); foreach $stu (@raw_data) { if($stu =~/unix/i) { print($stu."\n"); }
11
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"); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.