650 likes | 890 Views
Perl. Practical Extraction and Report Language. Objectives. Introduction Basic features Variables Operators and functions Control structures Input and output. Introduction to Perl. Perl is a script language Elements of csh, sh, awk and C Used for: Text manipulation CGI scripting
E N D
Perl Practical Extraction and Report Language
Objectives • Introduction • Basic features • Variables • Operators and functions • Control structures • Input and output
Introduction to Perl • Perl is a script language • Elements of csh, sh, awk and C • Used for: • Text manipulation • CGI scripting • System administration • Written and maintained by Larry Wall • Websites • www.perl.comwww.perl.org • www.perldoc.comwww.perlfoundation.org • www.cpan.org
Introduction to Perl • Example: #! /usr/bin/perl # example of perl script print “hello world\n”; print(“hello world\n”); printf(“hello world\n”); printf(“%s\n”, “hello world”);
Variables • Scalar • Single valued • Array • Indexed collection of values • Hash • Collection of key, value pairs
Scalar Variables • string $word = “test”; print “here it is: $word \n”; • quotes around string optional, if clear $word = test; print “here it is $word \n”;
Scalar Variables • integer $i = 2; print $i; print “$i\n”; $j = 2 * $i + 4; printf(“Result %d \n”, $j); print “Result $j \n”;
Scalar Variables • floating point $f = 1.234; $g = 2 * $f + 4; printf("Result %f\n", $g); print "Result $g\n";
Reading Input • from Standard input print("enter a number: "); $number = <>; chop($number); print("here it is: $number \n"); convert.pl
Array Variables • Array holds list: ("one", "two", "three") (1, 4, 5, 7, 8, 0) (1.2, 4.3, 6.5) (“one”, 2, 3.14159) ($word, $i, $f)
Array Variables • array variable starts with @ @list=("one", "two", "three"); print “@list \n”; • array element starts with $ print("here it is: $list[0]\n"); print("here it is: $list[1]\n"); print("here it is: $list[2]\n");
Array Variables • array slice @list=("one", "two", "three”, 4); @a = @list[1..3]; @b = @list[2,0]; • or: @c = ("one", "two", "three”, 4)[2,0]
Array Variables • assignment ($first, $second) = (“one”, “two”); @list = ($first, $second); ($o1, $o2) = @list; • array size $size = @array; • highest array index $#array
Array Variables • example: swap two variables $temp = $first; $first = $second; $second = $temp • better: ($first, $second) = ($second, $first);
Array functions • to remove element from front • shift @array; • to remove element from back • pop @array; • to reverse elements • reverse @array; • to sort elements • sort @array;
Variables • Hash: associative array %animals = ("cat“ => 10, "dog“ => 12, "fish“ => 23); print("1: $animals{'cat'}\n"); print("2: $animals{'dog'}\n"); print("3: $animals{'fish'}\n");
Hash variable operation %animals = ("cat“ => 10, "dog“ => 12, "fish“ => 23); %index = reverse %animals; print("1: $index{‘10'}\n"); print("2: $index{‘12'}\n"); print("3: $index{‘23'}\n");
Hash variable operation %animals = ("cat“ => 10, "dog“ => 12, "fish“ => 23); @names = keys %animals; @numbers = values %animals;
Special Variables • Default parameter: $_ • Used by many operations when no parameters are given • Example: $_ = “hello world\n”; print;
Command line arguments • $ARGV[0], $ARGV[1], … print("$ARGV[0], $ARGV[1]\n"); • With a loop: $size = @ARGV; for ($i = 0; $i < $size; $i++) { print("$ARGV[$i]\n"); }
Environment variables • %ENV hash • examples: print $ENV{HOME}; print $ENV{PATH}; $ENV{EDITOR} = “emacs”;
Operators • numeric: + - * / % ++ -- • boolean: && || ! and or not & | ^ • string: • . • x • examples: $a = “foo” . “bar”; $b = “number “ . 1; $c = “1” . “2”; $d = “ba” . (“na”x4); $e = 1 x 3;
String functions • remove last character: • chop • remove last character, if it is “\n”: • chomp $a = “foobar\n”; print chop(a); print chomp(a);
String function: split • split string into words • needs word separator • example: $test = “one two three”; @list = split “ “, $test; print “@list\n”;
String function: split • split with no parameters will split $_ on whitespace • example: $_ = “one two three”; print split;
String function: split • word separator examples: • split on string • “:” • split on regular expression • /e.e/ • /[abc]/
String function: join • opposite of split: @list = (“one”, “two”, “three”); $text = join “?”, @list;
String function: substr • manipulate substring $string="the glistening trophies"; print substr($string, 15); # trophies print substr($string, -3); # ies print substr($string, -18, 9); # listening print substr($string, 4, 4); # glis substr($string, 7, 4, "tter"); # Returns "sten" print $string; # the glittering trophies substr($string, 7, 4) ="tter"; # Functions as lvalue
Control Structures • sequence: • run until end of file • exit and die • conditional: • if-then-else, unless • loops: • for • while • functions
Sequence • all statements are terminated with semicolon • to end execution • exit; • to end with error message • die(“it was the food”); • to just print a warning (without exiting) • warn(“it is too warm in here”);
Conditional • if if ( $i > 10 ) { print “yes\n”; } else { print “no\n”; }
Conditional • unless unless ( $i > 10 ) { print “no\n”; } else print “yes\n”; }
Conditional • one liners: print “no\n” unless ( $i > 10 ); print “yes\n” if ( $i > 10 ); die(“unhappy”) unless ( $happy > 0);
Loops • C-like for loop: for ($i=0; $i<5; $i++) { print “Counter is $i\n”; } • simplified: for $i (0..4) { print “Counter is $i\n”; }
Loops • for arrays: @array = (“one”, “two”, “three”); foreach (@array) { print “value is $_ \n”; }
Loops • while: $i=10; while ($i > 5) { print “value is $i \n”; $i--; }
Loops • special keywords within while: next skip to next iteration last end loop redo rerun iteration
Pattern matching • Perl supports regular expressions • basic and extended • enclosed in forward slash “/” • match operator =~ • return boolean true or false $sea = “water sand jaws swimmers”; print “Shark Alert !” if $sea =~ /jaws/;
Pattern matching • special characters: \w - word \W - non word \s - space \S - non space \d - digit \D - non digit \b - word boundary
Pattern matching • pattern may include parenthesis () • identifies matched junks as $1, $2, $3 $sea = “water sand jaws swimmers”; print “Shark Alert !” if $sea =~ /(j??s)/; print $1;
Pattern substitution $test = “he said she said”; $test =~ s/said/did/; frequency.pl
Sub-functions • example: sub greeting { print “hello world\n”; } … greeting();
Sub-functions • parameters are passed via @_ array: sub action { ($one, $two, $three) = @_; warn “too few parameters” unless $one && $two && $three; print “The $one $two on the $three\n”; } … action(“cat”, “sat”, “hat”);
Sub-functions • return results via return: sub action { ($one $two $three) = @_; warn “too few parameters” unless $one && $two && $three; return “The $one $two on the $three\n”; } … print action(“cat”, “sat”, “hat”);
Sub-functions • local variables: visible only with scope of sub-function sub getwords { my pat = $_[0]; my ($line, @subwords); $line = <>; chop($line); @subwords = split /$pat/, $line; return @subwords; } wordcount.pl
Reading Input • from Standard input print("enter a number: "); $number = <STDIN>; print("here it is: $number");
Reading Input • from Standard input $i = 0; while ($line = <STDIN>) { print $line; $i++; } print(“line count: $i \n");
File I/O • based on file handle • predefined file handles STDIN STDOUT STDERR print STDERR “…oohh, something went wrong !\n”;
File I/O • to create new file handle open file • open filehandle filename; • filename can be: • “file” • “<file” • “>file” • “>>file” • “ls|” • “|wc”
Reading from a File if (!open(FILE, "$ARGV[0]")) { print "cannot open: $ARGV[0]\n“ ; } while ($line = <FILE>) { print "$line“ ; }