260 likes | 344 Views
Introduction to Programming the WWW I. CMSC 10100-1 Winter 2003 Lecture 17. Environment variables. When Perl is started from a browser, a special hash list is created describing how it was invoked. This is called the environmental hash list and is called %ENV
E N D
Introduction to Programming the WWW I CMSC 10100-1 Winter 2003 Lecture 17
Environment variables • When Perl is started from a browser, a special hash list is created describing how it was invoked. This is called the environmental hash list and is called %ENV • Referring Web site, what browser, language, method, remote address, remote host
Environment variables HTTP_REFERRER, REQUEST_USER_AGENT, HTTP_ACCEPT_LANGUAGE, REQUEST_METHOD, REMOTE_ADDRESS, REMOTE_HOST • Can configure response to browser or disallow/allow certain domains
What next? • Scripts to write the forms • Scripts to validate the form and spit it back if the user didn’t enter everything correctly • Example: newform.pl
Files • Files are opened via the open command: open(FILE,’filename’); • First argument is the “handle” • Second argument is a string -- the name of the file (perhaps including path)
Options for opening files • Read (default): open(HANDLE,’<filename’); • Write: open(HANDLE,’>filename’); • Append open(HANDLE,’>>filename’); • Uses redirection operators from Unix
What about errors? • Errors can occur if a file that doesn’t exist is opened for reading, etc open(HANDLE,’<filename’) || die “Can’t open file $!”; • sends error message to STDERR • The variable $! contains the latest error message returned by a system call • open returns 0 or 1
Reading from files (and STDIN) • Use the syntax <HANDLE> to get either a line or the whole file $aline = <MYFILE>; @lines = <MYFILE>; • By not specifying a location, the line of input appears in $_.
Example • To read in a whole file and print it back to the screen, we use the code open(FILE,’filename’) || die $!; while(<FILE>) { print $_; } close FILE; • An EOF is interpreted as false in the loop
Even more arcane open(FILE,’filename’) || die $!; while(<FILE>) { print; } close FILE; • The default argument of print (and some other functions) is $_.
More implicit variables • Records in a file (normally lines) are separated by $/ • changing this from “\n” to “” reads in paragraph mode, to “undef();” reads in the whole file • Output field separator: $, • print “one”, “two” equivalent to • print “one” . $, . “two • Output record separator: $\ • Typically blank • Changing it changes the terminal value of print statements
Example • Read and print email addresses • Read, sort, and print email addresses • Read and print links to email addresses
Editing a file • Open file for reading • Read everything into memory • Close the file • Make the changes (in memory) • Open the file for writing • Write the file • Close the file
Functions • You can define your own functions in Perl: sub foo { # do stuff # return a value }
Where are the parameters? • You call foo(a,b); (or something) • How do you get the values? • Another cooky variable --- @_ • arbitrary list of parameters read in
Example: maximum of a set sub nmax { my $local_max = $_[0]; for (my $i=1;$i<=$#_;$i++) { if ($_[$i] > $local_max) { $local_max = $_[$i]; } } return $local_max; }
Pattern matching TARGET =~ m/PATTERN/ • TARGET is the string we are searching • =~ is the binding operator • PATTERN is what we are looking for • Returns either true or false • Default target is $_
Example #!/usr/bin/perl print <<END; type a message till you say the word. Press ^D to quit END while (<STDIN>) { if (m/foo/) { print "you typed 'foo'!"; exit(); } }
Applications • Form validation • checking dates • ZIP codes • Parsing/reading files
Matching constructs • Don’t just have to match exact expressions • Eg: m/foo/i (case insensitive) • Eg: m/foo[bc]ar/ matches foobar and foocar • Can specify the complement of a set: m/foo[^bc]ar/ to match foo?ar with ? not b or c
Other constructs • Range: [a-g] same as [abcdefg] • Backslash sequences: • \d same as [0-9], \D = [^0-9] • \w is “word character” [a-zA-Z0-9_] • \s is any whitespace character • . is anything but new line
Anchors • We can match a string beginning with a value by: m/^abc/ • We can match a string ending with a value by: m/abc$/ • We can also match both
Quantifiers • By default, regexp characters match one and only one thing. We can edit this with quantifiers: • ? means 0 or 1 of the preceeding • * means 0 or more • + means 1 or more • a{n,m} matches between n and m a’s
Capturing subpatterns • Regexp in parentheses cause the matched text to be remembered for later m/foo(\w)bar/ • stores whatever word character occurs between foo and bar into $1 • This can be referred to as \1 in the regexp: m/foo(\w)bar\1/ matches fooTbarT but not foodbars
Substitution operator • We can also replace one expression with another by s/PATTERN/REPLACEMENT/ pattern may have a regexp, replacement is a double-quoted string $string = “abcdefgh”; $string =~ s/def.*/...xyz/; # $string now abc...xyz
Next time • Multi-page sessions • Cookies • Hidden fields