1 / 6

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.

jael
Download Presentation

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

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

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