Download presentation
Presentation is loading. Please wait.
Published bySuzan Shelton Modified over 9 years ago
1
Perl: Lecture 2 Advanced RE & CGI
2
Regular Expressions 2
3
Advanced 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
4
Character classes „[ ]“ notation „-“ range of characters „^“ negated /[bc]at/; # matches 'bat', 'cat' "abc" =~ /[cab]/; # matches 'a' /[\]c]def/; # matches ']def' or 'cdef' /item[0-9]/; # matches 'item0' or... or 'item9' /[0-9a-fA-F]/; # matches a hexadecimal digit /[^0-9]/; # matches a non-numeric character
5
Common 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'‚ \bword boundary\w\W or \W\w
6
Alternation & Grouping „|“ alternation operator „( )“ grouping m//i modifier – Case-insensitive match "cats and dogs" =~ /dog|cat|bird/; # matches "cat" /house(cat|keeper)/ "20" =~ /(19|20|)\d\d/;
7
Extracting 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)/);
8
Matching 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 =~ /\d{4}|\d{2}/; # 2 or 4 digit years
9
Search and Replace s/regex/replacement/ Syntax replaces only one occurence – s///g modifier replaces all occurences s///g $x = "Time to feed the cat!"; $x =~ s/cat/dog/; $y =~ s/^'(.*)'$/$1/;
10
RE 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
11
Time functions time – Returns number of seconds since jan 1, 1970 localtime – Convert time to human-readable format ($sec,$min,$hour,$mday,$mon,$year,$wday,$yd ay,$isdst) = localtime(time); gmtime – Do the same on the basis of GMT
12
CGI
13
What is CGI? Common Gateway Interface Not a programming language Method for the web server for the invocation of a program on a user‘s request Method of exchanging data between a user, a web server and the program
14
CGI Functionality CGI Program invoked when its URL is requested Parameters / Data Included in the request
15
Setting up the web server for Perl CGI Example Microsoft Windows, Apache HTTPD – HTTPD is preconfigured for scripts – http://host/cgi-bin http://host/cgi-bin – #! required (e.g. #!C:\perl\bin\perl) – Possibly delete windows file type association
16
CGI Data Exchange overview Web Server User Perl Script Request GET / POST Response
17
Simple CGI program #!/usr/bin/perl print „Content-type: text/html\n\n“; print „ “; print „ This is CGI “; print „ “;
18
CGI 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@gmx.net" SERVER_NAME="ba35002" SERVER_PORT="80" SERVER_PROTOCOL="HTTP/1.1" SERVER_SIGNATURE=" Apache/1.3.27 Server at ba35002 Port 80 \n" SERVER_SOFTWARE="Apache/1.3.27 (Win32) PHP/4.2.3"
19
Generating 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";
20
Getting 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 -> + – Special characters -> %xx
21
Where is the data? HTML Forms (1)
22
Where is the data? HTML Forms (2) Label Label
23
GET / POST examples GET example POST example POST /cgi-bin/script.pl HTTP/1.0 Content-type: application/x-www-form-urlencoded Content-length: 26 in=hello+there&button=Send GET /cgi-bin/script.pl?in=hello+there&button=Send HTTP/1.0
24
Tasks 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!
25
CGI.pm example use CGI qw(:standard); print header(); @names=param(); foreach $name (@names) { my @value=param($name); print “$name -> @value \n"; }
26
Error 500? Internal Server Error Script behaved incorrectly See Web Server Log for more details – apache\logs\error.log
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.