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.

if / unless statements

An if statement executes the statement block only if the expression evaluates to true

An unless statement executes the statement block only if the expression evaluates to false

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

  1. execute the initial expression
  2. execute the statement block if the test expression evaluates to true
  3. execute the update expression, then reiterate if the test expression still evaluates to true
  4. 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

Hash Functions

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.

Matching with Regular Expressions

Split & Join

These operators break a string into fields, and glue fields together in a single string


Common Gateway Interface

A CGI program is a program run by the Web server in response to a request from a client. Examples are

All CGI programs must print one of 3 headers, followed by a blank space, to standard output:

Creating a Form

A form begins with a <FORM> tag and ends with a </FORM> tag.

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.