perl created in 1987 by larry wall. perl is open source probably best known as a cgiscripting...

11
Perl created in 1987 by Larry Wall. Perl is open source robably 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

Upload: bertina-glenn

Post on 02-Jan-2016

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 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.”

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

Referenceswww.perl.org

www.perltutorial.orgwww.comp.leeds.ac.uk/Perl/start.html

www.troubleshooters.com/codecorn/littperl/perlreg.htm

Page 2: 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.”

# 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);

Page 3: 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.”

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;

Page 4: 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.”

Perl supports two relational operators for pattern matching.

string =~ /pattern/ string !~ /pattern/

Exampleif ("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).

Page 5: 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.”

Anchor symbols for use in patterns.

^ $

Examples"itinerary" =~ /^in/

"itinerary" =~ /^it/

"itinerary" =~ /tin/

"itinerary" =~ /tin$/

"itinerary" =~ /ary$/

"itinerary" =~ /^ary$/

"abc" =~ /^abc$/

Page 6: 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.”

Regular expression notations supported in Perl patterns.

*| +

Examples

"aluminum" =~ /max|min/

"aluminum" =~ /max | min/

"football" =~ /l*/

"football" =~ /z*$/

"football" =~ /^fo+/

Page 7: 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.”

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/

Page 8: 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 few special symbols are permitted to match a single character froma 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].$/

Page 9: 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.”

In addition to the standard RE repetition symbols (* and +) Perlsupports 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+)/

Page 10: 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.”

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";

}

Page 11: 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.”

Examples

$str = "Now is the time for all good perls."; $str =~ s/\W*$//; #remove trailing non-alphanumericwhile ( $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/;