Perl Practical Extraction and Report Language Perl Developed

  • Slides: 20
Download presentation
Perl Practical Extraction and Report Language Perl: Developed by Larry Wall in the late

Perl Practical Extraction and Report Language Perl: Developed by Larry Wall in the late 1980 s. Builds on. . . sh awk sed c Perl is a fully general programming language, but it’s particularly well-adapted to processing “text”. CSE 341 -- S. Tanimoto Perl Introduction 1

Scripting Languages Scripting in Drama: programming the sequence of actor’s actions and utterances. A

Scripting Languages Scripting in Drama: programming the sequence of actor’s actions and utterances. A play is represented by a script. Scripting in software systems: programming a sequence of operations to be performed by a software system. Scripting operations are typically higher-level operations than machine instructions, typically application-oriented. Examples: Gnu Emacs is scripted with elisp. The GIMP (Gnu Image Manipulation Program) is scripted with a dialect of Scheme. Shell-level scripting language: Glue that ties together programs written in other languages. CSE 341 -- S. Tanimoto Perl Introduction 2

Is Scripting a Paradigm? No, not like functional programming or object-oriented programming. Scripting does

Is Scripting a Paradigm? No, not like functional programming or object-oriented programming. Scripting does not seem to require a different model for how computation works. Yet there are noticeable patterns that arise in scripts for particular types of applications. In CGI scripting for the web, many scripts begin by collecting input data, using it to access a database, and then they output a web page. But yes, we can view scripting as a paradigm, in the general sense of the word as a model or example of a way of programming. CSE 341 -- S. Tanimoto Perl Introduction 3

Perl and the Web One-pass compilation means Perl programs can be efficiently compiled on

Perl and the Web One-pass compilation means Perl programs can be efficiently compiled on demand. Perl makes it easy to process text (and generate HTML!). With contributed modules in the CPAN library, Perl has become a defacto standard for CGI programming on the Web, esp. on Unix servers. CSE 341 -- S. Tanimoto Perl Introduction 4

Perl vs Java Server vs Client. . . Input info may be too sensitive

Perl vs Java Server vs Client. . . Input info may be too sensitive to send over the net to an applet. Applet downloading and startup cause high latency. No special browser plugins are needed, since Perl doesn’t run in the browser. CSE 341 -- S. Tanimoto Perl Introduction 5

Perl has a “Little Language” philosophy Perl programs traditionally have been small. Perl itself,

Perl has a “Little Language” philosophy Perl programs traditionally have been small. Perl itself, though not small, is to some extent a successor of awk and sed. Small numbers of variables means less concern about name conflicts. . . declarations not required. Perl grows out of a systems-programming context. Conciseness was valued over transparency. The meanings of language features are often dependent on context (e. g. , list context vs scalar context). One system may consist of many Perl scripts. CSE 341 -- S. Tanimoto Perl Introduction 6

Comments on the Book by Sebesta A Little Book on Perl, by Robert W.

