Introduction to Programming the WWW I

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

description

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 - PowerPoint PPT Presentation

Transcript of Introduction to Programming the WWW I

Page 1: Introduction to Programming the WWW I

Introduction to Programming the WWW I

CMSC 10100-1

Winter 2003

Lecture 17

Page 2: Introduction to Programming the WWW I

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

Page 3: Introduction to Programming the WWW I

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

Page 4: Introduction to Programming the WWW I

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

Page 5: Introduction to Programming the WWW I

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)

Page 6: Introduction to Programming the WWW I

Options for opening files

• Read (default):open(HANDLE,’<filename’);

• Write:open(HANDLE,’>filename’);

• Appendopen(HANDLE,’>>filename’);

• Uses redirection operators from Unix

Page 7: Introduction to Programming the WWW I

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

Page 8: Introduction to Programming the WWW I

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

Page 9: Introduction to Programming the WWW I

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

Page 10: Introduction to Programming the WWW I

Even more arcane

open(FILE,’filename’) || die $!;

while(<FILE>) {

print;

}

close FILE;

• The default argument of print (and some other functions) is $_.

Page 11: Introduction to Programming the WWW I

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

Page 12: Introduction to Programming the WWW I

Example

• Read and print email addresses

• Read, sort, and print email addresses

• Read and print links to email addresses

Page 13: Introduction to Programming the WWW I

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

Page 14: Introduction to Programming the WWW I

Functions

• You can define your own functions in Perl:

sub foo {

# do stuff

# return a value

}

Page 15: Introduction to Programming the WWW I

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

Page 16: Introduction to Programming the WWW I

Example: maximum of a setsub nmax { my $local_max = $_[0]; for (my $i=1;$i<=$#_;$i++) { if ($_[$i] > $local_max) { $local_max = $_[$i]; } } return $local_max;}

Page 17: Introduction to Programming the WWW I

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

Page 18: Introduction to Programming the WWW I

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

Page 19: Introduction to Programming the WWW I

Applications

• Form validation– checking dates– ZIP codes

• Parsing/reading files

Page 20: Introduction to Programming the WWW I

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

Page 21: Introduction to Programming the WWW I

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

Page 22: Introduction to Programming the WWW I

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

Page 23: Introduction to Programming the WWW I

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

Page 24: Introduction to Programming the WWW I

Capturing subpatterns• Regexp in parentheses cause the matched text to

be remembered for laterm/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

Page 25: Introduction to Programming the WWW I

Substitution operator• We can also replace one expression with another

bys/PATTERN/REPLACEMENT/pattern may have a regexp, replacement is a double-

quoted string$string = “abcdefgh”;$string =~ s/def.*/...xyz/;# $string now abc...xyz

Page 26: Introduction to Programming the WWW I

Next time

• Multi-page sessions

• Cookies

• Hidden fields