1 / 11

Perl

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.

Télécharger la présentation

Perl

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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);

  3. 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;

  4. Pattern Matching Perl supports two relational operators for pattern matching. string =~/pattern/ string !~/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).

  5. Anchoring a Pattern Anchor symbols for use in patterns. ^ $ Examples "itinerary" =~ /^in/ "itinerary" =~ /^it/ "itinerary" =~ /tin/ "itinerary" =~ /tin$/ "itinerary" =~ /ary$/ "itinerary" =~ /^ary$/ "abc" =~ /^abc$/

  6. Patterns == RE’s Regular expression notations supported in Perl patterns. * | + Examples "aluminum" =~ /max|min/ "aluminum" =~ /max | min/ "football" =~ /l*/ "football" =~ /z*$/ "football" =~ /^fo+/

  7. More RE Notation Parentheses can be used to group parts of REs. Patterns are concatenated by position. Examples "soooo" =~ /^s(oo)*$/ "abaaba" =~ /^(aba)+$/ "heebee jeebees" =~ /(ee)+(ae)*bee/ "12.24 311.42 " =~ /^((0|1|2|3|4)+\.(0|1|2|3|4)+ )+$/

  8. Symbols for character sets A few special symbols are permitted to match a single character from a set of characters. . \d \D \w \W \s \S You can build your own set by enclosing characters in brackets. Examples "4-letter word" =~ /^\w..\S*\s\w+$/ "Ape" =~ /^[AEIOUY][b-r].$/

  9. RE Repeating Symbols In addition to the standard RE repetition symbols (* and +) Perl supports additional repetition operations (all postfix). ? {m} {m,n} {m,} Examples "+73.111" =~ /^[+-]?[0-9]{1,}\.1{3}$/ $listing="-rw-r--r--@ 1 driley staff 49 Jan 6 15:34 z.txt"; $listing =~ /^.{15}(\w+)/

  10. 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”; if ("abeaceapteee" =~ /((a..)+(e*))/) { print $1,"\n"; print $2,"\n"; print $3,"\n"; }

  11. 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"; }

More Related