1 / 48

Introduction to Perl

Introduction to Perl. Lecturer: Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954. “UNIX for Programmers and Users” Third Edition, Prentice-Hall, GRAHAM GLASS, KING ABLES Slides partially adapted from Kumoh National University of Technology (Korea) and NYU.

betty_james
Download Presentation

Introduction to 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. Introduction to Perl Lecturer: Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954 “UNIX for Programmers and Users” Third Edition, Prentice-Hall, GRAHAM GLASS, KING ABLES Slides partially adapted from Kumoh National University of Technology (Korea) and NYU

  2. What is Perl? • Practical Extraction and Report Language • Written in 1986 by Larry Wall • Influenced from awk, sed, and C Shell • Similar to ksh93, python, tcl, javascript • Widely used for CGI scripting • Freely available Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  3. Running Perl in UNIX • Explicit invocation of a program $ perl [options] program [arguments] • Using the #! Directive in program $ program [arguments] • Command line expression $ perl –e ’print ”hello\n”;’ • Debugging mode (opens a CLI debugger - similar to gdb, dbx, jdb, etc.) $ perl -d program [args] Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  4. Perl Example #!/usr/bin/perl -w # THIS IS A COMMENT: -w issues warnings print “What is your favorite color? “; $color = <STDIN>; chomp($color); if ($color eq ‘blue’) { print “That is my favorite! \n”; } elsif ($color =~ /black/) { print “ I do like $color for my shoes\n”; } else { print “$color is a nice choice \n”; } Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  5. Data Types • Basic data types: scalars, indexed arrays of scalars (lists), and associative arrays. • Scalars are strings or numbers depending on context. • Type of variable determined by special leading character: $ scalar @ indexed array % associative array & function • All data types have separate name spaces Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  6. Data Types Examples • Scalars: $str = “The world is round” ; # string variable $num = 134.99; # numeric variable • Arrays @students =(“Mike”, “Lisa”, “John”); print $students[0]; # print Mike $students[3]= “Julie”; # add a new element $size = @students; # size of the array (== 4) @students = (); # empty array • Hashes %emp =(“Julie”, “President”, “Mary”, “VP”); print $emp {“Julie”}; # print “President” $emp{“John”} = “controller”; # add an element Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  7. Special Scalar Variables Important special variable: $_ default pattern for operators Plenty fo other variable: $0 name of the currently running script $$ the current PID $? status of last pipe or system call $. the current line number of last input $] the version of perl being used $< the real uid of the process $> the effective uid of the process Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  8. Special Array Variables @ARGV command line arguments @INC search path for modules @_ default for split and subroutine parameters %ENV current environment %SIG used to set signal handlers sub trapped { print STDERR “Interrupted!\n”; exit 1;}$SIG{’INT’} = ’trapped’; Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  9. Operators • Perl uses all of C’s operators (except type casting and pointer operators), and adds: • exponentiation:**, **= • range operator:.. @new = @old[30..50]; print 1 .. 9; • string concatenation:. , .= $x = $y . &frob(@list) . $z; $x .= ”\n”; • string repetitor operator:x “fred” x 3 # is fredfredfred Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  10. Numeric and String comparison operators Comparison Numeric String equal==eq not equal!=ne less than<lt greater than>gt less than or equal to<=le greater than or equal to>=ge Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  11. Flow Control • Unlike C, blocks always require braces • unless and until are just if and while negated if (EXPR) BLOCK else BLOCK if (EXPR) BLOCK elsif (EXPR) BLOCK else BLOCK while (EXPR) BLOCK do BLOCK while EXPR for (EXPR; EXPR; EXPR) BLOCK foreach $VAR (LIST) BLOCK • For readability, if, unless, while and until may be used as trailing statement modifiers: return –1 unless $x > 0; &myfunction or die • Uses next and last rather than C’s continue and break. Also contains redo to repeat loop iteration Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  12. Flow Control Examples # print an array for ($i = 0; $i < @myarray; $i++) { # @myarray is the number of elements print “$myarray[$i]\n”;} # print all element of an array until the one that is negative foreach $element (@myarray) { last if $element < 0; print “$element\n”;} # check if password file exists if (-f /etc/passwd) { print “file exists\n”} Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  13. Flow Control Examples # reverse foo $n = @foo; for ($i=0; $i < $n/2; $i++) { $temp = $foo[$i]; $foo[$i] = $foo[$n-1-$i]; $foo[$n-1-$i] = $temp; } # find power of 2 larger than a number print "Enter a number: "; $num = <STDIN>; $power = 1; $power *=2 until ($power >= $num); print "next power of 2 is $power\n"; Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  14. Another Flow Control Example # ask for a non-empty name { print("What is your name? "); $name = <STDIN>; chop($name); if (! length($name)) { print("Please try again\n"); redo; } print("Thank you, " . uc($name) . "\n"); } do { print("What is your name? "); $name = <STDIN>; chomp($name); if (! length($name)) { print("Please try again\n"); } } until (length($name)); Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  15. Regular Expressions • Understands egrep-style EREs, plus: \w, \W alphanumerics and _ \d, \D digits \s, \S whitespace \b, \B word boundaries \t tab • Special variables: $& means all text matched, $` (back quote) is text before match, $’ (single quote)text after • Use \1 .. \9 within EREs, $1 .. $9 outside if (/^this (red|blue|green) (bat|ball) is \1/) { ($color, $object) = ($1, $2); } Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  16. Regular Expressions Substitute and translation operators are like sed: s/alpha/beta/; # alpha --> beta in special variable $_ s/(.)\1/$1/g; # (blah) --> blah in special variable $_ y/A-Z/a-z/; # upper case --> lower case # in special variable $_ • Use =~ and !~ to match against variables $var1 =~ s/pattern/replacement/; # substitute in $var1 if ($var =~ /pattern/) {...} # if $var has the pattern if (<STDIN> =~ /^[xX]/) { ... } # if STDIN starts with X or x if ($var !~ /pattern/) { ... } # if $var does not have a pattern Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  17. The Variable $_ • $_ is used a lot in Perl • When variable is left out, $_ is used: • Example: foreach (@list) {... $_ ...} # points to “each” s/good/bad/; # in whatever $_ is if (/pattern/) {…} # if $_ matches “pattern” if (!/pattern/) {…} # if $_ does not match “pattern” Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  18. I/O • Filehandles conventionally uppercase: STDIN, STDOUT, STDERR • Mentioning a filehandle in angle brackets (<…>) reads next line: • scalar context: returns line (with newline) $line = <TEMP>; • array context: returns all lines (with newlines) @lines = <TEMP>; Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  19. I/O continued • When used in a while construct, input lines are automatically assigned to the $_ variable. • Typical usage: iterate over file one line at a time, using $_ while ( <> ) { next if /^#/; # continue - skip comments == if line starts with # last if /STOP/; # break - stop if a line with “STOP” read s/left/right/g; # global substitute: left --> right print; # print $_ } • <> means all files supplied on command line (or STDIN if none) put together Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  20. Files • Opening a file open(PWD, ”/etc/passwd”); open(TMP, ”>/tmp/foobar.$$”); open(LOG, ”>>logfile”); open(TOPIPE, ”| lpr”); open(FROMPIPE, ”ps –ef |”); open(PWD, ”/etc/passwd”) or die ”cannot open”; • Printing: print LOG ”This is a log entry\n”; printf LOG ”entry: %s\n”, $var • awk influence $, field separator (column delimiter) $/ record separator (line delimiter) Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  21. I/O Example # Read an array from a file $buffer = ""; open(FILE, "<binary.dat"); read(FILE, $buffer, 20, 0); close(FILE); # print the value of all array elements in hexadecimal format. foreach (split(//, $buffer)) { # split into characters printf("%02x ", ord($_)); print "\n" if $_ eq "\n"; } Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  22. Globbing • Perl has globbing, similar to shell • A string in angle brackets with metacharacters evaluates to a list of matching filenames # change all *.c and *.h files to *.c.old and *.h.old foreach $x ( <*.[ch]> ) { rename($x, “$x.old”); } chmod 711, <*.c>; Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  23. Subroutines • Defined with sub: sub myfunc { ... } • Subroutines calls are prefaced with &. Any of the three principal data types may be passed as parameters or used as a return value; e.g., &foo; • Special variable $_ passed by default • Parameters are received by the subroutines in the special array @_: $_[0], $_[1], ... • Local variables can be declared with my • By default, $_ returned. Override with return statement Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  24. Subroutine Examples &foo; # call subroutine foo with $_ &foo(@list); # call foo passing an array $x = &foo(‘red’, 3, @arr); # foo returns a value that is assigned to x @list = &foo; # foo returns a list sub simple { my $sum; # local variable foreach $_ (@_) { # @_ is the list of parameters $sum += $_; } return $sum; } $foo = ‘myroutine’; &$foo(@list); # call subroutine indirectly Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  25. Array Functions • push and pop functions: • when arrays are used a stack of information, where new values are added to and removed from the right hand side of the list. • Example: push(@mylist, $newvalue); # like: @mylist = (@mylist, $newvalue); $oldvalue = pop(@mylist); # removes last element of my list. Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  26. Array Functions (cont.) • shift and unshift functions: • performs similar action to push and pop on the "left" side of a list. • Example: @fred = (5, 6, 7); unshift(@fred, $a) ; # like @fred = ($a, @fred); $x = shift(@fred); # x gets 5, @fred is now (6, 7) • reverse function: • reverses the order of the elements of its argument, returning the resulting list @a = (1, 2, 3); @b = reverse(@a); # means @b = (3, 2, 1); Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  27. Array Functions (cont.) • sort function: • returns a sorted list in ascending ASCII order without changing original list • grep function: • returns a new list consisting of all the elements which match the given expression @lines = grep(!/^#/, @lines); Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  28. Array functions (cont.) • split • breaks up a string into an array of new strings. You can split on arbitrary regular expressions: @list = split(/[, \t]+/, $expr); # use any number of “,”, “ “ or “\t” to separate while (<PASSWD>) { # use “:” to split lines in the PASWD file ($login, $passwd, $uid, $gid, $gcos, $home, $shell) = split(/:/); } • The inverse of split is join $line = join(‘:’, $login, $passwd, $uid, $gid, $gcos, $home, $shell); • pattern “//” will split into single characters Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  29. Directory Access • Method 1: • Open a pipe from /bin/ls: open(FILES, “/bin/ls *.c |”); • Method 2: • The directory-reading routines are provided as built-ins and operate on directory handles. Supported routines are opendir, readdir, closedir, etc. opendir(DIR, "."); @files = sort(grep(/pl$/, readdir(DIR))); # extract only those that end in pl closedir(DIR); foreach (@files) { print("$_\n") unless -d; # print name unless the file is a directory } Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  30. Hash Table Functions • For manipulating associative (hash) arrays, the following functions are useful: • keys: returns indexed array of keys • values: returns indexed array of values • each: returns indexed array of two-element arrays containing ($key,$value) pairs. while (($key, $value) = each %array) { printf “%s is %s\n”, $key, $value;} foreach $key (keys %array) { printf “%s is %s\n”, $key, $array{$key};} print reverse sort values %array; Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  31. String functions • Several C string manipulation functions: • crypt, index, rindex, length, substr, sprintf • Adds others: • chop: removes the last character from a string (very useful for removing \n character) • chomp: removes last character only if \n • work with scalars or arrays chop($line) while (<STDIN>) { chomp; ... } @stuff = (“hello\n”, “hi\n”, “ola\n”); chomp(@stuff); Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  32. Example Program #!/usr/bin/perl # Print out today’s yearday on stdout ($sec,$min,$hour,$day,$mon,$year,@rest) = gmtime(); @months = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); if ($year == 2000) { # A leap year $months[1] = 29; } $yearday = 0; for ($m=0; $m < $mon; $m++) { $yearday += $months[$m]; } $yearday += $day; print $yearday . “\n”; Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  33. awk and sed Connection • a2p • Translates an awk program to Perl • s2p • Translates a sed script to Perl Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  34. Perl can get messy… Write your own munitions… #!/bin/perl -s-- -export-a-crypto-system-sig -RSA-3-lines-PERL $m=unpack(H.$w,$m."\0"x$w),$_=`echo "16do$w 2+4Oi0$d*-^1[d2%Sa 2/d0<X+d*La1=z\U$n%0]SX$k"[$m*]\EszlXx++p|dc`,s/^.|\W//g,print pack('H*',$_)while read(STDIN,$m,($w=2*$d-1+length$n&~1)/2) Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  35. Packages (Modules) • A collection of functions • Usually stored in a file called MyModule.pm • Each has its own namespace • Access with: $MyModule::Variable • Default package is $main • To use a package: use MyModule; • To create a module: • Start off with: package MyModule; • Definitions of subroutines and variables • Export routines/variables to global namespace • Indicate success with line: 1; #last statement in file • Many available packages: • e.g., CGI Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  36. Web Browser Web Browser html Perl as a CGI scripting language Simple file serving • CGI - Common Gateway Interface • allows for embedding actions on Web pages • Java Servlet - same idea, but needs a Java Application Server (J2EE) • Microsoft has .Net Similarly, .txt, .jpg, .pdf, etc. Web Browser Web Server html CGI Engine cgi e.g., Perl program e.g., Perl interpreter Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  37. cgi.pm • Perl module for CGI parsing (Perl 5 and newer) • Also includes utility functions for generating HTML, debugging, etc. $ cat birthday.cgi #!/usr/bin/perl use CGI; $query = CGI::new(); $bday = $query->param("birthday"); print "Content-type: text/html\n\n"; print "Your birthday is $bday <br>\n"; $ chmod 755 birthday.cgi $ birthday.cgi birthday=11/5/02 # pass the parameters Content-type: text/html Your birthday is 11/5/02 Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  38. CGI Script: Example Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  39. Part 1: HTML Form <html> <center> <H1>Anonymous Comment Submission</H1> </center> Please enter your comment below which will be sent anonymously to me. <p> <form action=cgi-bin/comment.cgi method=post> <textarea name=comment rows=20 cols=80> </textarea> <input type=submit value="Submit Comment"> </form> </html> Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  40. Part 2: CGI Script (Perl) #!/usr/bin/perl use CGI; $query = new CGI; $comment = $query->param('comment'); $recipient = 'andrzej@csun.edu'; $sendmail = '/usr/sbin/sendmail'; open(MAIL, "|$sendmail -oi -t") or die "Can't open pipe to $sendmail: $!\n"; print MAIL "To: $recipient\n"; print MAIL "Subject: Sample Web Form Submission\n\n"; print MAIL "$mail_body"; close(MAIL) or die "Can't close pipe to $sendmail: $!\n"; Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  41. Part 2: CGI Script (Perl) print "Content-type: text/html\n\n"; print <<"EOF"; # print everything literally until the text <HTML> <HEAD> <TITLE>Thank you</TITLE> </HEAD> <BODY> <H1>You submitted the following comment:</H1> <pre> EOF # end of the first print print $comment . "\n"; print <<"EOF"; </pre> <H1>Thank you</H1> <P>Thank you for your form submission. </BODY> </HTML> EOF Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  42. CGI environment Login to HP servers and do the following: $ mkdir public_html $ cd public_html $ vi page.html # enter the HTML file $ chmod 644 page.html $ mkdir cgi-bin $ cd cgi-bin $ vi script.cgi # enter the CGI script (Perl program) $ chmod 755 script.cgi Then open a browser and go to http://www.csun.edu/~yourname/page.html Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  43. Another Example #!/usr/bin/perl # Dump parameters and environment variables use CGI; $query = CGI::new; print &CGI::header; print "<H1> Date </H1>\n"; print "<pre>\n"; print `date\n`; print "</pre>\n"; print "<H1> Form Variables </H1>\n"; print "<pre>\n"; foreach $p (sort(($query->param))) { print "$p = " . $query->param($p) . "\n"; } print "</pre>\n"; print "<H1> Environment </H1>\n"; print "<pre>\n"; foreach $p (sort((keys ENV))) { print "$p=" . $ENV{$p} . "\n"; } print "</pre>\n"; Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  44. Another Example with Database Access <html> <head> <title> User Database Access Test - perl </title> </head> <body> <h1> User Database Access Test - perl </h1> <p> <form action="http://www.some.com/userData.cgi" method=post> A simple example to find information about a user data. <br> Enter your user ID name <input type=text name=“ID”> <br> <input type=submit value="Search database"> </form> <p> </body> </html> Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  45. Another Example with Database Access #!/usr/local/bin/perl use CGI; # DBI is a module with a database connector use DBI; $query = new CGI; # a debugging option that sends error messages to the browser rather # than log file use CGI::Carp qw(fatalsToBrowser); # print page header print "Content-type: text/html\n\n"; print "<html><head><title>Location query</title></head>"; print "<body><h1>Location query</h1><p>"; # connect to the database “userDatabase” with ID “demo” and password “” $dbConnector = DBI->connect("dbi:mysql:userDatabase","demo","") or die("Couldn't connect"); # import names from the query page into a namespace ‘MySpace’ $query->import_names(’MySpace'); # prepare a database query $sqlTable = $dbConnector->prepare("select * from placeDatabase where feature = ?") or die("Couldn't prepare"); Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  46. Another Example with Database Access # execute the query replacing ‘?’ with $MySpace::ID $sqlTable->execute($MySpace::ID); # are there any rows in the table? if($sqlTable->rows == 0) { print "No information for " . $MySpace::ID; } else { print "<table border=2>\n"; # get the row and put it into an associative array (a hash) while( $rowHash = $sqlTable->fetchrow_hashref() ) { print "<tr>"; print "<td>" . $rowHash->{”name”}; print "<td>" . $rowHash->{”address"}; print "<td>" . $rowHash- >{”telephone"}; print "<td>" . $rowHash- >{”email"}; print "\n"; } print "</table>\n"; } print "</body></html>\n"; # disconnect from the database $dbConnector->disconnect; Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

  47. Perl Challenge

  48. Homework/Project • Read the CGI/Perl tutorials (see my links) - start with the “beginners” • Implement the following Web-based pizza ordering system: • You should have forms for: • delivery address • some selection for pizza (ingredients, style, etc.) • credit card information and • email address for confirmation. • You do not need to store the data. • You do not need to verify and charge credit card. • The data collected from the form should be sent to your email address. • A confirmation with order details should be displayed on the new page. • You’ve got 3 weeks! I need the link to try out your program and your HTML file and CGI script. Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954

More Related