Comments on the Book by Sebesta A Little Book on Perl, by Robert W. Sebesta is a recent book (2000). It covers Perl 5. 005. (Perl 5. 8 has since come out, but Sebesta's book is generally consistent with it. ) The book is clearly written and covers the right stuff. Sebesta is the author of a popular text on programming languages. CSE 341 -- S. Tanimoto Perl Introduction 7

Running a Perl Program Perl was originally intended for use under Unix, but has

Running a Perl Program Perl was originally intended for use under Unix, but has since been ported to the PC. We’ll assume a Unix (Linux) environment here. #!/usr/bin/perl print("Out of the oyster!n"); # comment Make sure the file is executable: chmod +x howdy. pl Run the program from the command line: . /howdy. pl CSE 341 -- S. Tanimoto Perl Introduction 8

A Perl Script for the Web #!/usr/bin/perl print "Content-type: text/htmlnn"; $now = localtime(); print

A Perl Script for the Web #!/usr/bin/perl print "Content-type: text/htmlnn"; $now = localtime(); print "<html><body>"; print "<h 2>The time is $nown"; print " </h 2></body></html>"; CSE 341 -- S. Tanimoto Perl Introduction 9

Online help perldoc perldoc perlfaq perldata perlsyn perlop perlre perlrun -f split (overview) (freq.

Online help perldoc perldoc perlfaq perldata perlsyn perlop perlre perlrun -f split (overview) (freq. asked questions) (data structures) (syntax) (operators and precedence) (regular expressions) (execution and options) (info on a function) CSE 341 -- S. Tanimoto Perl Introduction 10

The Fundamental Data Types $n = 25; # a numeric scalar $str = "Here

The Fundamental Data Types $n = 25; # a numeric scalar $str = "Here is a string"; print('The value of $n is '. $n); # Note strings with ' ' do not have # variable "interpolation" performed # But strings with " " DO have it. print("The value is $n. "); CSE 341 -- S. Tanimoto Perl Introduction 11

String Operations "string 1". "string 2" # concatenation Can also be done using interpolation:

String Operations "string 1". "string 2" # concatenation Can also be done using interpolation: $str 1 = "abc"; $str 2 = "def"; $newstr = "$str 1$str 2"; CSE 341 -- S. Tanimoto Perl Introduction 12

Support for processing text: Immediate, embedded (“Here”) Documents print << "END-OF-PRINT"; Hello there. This

Support for processing text: Immediate, embedded (“Here”) Documents print << "END-OF-PRINT"; Hello there. This is a two-line immediate text doc. END-OF-PRINT CSE 341 -- S. Tanimoto Perl Introduction 13

Choice of Interpolating or Not $dollars = 64000; print <<”EOT”; The $dollars question. Are

Choice of Interpolating or Not $dollars = 64000; print <<”EOT”; The $dollars question. Are $dollars printed out here? EOT CSE 341 -- S. Tanimoto Perl Introduction 14

Choice of Interpolating or Not $dollars = 64000; print <<’EOT’; The $64000 question. Are

Choice of Interpolating or Not $dollars = 64000; print <<’EOT’; The $64000 question. Are $dollars printed out here? EOT CSE 341 -- S. Tanimoto Perl Introduction 15

Interpolation -- Not The $64000 question. Are $dollars printed out here? CSE 341 --

Interpolation -- Not The $64000 question. Are $dollars printed out here? CSE 341 -- S. Tanimoto Perl Introduction 16

Arrays In Perl, arrays are data structures that hold lists. @trees = ("Oak", "Maple",

Arrays In Perl, arrays are data structures that hold lists. @trees = ("Oak", "Maple", "Madrona"); @moretrees = qw(pine alter hemlock); @ages = (75, 50. 5, "twenty"); $mytree = $trees[1]; # "Maple" CSE 341 -- S. Tanimoto Perl Introduction 17

Hashes (Associative Arrays) $foodtype{"Spaghetti"} = "Italian"; $thetype = $foodtype{"Spaghetti"}; foreach $food (keys %foodtype) {

Hashes (Associative Arrays) $foodtype{"Spaghetti"} = "Italian"; $thetype = $foodtype{"Spaghetti"}; foreach $food (keys %foodtype) { $thetype = $foodtype{$food}; print "Food $food is $thetypen"; } CSE 341 -- S. Tanimoto Perl Introduction 18

Quick Example of CGI Processing: The HTML Form <html><body> <form method=post action="http: //www. domain.

Quick Example of CGI Processing: The HTML Form <html><body> <form method=post action="http: //www. domain. edu/do-something. pl"> Type your user name: <input type=text name=username size=20> <input type=submit value="Click here"> </form> </body></html> CSE 341 -- S. Tanimoto Perl Introduction 19

CGI Processing: The Perl Script #!/usr/bin/perl use CGI qw/: standard/; $the. Name = param("username");

CGI Processing: The Perl Script #!/usr/bin/perl use CGI qw/: standard/; $the. Name = param("username"); print "Content-type: text/htmlnn"; " <html><body><h 2>"; " Your user name is $the. Name"; "</h 2></body></html>"; CSE 341 -- S. Tanimoto Perl Introduction 20