Perl
Control Structures
A statement block is a sequence of Perl statements enclosed in matching
curly braces:
{
first_statement;
second_statement;
…
last_statement;
}
A statement block may be used in place of a single statement, but not
vice versa!
Conditional Statements
In conditional statements, Perl interprets an expression as either true
or false.
- False expressions evaluate to
- True expressions are everything else
if / unless statements
An if statement executes the statement block only if the expression
evaluates to true
- if ( expression )
statement_block
- if ( expression )
statement_block
else
statement_block
- if ( expression )
statement_block
elsif ( expression )
statement_block
else
statement_block
An unless statement executes the statement block only if the expression
evaluates to false
- unless ( expression )
statement_block
- unless ( expression )
statement_block
else
statement_block
while / until statements
A while statement repeatedly executes the statement block, as long as
the expression evaluates to true
An until statement repeatedly executes the statement block, as long
as the expression evaluates to false
do {} while / until statements
A do {} while statement executes the statement block, then reiterates
as long as the expression evaluates to true
A do {} until statement executes the statement block, then reiterates
as long as the expression evaluates to false
for / foreach statements
A for statement will
- execute the initial expression
- execute the statement block if the test expression evaluates to true
- execute the update expression, then reiterate if the test expression
still evaluates to true
for (init_exp; test_exp; update_exp)
statement_block
A foreach statement will
Hashes
A hash is an associative array that uses scalar keys to access array
elements
- A hash variable name begins with % followed by a letter, then more
letters, numbers, and underscores
- Each element of the hash is a scalar variable accessed by a string
index (key)
- Elements may be added either one at a time or all at once
- $birthday{"Mary"} = "12/10/81";
- %birthday = ("Mary", "12/10/81", "John",
"8/6/79");
- Actual order of the elements in the hash is determined by the system
Hash Functions
- reverse swaps keys and values
%people = reverse %birthday;
- keys yields a list of all key values in the hash
@names = keys(%birthday);
- values function returns a list of all the current values of the hash
@dates = values(%birthday);
- each will select every key/value pair
while (($n,$d) = each(%birthday)) { print "$n's birthday is $d\n";
}
Regular Expressions in Perl
Perl uses regular expressions -- usually delimited by / / -- to match
a template to a string. Some patterns are like those available with sed
and the Korn shell.
- literal strings (case sensitive)
/abc/ literally matches the sequence "abc"
- single character selection
/./ matches any character except newline
/[ab0-9]/ matches "a" or "b" or some digit
- multipliers
/a*b+c?/ matches 0 or more "a" followed by 1 or more "b"
followed by 0 or 1 "c"
- beginning and end of line
/^A/ matches a string that begins with "A"
/ing$/ matches a string that ends with "ing"
Matching with Regular Expressions
- Matching
- $a =~ /the/
true if variable $a contains pattern the
- /the/
true if variable $_ contains pattern the
- Substitution
- $a =~ s/abc/xyz/
replaces 1st occurrence of pattern abc with xyz in variable $a
- s/abc/xyz/
replaces 1st occurrence of pattern abc with xyz in variable $_
- s/abc/xyz/g
replaces all occurrences of pattern abc with xyz in variable $_
Split & Join
These operators break a string into fields, and glue fields together
in a single string
- Split uses a regular expression as a field separator
- @fields = split(/[:,]/, $line);
uses either ":" or "," as a field separator
- Join uses a given string as a field separator
- $longlist = join(":", @fields);
recombines elements of array @fields into a string, with elements separated
by ":"
Common Gateway Interface
A CGI program is a program run by the Web server in response to a request
from a client. Examples are
- Forms: gather information sent by the client
- Gateways: provide web access to information on the server, such as
a database
- Virtual documents: generate customized web pages on the fly
All CGI programs must print one of 3 headers, followed by a blank space,
to standard output:
- Content-type indicates http will follow
Content-type: text/html
- Location points to a document or other valid URL
Location: http://www.perl.com/CPAN
- Status doesn't affect what the browser shows
Status: 204 No Response
Creating a Form
A form begins with a <FORM> tag and ends with a </FORM>
tag.
- Attributes of the form include
- ACTION specifies URL of the CGI program
- METHOD indicates where parameters will go: GET (default) or POST (standard
input)
- <FORM METHOD=GET ACTION= "http://www.com/cgi-bin/hi.pl">
- Input fields are indicated with the <INPUT> tag
- TYPE indicates the type of input field
- NAME is a key word representing that field
- VALUE is the default value
- <INPUT TYPE=submit VALUE="Go!">
- Types of input fields include
- TEXT (default)
- CHECKBOX
- HIDDEN
- RADIO
- SUBMIT
- PASSWORD
Processing Input
Arguments are generally passed to the CGI program as part of the URL.
The CGI program accesses these arguments by examining environment variables.
- QUERY_STRING contains the parameters
$form_in = $ENV{'QUERY_STRING'};
- CONTENT_LENGTH = size of QUERY_STRING
read (STDIN, $query, $ENV{'CONTENT_LENGTH'});
- REQUEST_METHOD is GET or POST
if ($ENV{'REQUEST_METHOD'} eq "GET")