DOC PREVIEW
Penn CIT 597 - Perl Lecture

This preview shows page 1-2-3-24-25-26-27-48-49-50 out of 50 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

PerlWhy Perl?Why not Perl?What is a scripting language?Major scripting languagesPerl Example 1Comments on “Hello, World”Perl Example 2More Perl notesPerl Example 3Comments on example 3Arithmetic in PerlString and assignment operatorsSingle and double quotesArrayspush and popforeachTestsfor loopswhile loopsdo..while and do..until loopsif statementsif - elsif statementsSlide 24Basic pattern matchingRE special charactersRE examplesSquare bracketsMore examplesMore special charactersQuoting special charactersAlternatives and parenthesesSubstitutionThe $_ variableGlobal substitutionsCase-insensitive substitutionsRemembering patternsDynamic matchingtrsplitAssociative arraysAssociative Arrays IIAssociative Arrays IIICalling subroutinesDefining subroutinesReturning a resultLocal variablesExample subroutinePerl 5The EndJan 13, 2019PerlMajor parts of this lecture adapted from http://www.scs.leeds.ac.uk/Perl/start.html2Why Perl?Perl is built around regular expressionsREs are good for string processingTherefore Perl is a good scripting languagePerl is especially popular for CGI scriptsPerl makes full use of the power of UNIXShort Perl programs can be very short“Perl is designed to make the easy jobs easy, without making the difficult jobs impossible.” -- Larry Wall, Programming Perl3Why not Perl?Perl is very UNIX-orientedPerl is available on other platforms......but isn’t always fully implemented thereHowever, Perl is often the best way to get some UNIX capabilities on less capable platformsPerl does not scale well to large programsWeak subroutines, heavy use of global variablesPerl’s syntax is not particularly appealing4What is a scripting language?Operating systems can do many thingscopy, move, create, delete, compare filesexecute programs, including compilersschedule activities, monitor processes, etc.A command-line interface gives you access to these functions, but only one at a timeA scripting language is a “wrapper” language that integrates OS functions5Major scripting languagesUNIX has sh, PerlMacintosh has AppleScript, FrontierWindows has no major scripting languagesprobably due to the weaknesses of DOSGeneric scripting languages include:Perl (most popular)Tcl (easiest for beginners)Python (new, Java-like, best for large programs)6Perl Example 1#!/usr/local/bin/perl## Program to do the obvious#print 'Hello world.'; # Print a message7Comments on “Hello, World”Comments are # to end of lineBut the first line, #!/usr/local/bin/perl, tells where to find the Perl compiler on your systemPerl statements end with semicolonsPerl is case-sensitivePerl is compiled and run in a single operation8Perl Example 2#!/ex2/usr/bin/perl# Remove blank lines from a file# Usage: singlespace < oldfile > newfilewhile ($line = <STDIN>) { if ($line eq "\n") { next; } print "$line";}9More Perl notesOn the UNIX command line;< filename means to get input from this file> filename means to send output to this fileIn Perl, <STDIN> is the input file, <STDOUT> is the output fileScalar variables start with $Scalar variables hold strings or numbers, and they are interchangeableExamples:$priority = 9;$priority = '9';Array variables start with @10Perl Example 3#!/usr/local/bin/perl# Usage: fixm <filenames># Replace \r with \n -- replaces input filesforeach $file (@ARGV) { print "Processing $file\n"; if (-e "fixm_temp") { die "*** File fixm_temp already exists!\n"; } if (! -e $file) { die "*** No such file: $file!\n"; } open DOIT, "| tr \'\\015' \'\\012' < $file > fixm_temp" or die "*** Can't: tr '\015' '\012' < $infile > $outfile\n"; close DOIT; open DOIT, "| mv -f fixm_temp $file"or die "*** Can't: mv -f fixm_temp $file\n"; close DOIT;}11Comments on example 3In # Usage: fixm <filenames>, the angle brackets just mean to supply a list of file names hereIn UNIX text editors, the \r (carriage return) character usually shows up as ^M (hence the name fixm_temp)The UNIX command tr '\015' '\012' replaces all \015 characters (\r) with \012 (\n) charactersThe format of the open and close commands is:open fileHandle , fileNameclose fileHa ndle , fileName "| tr \'\\015' \'\\012' < $file > fixm_temp" says: Take input from $file, pipe it to the tr command, put the output on fixm_temp12Arithmetic in Perl$a = 1 + 2; # Add 1 and 2 and store in $a$a = 3 - 4; # Subtract 4 from 3 and store in $a$a = 5 * 6; # Multiply 5 and 6$a = 7 / 8; # Divide 7 by 8 to give 0.875$a = 9 ** 10; # Nine to the power of 10, that is, 910$a = 5 % 2; # Remainder of 5 divided by 2++$a; # Increment $a and then return it$a++; # Return $a and then increment it--$a; # Decrement $a and then return it$a--; # Return $a and then decrement it13String and assignment operators$a = $b . $c; # Concatenate $b and $c$a = $b x $c; # $b repeated $c times$a = $b; # Assign $b to $a$a += $b; # Add $b to $a$a -= $b; # Subtract $b from $a$a .= $b; # Append $b onto $a14Single and double quotes$a = 'apples';$b = 'bananas';print $a . ' and ' . $b;prints: apples and bananasprint '$a and $b';prints: $a and $bprint "$a and $b";prints: apples and bananas15Arrays@food = ("apples", "bananas", "cherries");But… print $food[1];prints "bananas" @morefood = ("meat", @food);@morefood == ("meat", "apples", "bananas", "cherries");($a, $b, $c) = (5, 10, 20);16push and poppush adds one or more things to the end of a listpush (@food, "eggs", "bread");push returns the new length of the listpop removes and returns the last element$sandwich = pop(@food);$len = @food; # $len gets length of @food$#food # returns index of last element17foreach# Visit each item in turn and call it $morselforeach $morsel (@food){ print "$morsel\n"; print "Yum yum\n"; }18Tests“Zero” is false. This includes:0, '0', "0", '', ""Anything not false is trueUse == and != for numbers, eq and ne for strings&&, ||, and ! are and, or, and not, respectively.19for loopsfor loops are just as in C or Javafor ($i = 0; $i < 10; ++$i){ print "$i\n";}20while loops#!/usr/local/bin/perlprint "Password? ";$a = <STDIN>;chop $a; # Remove the last character (\n)while ($a ne "fred"){ print "sorry. Again? "; $a =


View Full Document

Penn CIT 597 - Perl Lecture

Documents in this Course
DOM

DOM

21 pages

More DOM

More DOM

11 pages

Rails

Rails

33 pages

DOM

DOM

21 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

Rake

Rake

12 pages

Ruby

Ruby

58 pages

DOM

DOM

21 pages

Tomcat

Tomcat

16 pages

DOM

DOM

21 pages

Servlets

Servlets

29 pages

Logging

Logging

17 pages

Html

Html

27 pages

DOM

DOM

22 pages

RELAX NG

RELAX NG

30 pages

Servlets

Servlets

28 pages

XHTML

XHTML

13 pages

DOM

DOM

21 pages

DOM

DOM

21 pages

Servlets

Servlets

26 pages

More CSS

More CSS

18 pages

Servlets

Servlets

29 pages

Logging

Logging

17 pages

Load more
Download Perl Lecture
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Perl Lecture and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Perl Lecture 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?