590 likes | 624 Views
perl (for the rest of us) Tools For Lunch. S. Strout (sps@cs.rit.edu). Larry Wall. "Eventually the revolutionaries become the established culture, and then what will they do?" --Linus Torvalds
E N D
perl (for the rest of us)Tools For Lunch S. Strout (sps@cs.rit.edu) Perl - Tools For Lunch (v1.00)
Larry Wall "Eventually the revolutionaries become the established culture, and then what will they do?" --Linus Torvalds • The Practical Extraction and Report Language was unleashed to the world by Larry Wall in 1987 Perl - Tools For Lunch (v1.00)
What is Perl? • Perl was originally developed for a Usenet-like bug reporting system Perl - Tools For Lunch (v1.00)
Perl’s Mascot – The Camel Perl is… • easy • nearly unlimited • mostly fast • ugly (but so are camels too) Perl - Tools For Lunch (v1.00)
Is Perl Easy or Hard? • You be the judge: while (<>) { chomp; print join(“\t”, (split /:/)[0, 2, 1, 5] ), “\n”; } • The language sides with the programmer versus the student because… “You only get to learn Perl once, but you use it again and again!” Perl - Tools For Lunch (v1.00)
Is Perl Easy or Hard? • “Perl looks like line-noise to the uninitiated, but to the seasoned programmer, it looks like check-summed line-noise with a mission in life” @P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{ @p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord ($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&& close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print Obfuscated Perl: http://perl.plover.com/obfuscated/ Perl - Tools For Lunch (v1.00)
The Good and the Bad • What is Perl good for? • Perl is ideal for problems working with 90% text • Perl is married to the web • Allows rapid movement of documents to HTML format • It is the darling language for CGI scripts run on a web server • What is Perl not good for? • Never say can’t with Perl because someone will try to prove you wrong. • Real-time systems programming or user interface design is better suited to C or Java • Representing complex data structures in Perl can be a syntactic nightmare Perl - Tools For Lunch (v1.00)
ActiveState Perl Haiku Contest – Entries About Perl Grand Prize Winner (Ed Snoeck) ugliness that grows into beauty inside of your favorite shell Dishonorable Mention (Geoff Kuenning) Unreadable code, Why would anyone use it? Learn a better way. Perl and the Internet (Zachary McArtor) Perly white syntax A linguistic masterpiece duct tape of the net We Won’t Go There… (Abuzar Chaudhary) Outdoor spring orgy Mating with queer fierce freedom Parrots and Gnu beasts Perl - Tools For Lunch (v1.00)
ActiveState Perl Haiku Contest – Entries *in* Perl Grand Prize Winner (James Tilley) no less can I say; require strict, close attention while you ... write haiku Language Wars (Jasvir Nagra) y? use Lisp? or C? ; use less keystrokes, B::Concise; bless Wall for our @perl True Love (Duane Bronson) open (your_heart,"<now"); push (my @perl_into,<your_heart>); close(your_heart) and die; The Perl Student (Joe Murray) open my $camel_book && (seek (good, advice, ()) || die "trying"); while (my $sanity) { last S; }; Perl - Tools For Lunch (v1.00)
Perl Resources • Main site: http://www.perl.org • The perl interpreter is compiled in C • The Source code is open and binaries exist for most platforms • Usenet: comp.lang.perl • Documentation: http://www.perldoc.com • Best book for Perl newbies • Title: Learning Perl (3E) • Authors: Schwartz, Phoenix • Paperback: 330 pages • Publisher: O'Reilly; 3rd edition (July 15, 2001) • ISBN: 0596001320 Perl - Tools For Lunch (v1.00)
Learning Perl • We’ll look at the following basic constructs in Perl • Scalars: $ • Lists and Arrays: @ • Hashes: % • Subroutines: & • I/O: <> Perl - Tools For Lunch (v1.00)
Scalars • A scalar means “one of something” • Singular vs. plural • Some example scalar literals: • Number: 255, 1.25e10, 255.0, 61598, 61_598 • String: hello, c, hello\n • Two important rules • Numbers and strings are automatically interchangeable • Perl computes everything as double-precision floating point values • There are no integers! • 10 / 3 # 3.333 • 10.5 % 3.2 # 1 Perl - Tools For Lunch (v1.00)
Strings • Strings are a sequence of zero or more characters • Shortest string is the empty string • Largest string fills all available memory • “Know thy ASCII table” \75\110\111\119\32\116\104\121\32\65\83\67\73\73\32\116\97\98\108\101 • Perl can handle non printable characters in a string (values outside ASCII 32-126) • This is perfect for manipulating raw binary data like a JPEG or a compiled program Perl - Tools For Lunch (v1.00)
Single Quoted Strings • A sequence of characters enclosed in a single quote • Any character besides a single quote, ’ , and a backslash, \ , stands for itself ’fred’ # the four characters: f, r, e, d ’’ # the null string ’Don\’t let an apostrophe end this string!’ ’The last char is a backslash: \\’ ’hello\n’ # hello, backslash, n ’hello there’ # hello, \n, there (11 chars) ’\’\\’ # What is this? Perl - Tools For Lunch (v1.00)
Double Quoted Strings • A sequence of characters enclosed in double quotes • Backslash now takes on the full power to specify a control character, or any character “barney” # same as ‘barney’ “hello world\n” # hello world, and a newline “The last character is a quote: \”” “coke\tsprite” # coke, a tab, and a sprite Perl - Tools For Lunch (v1.00)
String Operations • Strings values can be concatenated with the . operator • Neither string is modified, a new result is formed “hello” . “world” # same as “helloworld” “hello” . ’ ’ . “world” # same as “hello world” ’hello world!’ . “\n” # same as “hello world!\n” • The string repetition operator, x “fred” x 3 # is “fredfredfred” “barney” x (1+1) # “barneybarney” 5 x 4 # What is this??? 5 x “4” # What is this??? Perl - Tools For Lunch (v1.00)
Automatic Conversion • Perl automatically converts numbers strings based on the operators “12” * “3” “12fred34” * “ 3” “fred” * 10 “Z” . 5 * 7 Perl - Tools For Lunch (v1.00)
Scalar Variables • A scalar variable contains a modifiable scalar value $myScalar = 10; • Scalar assignment gives a value to a scalar variable $fred = 17 # $fred stores 17 $barney = ’hello’ # $barney stores ’hello’ $barney = $fred + 3 # $barney now stores 20 $barney = fred + 65 # What happens here? $bbarney = $fredd + 65 # What happens here? Perl - Tools For Lunch (v1.00)
Scalar/String Interpolation • The print function dumps output to standard output (default) • Double quoted string literals are subject to variable interpolation $meal = “brontosaurus steak”; $barney = “fred ate a $meal” $barney = ’fred ate a ’ . $meal; $barney = “fred ate a $meat”; # What happens here? print “$fred”; # unneeded quote marks print $fred; # the preferred style $fred = ’hello’; print “The name is \$fred.\n”; # What happens here? Perl - Tools For Lunch (v1.00)
Scalar/String Interpolation • Perl provides a delimiter for the variable name. Enclose the name of the variable in curly braces $what = “brontosaurus steak”; $n = 3; print “fred ate $n $whats.\n”; # What happens? print “fred ate $n $what” . “s.\n”; # cumbersome print “fred ate $n ${what}s.\n”; # easy easy Perl - Tools For Lunch (v1.00)
Comparison Operators • The logical comparison operators returns true or false ComparisonNumericString Equal == eq Not equal != ne Less than < lt Greater than > gt Less than or equal <= le Greater than or equal to >= ge 35 != 30 + 5 # false 35 == 35.0 # true ’35’ eq ’35.0’ # false (why?) ’fred’ < ’barney’ # false ’fred’ < ’free’ # false (not lexicographical) ’fred’ eq “fred” # true ’fred’ eq “Fred” # false ‘ ‘ gt ‘‘ # true Perl - Tools For Lunch (v1.00)
The if Control Structure • Curly braces are required around all conditionals if ($name gt ’fred’) { print “’$name’ > ’fred’.\n”; } else { print “’$name’ <= ’fred’.\n”; } • The conditional can come after the statement and the parentheses can be omitted print “’$name’ > ’fred’.\n” if $name gt ’fred’; Perl - Tools For Lunch (v1.00)
Boolean Values • Perl has no separate boolean type. The rules: • The special value undef is false • 0 is false, all other numbers are true • “” and “0” is false, all other strings are normally true • Any string that evaluates to 0 is false “0” “0.00” Perl - Tools For Lunch (v1.00)
undef • Variables have a special undef value before they are given a value • As a numeric, undef is treated as 0, and as a string it is treated as the empty string • undef is neither a number nor a string, it’s an entirely separate scalar value $n = 1; while ($n < 10) { $sum += $n; # using undef as 0 $n += 2; } print “The total was $sum.\n”; $string .= “more text\n”; # using undef as “” Perl - Tools For Lunch (v1.00)
defined • To tell whether a value is undef and not the empty string, use the defined function, which returns false for undef and true for everything else if ( defined($madonna) ) { print “Madonna = $madonna”; } • To force a variable to be undefined: $madonna = undef; # as if it had never been touched Perl - Tools For Lunch (v1.00)
Values 0 35 1 12.4 2 ’hello’ Indices 1.72e30 3 4 “bye\n” Lists and Arrays • Plurals are represented by lists and arrays • A list is an ordered collection of scalars (i.e. the data) • An array is a variable that contains a list • A list can hold any mixture of scalar values Perl - Tools For Lunch (v1.00)
Accessing Array Elements • Array elements begin at zero and increase by one $fred[0] = “yabba”; $fred[1] = “dabba”; $fred[2] = “doo”; • Arrays use a different namespace than scalars, so variables can have the same names: $fred = 65; print “scalar fred=$fred\n”; print “array fred[0]=$fred[0]”; Perl - Tools For Lunch (v1.00)
Accessing Array Elements • The subscript is truncated to an integer $fred[“wilma”] ; // $fred[0], or “yabba” • Accessing an element beyond the end of an array yields an undef value (same behavior as scalars) $blank = $fred[ 142_897 ]; # $blank = undef $darl = $mcbride; # most certainly undef • Arrays are automatically extended as needed Perl - Tools For Lunch (v1.00)
Special Array Indices $rocks[0] = ’bedrock’; $rocks[1] = ’slate’; $rocks[2] = ’lava’; $rocks[3] = ’crushed rock’; $rocks[99] = ’schist’; # 95 undef elements • The last element index is $#array_name $rocks = $#rocks; # 99 $num_rocks = $rocks + 1 # ok but not ideal $#rocks = 2 # forget all rocks after lava $#rocks = 99 # add 97 new undef elements $rocks[ $#rocks ] = ’hard rock’ # add this rock at end • Negative indices wrap around once $rocks[ -1 ] = ’hard rock’ # easier way $dead_rock = $rocks[ -100 ]; # gets ’bedrock’ $rocks[ -200 ] = ’crystal’ # undef Perl - Tools For Lunch (v1.00)
List Literals • A list is represented in your program as a list of comma-separated values enclosed in parentheses (1, 2, 3) # list of three values, 1, 2 and 3 (1, 2, 3,) # trailing comma is ignored (“fred”, 4.5) # two values, “fred” and 4.5 () # empty list – zero elements • The range operator creates a series of scalars (1..100) # list of 100 ints (5..1) # empty list – only counts “uphill” (0, 2..6, 10) # 0, 2, 3, 4, 5, 6, 10 ($a..$b) # range determined by scalar vals (0..$#rocks) # the indices of the $rocks[] array ($a, 17) # two values, $a and 17 ($b+$c, $d+$e) # two values (“fred”, “barney”, “wilma”, “dino”) # 4 strings Perl - Tools For Lunch (v1.00)
Quoted Words • Use qw to generate a list of words without typing quote marks qw / fred barney betty wilma dino / • The whitespace is discarded and the elements are treated as single quoted strings • Can’t use \n or $scalar_name • How can I make a quoted word list out of these pathnames? /usr/local/pub /home/adjunct/sps Perl - Tools For Lunch (v1.00)
List Assignment • List values can be assigned to scalar variables ($fred, $barney, $dino) = (“flintstone”, “rubble”, undef); $fred = (“a”, “b”, “c”); # $fred = “c”; ($fred, $barney) = ($barney, $fred); # swapping ($betty[1], $betty[0]) = ($betty[0], $betty[1]); ($fred, $barney) = qw< flintstone rubble slate granite >; ($wilma, $barney) = qw[flintstone]; • Use the at-sign, @ , to refer to the entire array @rocks = qw/ bedrock slate lava /; @tiny = (); # empty list @giant = 1..1e5; # list of 100,000 elements @stuff = (@giant, undef, @giant) # 200,001 elements $dino = “granite”; @quarry = (@rocks, “crushed rock”, @tiny, $dino); # 5 elements Perl - Tools For Lunch (v1.00)
Interpolating Arrays into Strings • Arrays can be interpolated into double quoted strings • Elements of the array are automatically separated by the value of the special $” variable (default is space) $” = “, “; @rocks = qw{ flintstone slate rubble }; print “quartz @rocks limestone\n”; # output is: quartz flintstone, slate, rubble limestone • Be careful when building strings with the @ symbol $email=“nospam@cs.rit.edu” # what happens here? Perl - Tools For Lunch (v1.00)
List Assignment • Array assignment uses copy, not share @copy = @quarry; # copy entire list • Array operations @array = 5..9; # (5, 6, 7, 8, 9) $fred = pop(@array); # $fred = 9 push(@array, 1..2); # (5, 6, 7, 8, 1, 2) $a = shift(@array); # $a = 5 unshift(@array, 10); # (10, 6, 7, 8, 1, 2) @array = reverse(@array); # (2, 1, 8, 7, 6, 10) @array = sort(@array); # (1, 10, 2, 6, 7, 8) Perl - Tools For Lunch (v1.00)
Foreach Control Structure • Process an entire array in a list using foreach foreach $rock (qw/ bedrock slate lava /) { print “One rock is $rock.\n”; } • The control variable is a reference to the list element @rocks = qw/ bedrock slate lava /; $rock = “rush”; foreach $rock (@rocks) { $rock = “\t$rock”; $rock .= “\n”; } print “The rocks are:\n”, @rocks; # each tab+newline print “\$rock=$rock\n”; # $rock=“rush” Perl - Tools For Lunch (v1.00)
Default Variable $_ • The default scalar variable is $_ foreach (1..10) { # uses $_ by default print “I can count to $_\n”; } • Used as the default with many operators $_ = “Yabba dabba doo\n”; print; # prints $_ by default Perl - Tools For Lunch (v1.00)
Scalar and List Contexts • A given expression means different things depending on the context it is found in 5 + something; # something must be a scalar sort something; # something must be a list @people = qw/ fred barney betty /; # scalar context @sorted = sort @people; # list context $number = 5 + @people; # scalar context @list = @people; # list of 3 people $n = @people; # the number 3 Perl - Tools For Lunch (v1.00)
Scalar and List Contexts • To force a scalar context when Perl is expecting a list, use the fake function scalar @rocks = qw/ talc quartz jade obsidian /; print “How many rocks do you have?\n”; print “I have “, @rocks, “ rocks!\n”; # incorrect print “I have “, scalar @rocks, “ rocks!\n”; # correct Perl - Tools For Lunch (v1.00)
What is a Hash? • A hash is a data structure of scalar values whose elements are indexed by keys • The keys are arbitrary, unique strings Values 216.109.118.65 “www.yahoo.com” 12.4 “bar” “hello” Keys “2.5” 1.72e30 “wilma” “bye\n” “betty” Perl - Tools For Lunch (v1.00)
Defining and Invoking a Subroutine sub marine { $n += 1; # global variable print “Hello, sailor number $n!\n”; } • Subroutines occupy a separate namespace, and are invoked via the, & , operator &marine; # Hello, sailor number 1! $marine; # scalar access $marine[0]; # list access &marine; # Hello, sailor number 2! • Subroutine definitions are global - the last definition in, wins Perl - Tools For Lunch (v1.00)
Hash Element Access • The syntax for hash element access is to use curly braces (not parens!!!) $hash_name{$some_key} • An example: $family_name{“fred”} = “flintstone”; $family_name{“barney”} = “rubble”; foreach $person (qw/ barney fred /) { print “I’ve heard of $person $family_name{$person}.”; } “flintstone” “fred” “rubble” “barney” Perl - Tools For Lunch (v1.00)
Hash Element Access and The Hash as a Whole • Accessing outside the hash gives undef $granite = $family_name{“larry”}; # no larry – undef • To refer to the entire hash, use %hash_name • A list can be converted to a hash (parens, not curlies!) %some_hash = ( “www.yahoo.com”, 216.109.118.65, “bar”, 12.4, 2.5, “hello”, “wilma”, 1.72e30, “betty”, “bye\n”); • A hash can be unwound into a list of key-value pairs: @any_array = %some_hash; print “@any_array\n”; #betty bye (and a newline) wilma 1.72e+30 2.5 hello… Perl - Tools For Lunch (v1.00)
Hash Functions – keys and values • The keys function returns a list of all the hash keys and the values function returns the corresponding values my %hash = (“a” => 1, “b” => 2, “c” => 3); my @k = keys %hash; # “a”, “c”, “b” my @v = values %hash; # 1, 3, 2 • In a scalar context, the functions return the number of elements (key-value pairs) my $count = keys %hash; # gets 3 (three pairs) if (%hash) { print “The hash is not empty\n”; } Perl - Tools For Lunch (v1.00)
Hash Functions – each • Use each to iterate over an entire hash • The return value is a two element list (a key-value pair) while ( ($key, $value) = each %hash ) { print “$key => $value\n”; } • To process the hash in order, sort the keys and use foreach foreach $key (sort keys %hash) { print “$key => $hash{$key}\n”; } Perl - Tools For Lunch (v1.00)
Hash Functions – exists and delete • To see whether a key is present in the hash, use the exists function if (exists $books{“dino”}) { print “There’s a library card for dino!\n”; } • Use delete to remove a key and its corresponding value from the hash my $person = “betty”; delete $books{$person}; # revoke betty’s library card • This is different than storing undef as the value Perl - Tools For Lunch (v1.00)
Hash Sorting • Suppose we have a hash whose keys are player names and values are frags my %fragHash = ( “Player1” => 10, “Player2” => 5, “Player3” => 19 ); • You can write a custom compare function to sort to compare the values (frags) foreach (sort { $fragHash{$b} <=> $players{$a} } keys %fragHash) { print $_ . “=“ . $fragHash{$_} . “, “; } Output: Player3=19, Player1=10, Player2=5, Perl - Tools For Lunch (v1.00)
Private Variables in Subroutines • Perl creates lexical variables with the my operator, which are statically scoped to their declaration block • The initial value of variables created with my is undef sub max { my($a, $b); # new private variables, $a and $b; ($a, $b) = @_; # give names to the parameters if ($a > $b) { $a # semicolon optional for single line } else { $b # ditto } } • Reduce code by combining the first two statements: my($a, $b) = @_; # max expects 2 params Perl - Tools For Lunch (v1.00)
Variable-length Parameter Lists • High-water mark algorithm for computing the maximum number in a list: sub max { my($max_so_far) = shift @_; foreach (@_) { if ($_ > $max_so_far) { $max_so_far = $_; } } $max_so_far; } $maximum = &max(3, 5, 10, 4, 6); # 10 $maximum = &max; # what happens here? Perl - Tools For Lunch (v1.00)
use strict Pragma • A pragma is a hint to the compiler, telling it something about the code use strict; # enforce good programming practice my $bamm_bamm = 3; # creates new lexical variable $bammbamm = 1; # error • Warning! Do not use $a and $b as your variables! They are reserved for doing things like comparisons • Also don’t use $0 through $9 either • Run perldoc strict to get a complete list of restrictions • Must declare new variables with my • String must be quoted in most cases • References must be true (hard) Perl - Tools For Lunch (v1.00)
Reading Input • Data can be read from standard input by using the diamond operator $line = <STDIN>; # read one line @lines = <STDIN>; # read all lines • Use <> to make your programs work like standard Unix utilities: cat, grep, lpr, etc % ./myprog.pl fred barney - betty while (defined($line = <>)) { chomp($line); print “It was $line that I saw!\n”; } Perl - Tools For Lunch (v1.00)