Download presentation
Presentation is loading. Please wait.
Published byBertina Glenn Modified over 9 years ago
1
Perl created in 1987 by Larry Wall. Perl is open source Probably best known as a CGIscripting language “Perl was designed to work more like a natural language.” a bit of history… “So, whatever it takes to give away my software and get it used, that’s great.” References www.perl.org www.perltutorial.org www.comp.leeds.ac.uk/Perl/start.html www.troubleshooters.com/codecorn/littperl/perlreg.htm
2
# This code assumes that the file contains one number per line. # open(NUMFILE, "numbers.txt"); $lineCount = 0; $total = 0; while ( $line = ) { $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
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
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
Anchor symbols for use in patterns. ^$^$ Examples "itinerary" =~ /^in/ "itinerary" =~ /^it/ "itinerary" =~ /tin/ "itinerary" =~ /tin$/ "itinerary" =~ /ary$/ "itinerary" =~ /^ary$/ "abc" =~ /^abc$/
6
Regular expression notations supported in Perl patterns. *|+*|+ Examples "aluminum" =~ /max|min/ "football" =~ /l*/ "football" =~ /z*$/ "football" =~ /^fo+/
7
Parentheses can be used to group parts of REs. Examples "soooo" =~ /^s(oo)*$/ "abaaba" =~ /^(aba)+$/ "12.24 311.42 " =~ /^((0|1|2|3|4)+\.(0|1|2|3|4)+ )+$/ Patterns are concatenated by position. "heebee jeebees" =~ /(ee)+(ae)*bee/
8
A few special symbols are permitted to match a single character from a set of characters. Examples "4-letter word" =~ /^\w..\S*\s\w+$/. \d \D \w \W \s \S You can build your own set by enclosing characters in brackets. "Ape" =~ /^[AEIOUY][b-r].$/
9
In addition to the standard RE repetition symbols (* and +) Perl supports additional repetition operations (all postfix). Examples "+73.111" =~ /^[+-]?[0-9]{1,}\.1{3}$/ ? { m } { m,n } { m, } $listing="-rw-r--r--@ 1 driley staff 49 Jan 6 15:34 z.txt"; $listing =~ /^.{15}(\w+)/
10
Parenthesized parts of patterns are automatically assigned to variables. 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”; Matched substrings are assigned left to right to $1, $2, and so forth. For multiple matches the last one is stored. if ("abeaceapteee" =~ /((a..)+(e*))/) { print $1,"\n"; print $2,"\n"; print $3,"\n"; }
11
Examples $str = "Now is the time for all good perls."; $str =~ s/\W*$//; #remove trailing non-alphanumeric while ( $str =~ s/\s*(\w*)$// ) { print $1,"\n"; } If a match occurs, then the matched pattern is replaced by string2. string =~ s/ pattern / string2 / $word= "zoo"; $word =~ s/o+/ebra/;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.