Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming the WWW I CMSC 10100-1 Winter 2003 Lecture 17.

Similar presentations


Presentation on theme: "Introduction to Programming the WWW I CMSC 10100-1 Winter 2003 Lecture 17."— Presentation transcript:

1 Introduction to Programming the WWW I CMSC 10100-1 Winter 2003 Lecture 17

2 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

3 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

4 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

5 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)

6 Options for opening files Read (default): open(HANDLE,’<filename’); Write: open(HANDLE,’>filename’); Append open(HANDLE,’>>filename’); Uses redirection operators from Unix

7 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

8 Reading from files (and STDIN) Use the syntax to get either a line or the whole file $aline = ; @lines = ; By not specifying a location, the line of input appears in $_.

9 Example To read in a whole file and print it back to the screen, we use the code open(FILE,’filename’) || die $!; while( ) { print $_; } close FILE; An EOF is interpreted as false in the loop

10 Even more arcane open(FILE,’filename’) || die $!; while( ) { print; } close FILE; The default argument of print (and some other functions) is $_.

11 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

12 Example Read and print email addresses Read, sort, and print email addresses Read and print links to email addresses

13 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

14 Functions You can define your own functions in Perl: sub foo { # do stuff # return a value }

15 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

16 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; }

17 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 $_

18 Example #!/usr/bin/perl print <<END; type a message till you say the word. Press ^D to quit END while ( ) { if (m/foo/) { print "you typed 'foo'!"; exit(); }

19 Applications Form validation –checking dates –ZIP codes Parsing/reading files

20 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

21 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

22 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

23 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

24 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

25 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

26 Next time Multi-page sessions Cookies Hidden fields


Download ppt "Introduction to Programming the WWW I CMSC 10100-1 Winter 2003 Lecture 17."

Similar presentations


Ads by Google