Title: Introduction to Programming the WWW I
1Introduction to Programming the WWW I
- CMSC 10100-1
- Winter 2004
- Lecture 12
2Todays Topics
- CGI module (contd)
- Matching patterns
- Regular expressions
3Midterm Results
- Total points of the paper 50
- Highest grade 47.5
- Avg. grade 42.6
- 10 submitted papers
- 9 papers with grade gt 40
4Review Using CGI.pm to generate HTML
- The CGI.pm module provides several functions
that can be used to concisely output HTML tags - For example,
- mypageIt is a New Day
- print ltHTMLgtltHEADgtltTITLEgt mypage
lt/TITLEgtlt/HEADgtltBODYgt - can also be written as
- mypageIt is a New Day
- print start_html(mypage)
5Review Three Basic CGI.pm Modules
- start_html
- creates starting HTML tags
- Header
- creates the MIME Content-type line
- end_html
- creates ending HTML tags
1. !/usr/local/bin/perl 2. use CGI
standard 3. print header 4. print
start_html 5. print 'ltFONT size4
color"blue"gt' 6. print 'Welcome
ltIgthumanslt/Igt to my sitelt/FONTgt' 7. print
end_html
6Review CGI.pm Basic Functions
- The various CGI/PM function accept 3 basic
syntactic formats - No argument format
- functions that can be used without any arguments
- Positional argument format
- functions that can accept comma-separated
arguments within parentheses - Name-value argument format
- functions that accept parameters submitted as
name-and-value pairs
7Review Some Single Argument Functions
8Review Some Positional Functions
9Review Some name/value functions
10Review How Web Applications Work with CGI
11Review HTLM Forms
- HTML Forms are used to select different kinds of
user input, defined with ltformgt tag - Form contains form elements to allow the user to
enter information - text fields
- textarea fields
- drop-down menus
- radio buttons
- checkboxes, etc
12Review ltformgt Tag Attributes
- action attribute
- Gives the URL of the program(CGI) to receive and
process the forms data - method attribute
- Sets the HTTP method by which the browser sends
the form data to the program, value can be GET or
POST - Avoid GET method in favor of POST for security
reasons
13Review ltinputgt Tag
- To define any one of a number of common form
controls - Text fields (including password, hidden fields)
- multiple-choice lists
- Clickable images
- Submission buttons
- Only type and name attribute required
- No ending tag (no lt/inputgt)
14Review Text Fields
- single line of text
- ltinput typetext nameXXXgt
- Set type to password to mask text like a password
- Set type to hidden to create a hidden field
- size and maxlength attributes
- value attributes to give default text
15Review Multi-line Text Area
- The lttextareagt tag
- Attributes
- cols
- rows
- wrap
- Values Off,virtual(default),physical
16Review Check Boxes
- Check boxes for check all that apply questions
- ltinput typecheckbox nameXXX valueXXXgt
- Make sure name identical among a group of
checkboxes - checked attribute
- When form is submitted, names and values of those
checked boxes are sent
17Review Radio Buttons
- Similar as checkboxes, but only one in the group
may be selected - ltinput typeradio nameXXX valueXXXgt
18Review Multiple Choice Elements
- The ltselectgt tag creates either pull-down menus
or scrolling lists - The ltoptiongt tag defines each item within a
ltselectgt tag - ltselectgt tag attributes
- size attribute
- Number of rows visible at the same time
- multiple attribute
- If set, allow multiple selections
- name attribute
19Review Action Buttons
- What are they?
- Submit buttons
- ltinput typesubmit nameXXX valueXXXgt
- Reset buttons
- ltinput typereset nameXXX valueXXXgt
- Regular buttons
- ltinput typebutton nameXXX valueXXXgt
- image buttons (will send form content as submit
button) - ltinput typeimage nameXXX srcXXXgt
- File buttons (need to deal with enctyple
attribute) - ltinput typefile nameXXX accepttext/gt
20Using CGI.pm with HTML forms
21Using CGI.pm with HTML forms (contd)
Perl CGI Reference
22A CGI Form Example
http//people.cs.uchicago.edu/hai/hw4/cgiform1.cg
i
23Receiving HTML Form Arguments
- Within the CGI program call param() function
- Input variables into CGI/Perl programs are called
CGI variables - Values received through your Web server as input
from a Web browser, usually filled in a form - To use param()
24Receiving HTML Form Arguments
http//people.cs.uchicago.edu/hai/hw4/cgiform1.cg
i
25Sending Arguments
- You can send arguments to your CGI program
directly from the URL address of a browser
http//people.cs.uchicago.edu/hai/hw4/cgiform1_ch
ecker.cgi?colorred
26Sending Multiple Arguments
http//people.cs.uchicago.edu/hai/hw4/cgiform1_ch
ecker.cgi?colorredsecretnothing
Precede first argument with ?
Precede next argument with
27Debug CGI Program in Command Line
- To start and send an argument to the password
program can execute the following - perl cgiform1_checker.cgi colorred
- Enclose blank spaces or multiple arguments in
quotation marks - perl cgiform1_checker.cgi colorrose red
- perl cgiform1_checker.cgi 'colorredsecretnone'
28Check CGI VariablesValues
- Perl provides a simple method to test if any
parameters were received or null - var param(some_cgi_variable)
- if (var)
- statement(s) to execute when var has a value
- else
- statement(s) to execute when var has no value
29Combining Program Files
- Applications so far have required two separate
files one file for to generate the form, and the
other to process the form - Example
- cgiform1.cgi and cgiform1_checker.cgi
- Can test return value on param() to combine these
- At least two advantages
- With one file, it is easier to change arguments
- It is easier to maintain one file.
30Combining Program Files
http//people.cs.uchicago.edu/hai/hw4/cgiform2.cg
i
31Patterns in String Variables
- Many programming problems require matching,
changing, or manipulating patterns in string
variables. - An important use is verifying input fields of a
form - helps provide security against accidental or
malicious attacks. - For example, if expecting a form field to provide
a telephone number as input, your program needs a
way to verify that the input comprises a string
of seven digits.
32Four Different Constructs
- Will look at 4 different Perl String manipulation
constructs - The match operator enables your program to look
for patterns in strings. - The substitute operator enables your program to
change patterns in strings. - The split function enables your program to split
strings into separate variables based on a
pattern. (already covered) - Regular expressions provide a pattern matching
language that can work with these operators and
functions to work on string variables.
33The Match Operator
- The match operator is used to test if a pattern
appears in a string. - It is used with the binding operator () to
see whether a variable contains a particular
pattern.
34Possible Values of name
35Other Delimiters?
- Slash (/) is most common match pattern
- Others are possible, For example, both use valid
match operator syntax - if ( name m!Dave! )
- if ( name mltDavegt )
- The reverse binding operator test if pattern is
NOT found - if ( color ! m/blue/ )
36The Substitution Operator
- Similar to the match operator but also enables
you to change the matched string. - Use with the binding operator () to test
whether a variable contains a pattern
37How It Works
- Substitutes the first occurrence of the search
pattern for the change pattern in the string
variable. - For example, the following changes the first
occurrence of t to T - name tom turtle
- name s/t/T/
- print Namename
- The output of this code would be
- NameTom turtle
38Changing All Occurrences
- You can place a g (for global substitution) at
the end of the substitution expression to change
all occurrences of the target pattern string in
the search string. For example, - name tom turtle
- name s/t/T/g
- print Namename
- The output of this code would be
- Name Tom TurTle
39Using Translate
- A similar function is called tr (for
translate). Useful for translating characters
from uppercase to lowercase, and vice versa. - The tr function allows you to specify a range of
characters to translate from and a range of
characters to translate to. - name"smokeY"
- name tr/a-z/A-Z/
- print "namename"
- Would output the following
- NameSMOKEY
40A Full Pattern Matching Example
- 1. !/usr/local/bin/perl
- 2. use CGI 'standard'
- 3. print header, start_html('Command Search')
- 4. _at_PartNums( 'XX1234', 'XX1892', 'XX9510')
- 5. comparam('command')
- 6. prodparam('uprod')
- 7. if (com eq "ORDER" com eq "RETURN")
- 8. prod s/xx/XX/g switch xx to XX
- 9. if (prod /XX/ )
- 10. foreach item ( _at_PartNums )
- 11. if ( item eq prod )
- 12. print "VALIDATED commandcom
prodnumprod" - 13. found 1
- 14.
- 15.
- 16. if ( found ! 1 )
- 17. print br,"Sorry Prod Numprod
NOT FOUND" - 18.
- 19. else
41Would Output The Following ...
42Using Regular Expressions
- regular expressions to enable programs to match
patterns more completely . - They actually make up a small language of special
matching operators that can be employed to
enhance the Perl string pattern matching.
43The Alternation Operator
- Alternation operator looks for alternative
strings for matching within a pattern. - (That is, you use it to indicate that the
program should match one pattern OR the other).
The following shows a match statement using the
alternation operator (left) and some possible
matches based on the contents of address
(right) this pattern matches either com or edu.
44Example Alternation Operator
45Parenthesis For Groupings
- You use parentheses within regular expressions to
specify groupings. For example, the following
matches a name value of Dave or David.
46Special Character Classes
- Perl has a special set of character classes for
short hand pattern matching - For example consider these two statements
- if ( name m/ / )
- will match name with embedded space char
- if (name m/\s/ )
- will match name with embedded space, tab, newline
47Special Character Classes
48Special Character Classes - II
49Special Character Classes - III
50Setting Specific Patterns w/ Quantifiers
- Character quantifiers let you look for very
specific patterns - For example, use the dollar sign () to to
match if a string ends with a specified pattern. - if (Name /Jones/ )
- Matches John Jones but not Jones is here
would not. Also, The guilty party is Jones
would matches.
51Selected Perl Character Quantifiers I
52Selected Perl Character Quantifiers II
53Selected Perl Character Quantifiers III
54Building Regular Expressions That Work
- Regular expressions are very powerfulbut they
can also be virtually unreadable. - When building one, tart with a simple regular
expression and then refine it incrementally. - Build a piece and then test
- The following example will build a regular
expression for a date checker - dd/mm/yyyy format (for example, 05/05/2002 but
not 5/12/01).
55Building Regular Expressions That Work
- 1. Determine the precise field rules. - What is
valid input and what is not valid input? - E.g., For a date field, think through the valid
and invalid rules for the field. - You might allow 09/09/2002 but not 9/9/2002 or
Sep/9/2002. - Work through several examples as follows
56Work through several examples
57Building Regular Expressions that Work
- 2. Get form and form-handling programs working
- Build a sending form the input field
- Build the receiving program that accepts the
field. - For example, a first cut receiving program
- date param(udate)
- if ( date m/./ )
- print Valid date, date
- else
- print Invalid date, date
-
Any Sequence of characters
58Building Regular Expressions that Work
- 3. Start with the most specific term possible.
- For example, slashes must always separate two
characters (for the month), followed by two more
characters (for the day), followed by four
characters (for the year). - if ( date m../../.... )
Any 2 characters
Any 4 characters
Any 2 characters
59Building Regular Expressions that Work
- 4. Anchor and refine. (Use and when
possible) - if ( date m\d\d/\d\d/\d\d\d\d )
Starts with 2 digits
2 digits in middle
Ends with 4 digits
60Building Regular Expressions that Work
- 5. Get more specific if possible.
- The first digit of the month can be only 0, 1, 2
or 3. For example, 05/55/2002 is clearly an
illegal date. - Only years from this century are allowed. Because
we dont care about dates like 05/05/1999 or
05/05/3003.
61Building Regular Expressions that Work
- Add these rules below
- if ( date m\d\d/0-3\d/2\d\d\d )
-
-
- Now the regular expression recognizes input like
09/99/2001 and 05/05/4000 as illegal. -
Month starts with a 0-3
Year starts with a 2
62Tip Regular Expression Special Variables
- Perl regexs set several special scalar variables
- will be equal to the first matching text
- will be the text before the match, and
- will be the text after the first match.
- name'Marty'
- if ( name m/\w/ )
- print "got match at "
- print "B4 after'"
- else print "Not match"
- would output got match atM B4 afterarty
63Full Example Program
- 1. !/usr/local/bin/perl
- 2. use CGI 'standard'
- 3. print header, start_html('Date Check')
- 4. dateparam('udate')
- 5. if (date m\d\d/0-3\d/2\d\d\d)
- 6. print 'Valid date', date
- 7. else
- 8. print 'Invalid date', date
- 9.
- 10. print end_html
64Would Output The Following ...