Title: Perl: Lecture 2
1Perl Lecture 2
2Regular Expressions 2
3Advanced Pattern Matching
- Anchor Metacharacters
- Beginning of String
- End of String
"housekeeper" /keeper/ matches
"housekeeper" /keeper/ no match
"housekeeper" /keeper/ matches
"housekeeper\n" /keeper/ matches
"housekeeper" /housekeeper/ matches
4Character classes
- notation
- - range of characters
- negated
/bcat/ matches 'bat', 'cat' "abc"
/cab/ matches 'a' /\cdef/ matches
'def' or 'cdef'
/item0-9/ matches 'item0' or ... or
'item9' /0-9a-fA-F/ matches a hexadecimal
digit
/0-9/ matches a non-numeric character
5Common Character Classes
- \d digit 0-9
- \s whitespace \ \t\r\n\f
- \w word character 0-9a-zA-Z_
- \D negated \d 0-9
- \S negated \s \s
- \W negated \w \w
- '.' any character but ''\n'
- \b word boundary \w\W or \W\w
6Alternation Grouping
- alternation operator
- ( ) grouping
- m//i modifier
- Case-insensitive match
"cats and dogs" /dogcatbird/ matches
"cat"
/house(catkeeper)/ "20" /(1920)\d\d/
7Extracting Matches
- Grouped patterns can be extracted
time /(\d\d)(\d\d)(\d\d)/ hours 1
minutes 2 seconds 3 (hours,
minutes, second) (time
/(\d\d)(\d\d)(\d\d)/)
8Matching repetitions
- Quantifier Metacharacters
- ? 0 or 1 times
- 0 or more times
- 1 or more times
- n,m at least n, at most m times
- n, at least n times
- n exactly n times
/\w/ matches a word year /\d4\d2/
2 or 4 digit years
9Search and Replace
- s/regex/replacement/ Syntax
- replaces only one occurence
- s///g modifier replaces all occurences
x "Time to feed the cat!" x
s/cat/dog/ y s/'(.)'/1/
10RE functions
- quotemeta EXPR
- escapes all RE metacharacters
- split /PATTERN/, EXPR
- splits a String at a delimiter specified by
PATTERN - pos SCALAR
- Returns offset of last m//g occurence
11Time functions
- time
- Returns number of seconds since jan 1, 1970
- localtime
- Convert time to human-readable format
- (sec,min,hour,mday,mon,year,wday,yday,is
dst) localtime(time) - gmtime
- Do the same on the basis of GMT
12CGI
13What is CGI?
- Common Gateway Interface
- Not a programming language
- Method for the web server for the invocation of a
program on a users request - Method of exchanging data between a user, a web
server and the program
14CGI Functionality
- CGI Program invoked when its URL is requested
- Parameters / Data Included in the request
15Setting up the web server for Perl CGI
- Example Microsoft Windows, Apache HTTPD
- HTTPD is preconfigured for scripts
- http//host/cgi-bin
- ! required (e.g. !C\perl\bin\perl)
- Possibly delete windows file type association
16CGI Data Exchange overview
Web Server
Perl Script
Request GET / POST
Response
User
17Simple CGI program
- !/usr/bin/perl
- print Content-type text/html\n\n
- print lthtmlgtltheadgtlt/headgtltbodygt
- print lth1gtThis is CGIlt/h1gt
- print lt/bodygtlt/htmlgt
18CGI Environment Variables
- HTTP_ACCEPT"/"
- HTTP_ACCEPT_ENCODING"gzip, deflate"
- HTTP_ACCEPT_LANGUAGE"en-us"
- HTTP_CONNECTION"Keep-Alive"
- HTTP_HOST"localhost"
- HTTP_USER_AGENT"Mozilla/4.0 (compatible MSIE
6.0 Windows NT 5.0)" - QUERY_STRING""
- REMOTE_ADDR"127.0.0.1"
- REMOTE_PORT"3039"
- REQUEST_METHOD"GET"
- REQUEST_URI"/cgi-bin/printenv.pl"
- SCRIPT_NAME"/cgi-bin/printenv.pl"
- SERVER_ADDR"127.0.0.1"
- SERVER_ADMIN"trace_at_gmx.net"
- SERVER_NAME"ba35002"
- SERVER_PORT"80"
- SERVER_PROTOCOL"HTTP/1.1"
- SERVER_SIGNATURE"ltADDRESSgtApache/1.3.27 Server
at ba35002 Port 80lt/ADDRESSgt\n" - SERVER_SOFTWARE"Apache/1.3.27 (Win32) PHP/4.2.3"
19Generating the response
- Own result
- Content-type text/html
- print "Content-type mime_type\n\n"
- Redirection
- Location http//www.domain.com/newpage
- print "Location url\n\n"
20Getting client data to the server
- Usually from an HTML form
- Two methods defined in HTTP
- GET
- Data in request URL
- Accessable by CGI script using QUERY_STRING env
var - POST
- Data after request
- Accessable STDIN and CONTENT_LENGTH env var
- Both methods use URL-encoding
- Fields separated by , values after sign
- Space -gt
- Special characters -gt xx
21Where is the data? HTML Forms (1)
- ltform actionURL methodget/postgt
- lt/formgt
- ltinput typesubmit valuelabel /gt
- ltinput typereset valuelabel /gt
- ltinput namename typepassword /gt
22Where is the data? HTML Forms (2)
-
- lttextarea namename /gt
- ltselect nametop5 multiplegt
- ltoption valuevaluegtLabellt/optiongt
- lt/selectgt
- ltinput typeradio namename valuevalue /gt
Label - ltinput typecheckbox namename valuevalue
/gt Label - ltinput typehidden namename valuevalue /gt
23GET / POST examples
GET /cgi-bin/script.pl?inhellotherebuttonSend
HTTP/1.0
POST /cgi-bin/script.pl HTTP/1.0 Content-type
application/x-www-form-urlencoded Content-length
26 inhellotherebuttonSend
24Tasks to work with data
- Find out if GET or POST is used
- Split into single fields
- URL-decode
- Send HTTP header w/MIME-type data
- Used for nearly every CGI script, so already
build into Perl!
25CGI.pm example
use CGI qw(standard) print header() _at_namespa
ram() foreach name (_at_names) my
_at_valueparam(name) print name -gt
_at_valueltbrgt\n"
26Error 500?
- Internal Server Error
- Script behaved incorrectly
- See Web Server Log for more details
- apache\logs\error.log