Perl: Lecture 2 - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Perl: Lecture 2

Description:

Perl: Lecture 2. Advanced RE & CGI. Regular Expressions 2 ... HTTP_ACCEPT_ENCODING='gzip, deflate' HTTP_ACCEPT_LANGUAGE='en-us' HTTP_CONNECTION='Keep-Alive' ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 27
Provided by: trA56
Category:

less

Transcript and Presenter's Notes

Title: Perl: Lecture 2


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

/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
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'
  • \b word boundary \w\W or \W\w

6
Alternation Grouping
  • alternation operator
  • ( ) grouping
  • m//i modifier
  • Case-insensitive match

"cats and dogs" /dogcatbird/ matches
"cat"
/house(catkeeper)/ "20" /(1920)\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 /\d4\d2/
2 or 4 digit years
9
Search 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/
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,yday,is
    dst) 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 users 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
  • ! required (e.g. !C\perl\bin\perl)
  • Possibly delete windows file type association

16
CGI Data Exchange overview
Web Server
Perl Script
Request GET / POST
Response
User
17
Simple CGI program
  • !/usr/bin/perl
  • print Content-type text/html\n\n
  • print lthtmlgtltheadgtlt/headgtltbodygt
  • print lth1gtThis is CGIlt/h1gt
  • print lt/bodygtlt/htmlgt

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

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 -gt
  • Special characters -gt xx

21
Where is the data? HTML Forms (1)
  • ltform actionURL methodget/postgt
  • lt/formgt
  • ltinput typesubmit valuelabel /gt
  • ltinput typereset valuelabel /gt
  • ltinput namename typepassword /gt

22
Where 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

23
GET / POST examples
  • GET example
  • POST example

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
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() _at_namespa
ram() foreach name (_at_names) my
_at_valueparam(name) print name -gt
_at_valueltbrgt\n"
26
Error 500?
  • Internal Server Error
  • Script behaved incorrectly
  • See Web Server Log for more details
  • apache\logs\error.log
Write a Comment
User Comments (0)
About PowerShow.com