DOC PREVIEW
Princeton COS 333 - Perl

This preview shows page 1-2-3 out of 10 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 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 10 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 10 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1Perl• background• usage• basic language– variables, operators, expressions, control flow, …• arrays and hashes• scalar and list contexts• file handles and I/O• regular expressions and strings• extension• performance• assessmentPerl• developed ~1987 by Larry Wall• a reaction to features lacking in Awk– "Larry's first thought was "Let's use awk." Unfortunately, the awk of that day couldn't handle opening and closing of multiple files based on information in the files. Larry didn't want to have to code a special-purpose tool. As a result, a new language was born."– plus pieces from shell, sed, C, …• started small, now large & complicated– "kitchen sink" language– we'll do only a very small part• system administration tool– string processing– lots of functions to access (Unix) system calls• primary scripting language for Web programming– string processing– cgi-bin scripts for generating Web pages in response to queries2Running perl:% perl -e 'print "hello, world\n";'% perl hello.pl% perlprint "hello, world\n";(ctl-D)hello, world• Disclaimer: I am NOT a Perl expert• seeProgramming Perl, 3rd editionLarry Wall, Tom Christiansen, Randal Schwartz (O'Reilly, 2000)World's most boring examplefor ($fahr = 0; $fahr <= 300; $fahr += 20) {printf("%3d %6.1f\n", $fahr, 5/9 * ($fahr-32));} # World's most boring example• while, for loops are like C• if (…) {…} elsif (…) {…} else {…}• {…} and terminating semicolons are required• scalar variable indicated by $: $name– $ is required• arithmetic is float (5/9 is 0.555, not zero)• variables hold strings or numbers as in Awk– interpretation is determined by operators & context• operators:– arithmetic operators much like C– string concatenation uses . ("dot")– relational operators are different for string comparison and numeric comparisoneq ne lt le gt ge vs. == != < <= > >=– file test operators -f, -d, …– regular expression operators3Safety measures• Perl is often too forgiving – like most scripting languages• -w flag warns about potential errors like undefined or uninitialized variables• use strict enforces variable declarations, etc.• my $var declares variable– my ($v1, $v2) to declare several variables#!/usr/local/bin/perl -wuse strict;my $fahr;for ($fahr = 0; $fahr <= 300; $fahr += 20) {printf("%3d %6.1f\n", $fahr, 5/9 * ($fahr - 32));}Arrays• array variable indicated by @: @arrname• elements accessed as $arrname[$index]• subscripts normally range from 0 to $#arrnameinclusive• echo command (two versions):for ($i = 0; $i <= $#ARGV; $i++) {if ($i < $#ARGV) {print "$ARGV[$i] ";} else {print "$ARGV[$i]\n";}}foreach $i (0 .. $#ARGV) {print $ARGV[$i] . ($i < $#ARGV ? " " : "\n");}4Scalar and list contexts• two basic contexts: scalar and list– an array is really a list@arr = (1, 2.3, "hello", 45);• many operators take a list as argument– and often return a list: keys, sort, reverse, grep, …• many operators can produce a scalar or a list, depending on context in which operator occurs– sort LIST produces a list– print LIST produces a scalar (string)– print sort LIST produces a sorted scalar– split LIST returns a list/array@wds = split " ", $_($n, $v) = split;–grepPAT, LIST produces a list@nonb_lines = grep /./, @all_lines;– print sort grep(PAT, LIST) produces a sorted string"There's more than one way to do it"• echo command using list contexts and conversionsforeach $i (@ARGV) { print $ARGV[$i] . ($i < $#ARGV ? " " : "\n"}foreach (@ARGV) { print $ARGV[$i] . ($_ < $#ARGV ? " " : "\n"}print join(" ", @ARGV) . "\n";print "@ARGV\n";5Hashes (== associative arrays)• associative arrays are a separate type • hash indicated by %: %hashname• subscripts are arbitrary strings– stored in arbitrary order– accessed as $hashname{$str}• example: add up values from name-value inputpizza 200beer 100pizza 500coke 50my ($i, %val, @wds);while (<>) { # loop over ARGV files@wds = split;$val{$wds[0]} += $wds[1];}foreach $i (keys %val) { # note keysprint "$i $val{$i}\n";}• AWK version:{ val[$1] += $2 }END { for (i in val)print i, val[i]}File handles and I/O• open function connects file to file handle– open(FH, "file") for reading– open(FH, ">file") for writing, >> for append– open(FH, "|cmd") for piping to– open(FH, "cmd |") for piping from– STDIN, STDOUT, STDERR already open• copy input lines to output:while (<>) { # all files in input listprint "$_"; # $_ is input line with \n}• add up names and values:open(SORT, "|sort +1 -nr");while (<>) {($n, $v) = split;$val{$n} += $v;}foreach $i (keys %val) {print SORT "$i\t$val{$i}\n";}• close function breaks connection, recovers resources:close FH6ARGV and file handles• special cases for ARGV– @ARGV is array/list of all command line arguments– <ARGV> is array/list of all lines of all arguments– <> is an abbreviation for <ARGV>• cat command:foreach $i (@ARGV) {open(IN, $i) or die "can't open $i: $!";while (<IN>) {print $_;}close IN;}• shorter versions:while (<ARGV>) { # each line of each file argprint "$_";}while (<>) { # ARGV is implicitprint; # $_ is implicit}print <ARGV>; # print gives scalar contextprint <>; # ARGV is implicitRegular exprs and pattern matching• m// match operator– m/re/ matches (is true) if re matches operandif (m/[yn]/) … implicit operand is $_if (/[yn]/) … implicit operand is $_• s/re/repl/ substitution operator– replace re with repl in target • tie these to an explicit string with =~ operator$str =~ s/re/repl/g; # g = global, i = ignore caseif ($str =~ /[yn]/i) …• shorthands– \d = digit, \D = non-digit– \w = "word" [a-zA-Z0-9_], \W = non-word– \s = whitespace, \S = non-whitespace– \b = word boundary, \B = non-boundary• substrings– matched parts are saved for later use in $1, $2, ...s/(\S+)\s+(\S+)/$2 $1/ swaps first two words• there's lots more!7More regexprs# remove some HTML sequences, print 1 word/lineouter:while (<>) { # collect all input into single stringif (/<script|<SCRIPT/) {while (<>) {next outer if (/<\/script|<\/SCRIPT/);}}$str .= $_; # by concatenating input lines}$str =~ s/<[^>]*>//g; # delete <...>$str =~ s/&nbsp;/ /g; # replace &nbsp; by blank$str =~ s/\s+/\n/g; # compress white spaceprint $str;Control flow revisitedif (…) {…} elsif (…) {…} else {…}stmt if expr;stmt unless expr;while (…) {…}stmt while expr;until (…)


View Full Document

Princeton COS 333 - Perl

Download Perl
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 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 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?