70 likes | 228 Views
Perl. a bit of history…. Perl created in 1987 by Larry Wall. “ Perl was designed to work more like a natural language. ”. Perl is open source. “ So, whatever it takes to give away my software and get it used, that ’ s great. ”. Probably best known as a CGIscripting language. References.
E N D
Perl a bit of history… Perl created in 1987 by Larry Wall. “Perl was designed to work more like a natural language.” Perl is open source “So, whatever it takes to give away my software and get it used, that’s great.” Probably best known as a CGIscripting language References www.perl.org www.perltutorial.org www.comp.leeds.ac.uk/Perl/start.html www.troubleshooters.com/codecorn/littperl/perlreg.htm
Example Perl Program # This code assumes that the file contains one number per line. # open(NUMFILE, "numbers.txt"); $lineCount = 0; $total = 0; while ( $line = <NUMFILE> ) { $lineCount++; $total = $total + $line; } if ($lineCount == 0) { print "Average of input file numbers is 0\n”; } else { print "Average of input file numbers is ", $total/$lineCount, "\n"; } close(NUMFILE);
String Processing in Perl CGI output == HTML and HTML is a string Perl’s scalar variables can store strings, as well as numbers or Booleans. $someString = "String him up!"; Strings may contain string variables. $str2 = "Cowboys in the movies say things like, '$someString'."; Escape Characters are embedded using a backslash – ( \ ) \t ... (tab) \n ... (end of line) \" ... " (double quote) \$ ... $ (dollar sign) \. ... . (period) (See a Perl tutorial for others.) Concatenation – ( . ) $firstLine = "Oh no!\n"; $secondLine = "Not more American Idol!\n"; $message = $firstLine . $secondLine;
Pattern Matching Perl supports two relational operators for pattern matching. is true when string contains one or more instances of pattern string =~/pattern/ string !~/pattern/ is true when string contains no instances of pattern. Example if ("Facebook" =~ /oo/) { print "There is 'oo' in Facebook.\n"; } $bigRiver = "Mississippi"; if ($bigRiver =~ /oo/) { print "There is 'oo' in the Mississippi.\n"; } elsif ($bigRiver !~ /is/) { print "There is no 'is' in the Mississippi.\n"; } else { print "There is an 'is' in the Mississippi.\n"; } Note that the Perl pattern matching algorithm normally … (1) proceeds left to right, and (2) matches the longest possible substring(s).
Pattern Assignment Parenthesized parts of patterns are automatically assigned to variables. Matched substrings are assigned left to right to $1, $2, and so forth. For multiple matches the last one is stored. Examples "abcdzefg" =~ /((ab)(cd))z((e)(fg))/; print $1,"\n"; print $2,"\n"; print $3,"\n"; print $4,"\n"; print $5,"\n"; print $6,"\n”; output: output: if ("abeaceapteee" =~ /((a..)+(e*))/) { print $1,"\n"; print $2,"\n"; print $3,"\n"; }
Search & Replace string =~s/pattern/string2/ If a match occurs, then the matched pattern is replaced by string2. Examples $word = "zoo"; $word =~ s/o+/ebra/; $str = "Now is the time for all good perls."; $str =~ s/\W*$//; #remove trailing non-alphanumeric while ( $str =~ s/\s*(\w+)$// ) { print $1,"\n"; }