.\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sh \" Subsection heading
.br
.if t .Sp
.ne 5
.PP
\fB\\$1\fR
.PP
..
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings. \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote. | will give a
.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
.\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
.\" expand to `' in nroff, nothing in troff, for use with C<>.
.tr \(*W-|\(bv\*(Tr
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
. ds -- \(*W-
. ds PI pi
. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
. ds L" ""
. ds R" ""
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds -- \|\(em\|
. ds PI \(*p
. ds L" ``
. ds R" ''
'br\}
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.if \nF \{\
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. nr % 0
. rr F
.\}
.\"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.hy 0
.if n .na
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear. Run. Save yourself. No user-serviceable parts.
. \" fudge factors for nroff and troff
.if n \{\
. ds #H 0
. ds #V .8m
. ds #F .3m
. ds #[ \f1
. ds #] \fP
.\}
.if t \{\
. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
. ds #V .6m
. ds #F 0
. ds #[ \&
. ds #] \&
.\}
. \" simple accents for nroff and troff
.if n \{\
. ds ' \&
. ds ` \&
. ds ^ \&
. ds , \&
. ds ~ ~
. ds /
.\}
.if t \{\
. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
. \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
. \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
. \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
. ds : e
. ds 8 ss
. ds o a
. ds d- d\h'-1'\(ga
. ds D- D\h'-1'\(hy
. ds th \o'bp'
. ds Th \o'LP'
. ds ae ae
. ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "CGI 3"
.TH CGI 3 "2001-09-21" "perl v5.8.8" "Perl Programmers Reference Guide"
.SH "NAME"
CGI \- Simple Common Gateway Interface Class
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
.Vb 2
\& # CGI script that creates a fill-out form
\& # and echoes back its values.
.Ve
.PP
.Vb 16
\& use CGI qw/:standard/;
\& print header,
\& start_html('A Simple Example'),
\& h1('A Simple Example'),
\& start_form,
\& "What's your name? ",textfield('name'),p,
\& "What's the combination?", p,
\& checkbox_group(-name=>'words',
\& -values=>['eenie','meenie','minie','moe'],
\& -defaults=>['eenie','minie']), p,
\& "What's your favorite color? ",
\& popup_menu(-name=>'color',
\& -values=>['red','green','blue','chartreuse']),p,
\& submit,
\& end_form,
\& hr;
.Ve
.PP
.Vb 9
\& if (param()) {
\& my $name = param('name');
\& my $keywords = join ', ',param('words');
\& my $color = param('color');
\& print "Your name is",em(escapeHTML($name)),p,
\& "The keywords are: ",em(escapeHTML($keywords)),p,
\& "Your favorite color is ",em(escapeHTML($color)),
\& hr;
\& }
.Ve
.SH "ABSTRACT"
.IX Header "ABSTRACT"
This perl library uses perl5 objects to make it easy to create Web
fill-out forms and parse their contents. This package defines \s-1CGI\s0
objects, entities that contain the values of the current query string
and other state variables. Using a \s-1CGI\s0 object's methods, you can
examine keywords and parameters passed to your script, and create
forms whose initial values are taken from the current query (thereby
preserving state information). The module provides shortcut functions
that produce boilerplate \s-1HTML\s0, reducing typing and coding errors. It
also provides functionality for some of the more advanced features of
\&\s-1CGI\s0 scripting, including support for file uploads, cookies, cascading
style sheets, server push, and frames.
.PP
\&\s-1CGI\s0.pm also provides a simple function-oriented programming style for
those who don't need its object-oriented features.
.PP
The current version of \s-1CGI\s0.pm is available at
.PP
.Vb 2
\& http://www.genome.wi.mit.edu/ftp/pub/software/WWW/cgi_docs.html
\& ftp://ftp-genome.wi.mit.edu/pub/software/WWW/
.Ve
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
.Sh "\s-1PROGRAMMING\s0 \s-1STYLE\s0"
.IX Subsection "PROGRAMMING STYLE"
There are two styles of programming with \s-1CGI\s0.pm, an object-oriented
style and a function-oriented style. In the object-oriented style you
create one or more \s-1CGI\s0 objects and then use object methods to create
the various elements of the page. Each \s-1CGI\s0 object starts out with the
list of named parameters that were passed to your \s-1CGI\s0 script by the
server. You can modify the objects, save them to a file or database
and recreate them. Because each object corresponds to the \*(L"state\*(R" of
the \s-1CGI\s0 script, and because each object's parameter list is
independent of the others, this allows you to save the state of the
script and restore it later.
.PP
For example, using the object oriented style, here is how you create
a simple \*(L"Hello World\*(R" \s-1HTML\s0 page:
.PP
.Vb 7
\& #!/usr/local/bin/perl -w
\& use CGI; # load CGI routines
\& $q = new CGI; # create new CGI object
\& print $q->header, # create the HTTP header
\& $q->start_html('hello world'), # start the HTML
\& $q->h1('hello world'), # level 1 header
\& $q->end_html; # end the HTML
.Ve
.PP
In the function-oriented style, there is one default \s-1CGI\s0 object that
you rarely deal with directly. Instead you just call functions to
retrieve \s-1CGI\s0 parameters, create \s-1HTML\s0 tags, manage cookies, and so
on. This provides you with a cleaner programming interface, but
limits you to using one \s-1CGI\s0 object at a time. The following example
prints the same page, but uses the function-oriented interface.
The main differences are that we now need to import a set of functions
into our name space (usually the \*(L"standard\*(R" functions), and we don't
need to create the \s-1CGI\s0 object.
.PP
.Vb 6
\& #!/usr/local/bin/perl
\& use CGI qw/:standard/; # load standard CGI routines
\& print header, # create the HTTP header
\& start_html('hello world'), # start the HTML
\& h1('hello world'), # level 1 header
\& end_html; # end the HTML
.Ve
.PP
The examples in this document mainly use the object-oriented style.
See \s-1HOW\s0 \s-1TO\s0 \s-1IMPORT\s0 \s-1FUNCTIONS\s0 for important information on
function-oriented programming in \s-1CGI\s0.pm
.Sh "\s-1CALLING\s0 \s-1CGI\s0.PM \s-1ROUTINES\s0"
.IX Subsection "CALLING CGI.PM ROUTINES"
Most \s-1CGI\s0.pm routines accept several arguments, sometimes as many as 20
optional ones! To simplify this interface, all routines use a named
argument calling style that looks like this:
.PP
.Vb 1
\& print $q->header(-type=>'image/gif',-expires=>'+3d');
.Ve
.PP
Each argument name is preceded by a dash. Neither case nor order
matters in the argument list. \-type, \-Type, and \-TYPE are all
acceptable. In fact, only the first argument needs to begin with a
dash. If a dash is present in the first argument, \s-1CGI\s0.pm assumes
dashes for the subsequent ones.
.PP
Several routines are commonly called with just one argument. In the
case of these routines you can provide the single argument without an
argument name. \fIheader()\fR happens to be one of these routines. In this
case, the single argument is the document type.
.PP
.Vb 1
\& print $q->header('text/html');
.Ve
.PP
Other such routines are documented below.
.PP
Sometimes named arguments expect a scalar, sometimes a reference to an
array, and sometimes a reference to a hash. Often, you can pass any
type of argument and the routine will do whatever is most appropriate.
For example, the \fIparam()\fR routine is used to set a \s-1CGI\s0 parameter to a
single or a multi-valued value. The two cases are shown below:
.PP
.Vb 2
\& $q->param(-name=>'veggie',-value=>'tomato');
\& $q->param(-name=>'veggie',-value=>['tomato','tomahto','potato','potahto']);
.Ve
.PP
A large number of routines in \s-1CGI\s0.pm actually aren't specifically
defined in the module, but are generated automatically as needed.
These are the \*(L"\s-1HTML\s0 shortcuts,\*(R" routines that generate \s-1HTML\s0 tags for
use in dynamically-generated pages. \s-1HTML\s0 tags have both attributes
(the attribute=\*(L"value\*(R" pairs within the tag itself) and contents (the
part between the opening and closing pairs.) To distinguish between
attributes and contents, \s-1CGI\s0.pm uses the convention of passing \s-1HTML\s0
attributes as a hash reference as the first argument, and the
contents, if any, as any subsequent arguments. It works out like
this:
.PP
.Vb 6
\& Code Generated HTML
\& ---- --------------
\& h1()
\& h1('some','contents');
some contents
\& h1({-align=>left});
\& h1({-align=>left},'contents');
contents
.Ve
.PP
\&\s-1HTML\s0 tags are described in more detail later.
.PP
Many newcomers to \s-1CGI\s0.pm are puzzled by the difference between the
calling conventions for the \s-1HTML\s0 shortcuts, which require curly braces
around the \s-1HTML\s0 tag attributes, and the calling conventions for other
routines, which manage to generate attributes without the curly
brackets. Don't be confused. As a convenience the curly braces are
optional in all but the \s-1HTML\s0 shortcuts. If you like, you can use
curly braces when calling any routine that takes named arguments. For
example:
.PP
.Vb 1
\& print $q->header( {-type=>'image/gif',-expires=>'+3d'} );
.Ve
.PP
If you use the \fB\-w\fR switch, you will be warned that some \s-1CGI\s0.pm argument
names conflict with built-in Perl functions. The most frequent of
these is the \-values argument, used to create multi-valued menus,
radio button clusters and the like. To get around this warning, you
have several choices:
.IP "1." 4
Use another name for the argument, if one is available.
For example, \-value is an alias for \-values.
.IP "2." 4
Change the capitalization, e.g. \-Values
.IP "3." 4
Put quotes around the argument name, e.g. '\-values'
.PP
Many routines will do something useful with a named argument that it
doesn't recognize. For example, you can produce non-standard \s-1HTTP\s0
header fields by providing them as named arguments:
.PP
.Vb 4
\& print $q->header(-type => 'text/html',
\& -cost => 'Three smackers',
\& -annoyance_level => 'high',
\& -complaints_to => 'bit bucket');
.Ve
.PP
This will produce the following nonstandard \s-1HTTP\s0 header:
.PP
.Vb 5
\& HTTP/1.0 200 OK
\& Cost: Three smackers
\& Annoyance-level: high
\& Complaints-to: bit bucket
\& Content-type: text/html
.Ve
.PP
Notice the way that underscores are translated automatically into
hyphens. HTML-generating routines perform a different type of
translation.
.PP
This feature allows you to keep up with the rapidly changing \s-1HTTP\s0 and
\&\s-1HTML\s0 \*(L"standards\*(R".
.Sh "\s-1CREATING\s0 A \s-1NEW\s0 \s-1QUERY\s0 \s-1OBJECT\s0 (\s-1OBJECT\-ORIENTED\s0 \s-1STYLE\s0):"
.IX Subsection "CREATING A NEW QUERY OBJECT (OBJECT-ORIENTED STYLE):"
.Vb 1
\& $query = new CGI;
.Ve
.PP
This will parse the input (from both \s-1POST\s0 and \s-1GET\s0 methods) and store
it into a perl5 object called \f(CW$query\fR.
.Sh "\s-1CREATING\s0 A \s-1NEW\s0 \s-1QUERY\s0 \s-1OBJECT\s0 \s-1FROM\s0 \s-1AN\s0 \s-1INPUT\s0 \s-1FILE\s0"
.IX Subsection "CREATING A NEW QUERY OBJECT FROM AN INPUT FILE"
.Vb 1
\& $query = new CGI(INPUTFILE);
.Ve
.PP
If you provide a file handle to the \fInew()\fR method, it will read
parameters from the file (or \s-1STDIN\s0, or whatever). The file can be in
any of the forms describing below under debugging (i.e. a series of
newline delimited TAG=VALUE pairs will work). Conveniently, this type
of file is created by the \fIsave()\fR method (see below). Multiple records
can be saved and restored.
.PP
Perl purists will be pleased to know that this syntax accepts
references to file handles, or even references to filehandle globs,
which is the \*(L"official\*(R" way to pass a filehandle:
.PP
.Vb 1
\& $query = new CGI(\e*STDIN);
.Ve
.PP
You can also initialize the \s-1CGI\s0 object with a FileHandle or IO::File
object.
.PP
If you are using the function-oriented interface and want to
initialize \s-1CGI\s0 state from a file handle, the way to do this is with
\&\fB\f(BIrestore_parameters()\fB\fR. This will (re)initialize the
default \s-1CGI\s0 object from the indicated file handle.
.PP
.Vb 3
\& open (IN,"test.in") || die;
\& restore_parameters(IN);
\& close IN;
.Ve
.PP
You can also initialize the query object from an associative array
reference:
.PP
.Vb 4
\& $query = new CGI( {'dinosaur'=>'barney',
\& 'song'=>'I love you',
\& 'friends'=>[qw/Jessica George Nancy/]}
\& );
.Ve
.PP
or from a properly formatted, URL-escaped query string:
.PP
.Vb 1
\& $query = new CGI('dinosaur=barney&color=purple');
.Ve
.PP
or from a previously existing \s-1CGI\s0 object (currently this clones the
parameter list, but none of the other object-specific fields, such as
autoescaping):
.PP
.Vb 2
\& $old_query = new CGI;
\& $new_query = new CGI($old_query);
.Ve
.PP
To create an empty query, initialize it from an empty string or hash:
.PP
.Vb 1
\& $empty_query = new CGI("");
.Ve
.PP
.Vb 1
\& -or-
.Ve
.PP
.Vb 1
\& $empty_query = new CGI({});
.Ve
.Sh "\s-1FETCHING\s0 A \s-1LIST\s0 \s-1OF\s0 \s-1KEYWORDS\s0 \s-1FROM\s0 \s-1THE\s0 \s-1QUERY:\s0"
.IX Subsection "FETCHING A LIST OF KEYWORDS FROM THE QUERY:"
.Vb 1
\& @keywords = $query->keywords
.Ve
.PP
If the script was invoked as the result of an <\s-1ISINDEX\s0> search, the
parsed keywords can be obtained as an array using the \fIkeywords()\fR method.
.Sh "\s-1FETCHING\s0 \s-1THE\s0 \s-1NAMES\s0 \s-1OF\s0 \s-1ALL\s0 \s-1THE\s0 \s-1PARAMETERS\s0 \s-1PASSED\s0 \s-1TO\s0 \s-1YOUR\s0 \s-1SCRIPT:\s0"
.IX Subsection "FETCHING THE NAMES OF ALL THE PARAMETERS PASSED TO YOUR SCRIPT:"
.Vb 1
\& @names = $query->param
.Ve
.PP
If the script was invoked with a parameter list
(e.g. \*(L"name1=value1&name2=value2&name3=value3\*(R"), the \fIparam()\fR method
will return the parameter names as a list. If the script was invoked
as an <\s-1ISINDEX\s0> script and contains a string without ampersands
(e.g. \*(L"value1+value2+value3\*(R") , there will be a single parameter named
\&\*(L"keywords\*(R" containing the \*(L"+\*(R"\-delimited keywords.
.PP
\&\s-1NOTE:\s0 As of version 1.5, the array of parameter names returned will
be in the same order as they were submitted by the browser.
Usually this order is the same as the order in which the
parameters are defined in the form (however, this isn't part
of the spec, and so isn't guaranteed).
.Sh "\s-1FETCHING\s0 \s-1THE\s0 \s-1VALUE\s0 \s-1OR\s0 \s-1VALUES\s0 \s-1OF\s0 A \s-1SINGLE\s0 \s-1NAMED\s0 \s-1PARAMETER:\s0"
.IX Subsection "FETCHING THE VALUE OR VALUES OF A SINGLE NAMED PARAMETER:"
.Vb 1
\& @values = $query->param('foo');
.Ve
.PP
.Vb 1
\& -or-
.Ve
.PP
.Vb 1
\& $value = $query->param('foo');
.Ve
.PP
Pass the \fIparam()\fR method a single argument to fetch the value of the
named parameter. If the parameter is multivalued (e.g. from multiple
selections in a scrolling list), you can ask to receive an array. Otherwise
the method will return a single value.
.PP
If a value is not given in the query string, as in the queries
\&\*(L"name1=&name2=\*(R" or \*(L"name1&name2\*(R", it will be returned as an empty
string. This feature is new in 2.63.
.PP
If the parameter does not exist at all, then \fIparam()\fR will return undef
in a scalar context, and the empty list in a list context.
.Sh "\s-1SETTING\s0 \s-1THE\s0 \s-1VALUE\s0(S) \s-1OF\s0 A \s-1NAMED\s0 \s-1PARAMETER:\s0"
.IX Subsection "SETTING THE VALUE(S) OF A NAMED PARAMETER:"
.Vb 1
\& $query->param('foo','an','array','of','values');
.Ve
.PP
This sets the value for the named parameter 'foo' to an array of
values. This is one way to change the value of a field \s-1AFTER\s0
the script has been invoked once before. (Another way is with
the \-override parameter accepted by all methods that generate
form elements.)
.PP
\&\fIparam()\fR also recognizes a named parameter style of calling described
in more detail later:
.PP
.Vb 1
\& $query->param(-name=>'foo',-values=>['an','array','of','values']);
.Ve
.PP
.Vb 1
\& -or-
.Ve
.PP
.Vb 1
\& $query->param(-name=>'foo',-value=>'the value');
.Ve
.Sh "\s-1APPENDING\s0 \s-1ADDITIONAL\s0 \s-1VALUES\s0 \s-1TO\s0 A \s-1NAMED\s0 \s-1PARAMETER:\s0"
.IX Subsection "APPENDING ADDITIONAL VALUES TO A NAMED PARAMETER:"
.Vb 1
\& $query->append(-name=>'foo',-values=>['yet','more','values']);
.Ve
.PP
This adds a value or list of values to the named parameter. The
values are appended to the end of the parameter if it already exists.
Otherwise the parameter is created. Note that this method only
recognizes the named argument calling syntax.
.Sh "\s-1IMPORTING\s0 \s-1ALL\s0 \s-1PARAMETERS\s0 \s-1INTO\s0 A \s-1NAMESPACE:\s0"
.IX Subsection "IMPORTING ALL PARAMETERS INTO A NAMESPACE:"
.Vb 1
\& $query->import_names('R');
.Ve
.PP
This creates a series of variables in the 'R' namespace. For example,
\&\f(CW$R::foo\fR, \f(CW@R:foo\fR. For keyword lists, a variable \f(CW@R::keywords\fR will appear.
If no namespace is given, this method will assume 'Q'.
\&\s-1WARNING:\s0 don't import anything into 'main'; this is a major security
risk!!!!
.PP
\&\s-1NOTE\s0 1: Variable names are transformed as necessary into legal Perl
variable names. All non-legal characters are transformed into
underscores. If you need to keep the original names, you should use
the \fIparam()\fR method instead to access \s-1CGI\s0 variables by name.
.PP
\&\s-1NOTE\s0 2: In older versions, this method was called \fB\f(BIimport()\fB\fR. As of version 2.20,
this name has been removed completely to avoid conflict with the built-in
Perl module \fBimport\fR operator.
.Sh "\s-1DELETING\s0 A \s-1PARAMETER\s0 \s-1COMPLETELY:\s0"
.IX Subsection "DELETING A PARAMETER COMPLETELY:"
.Vb 1
\& $query->delete('foo','bar','baz');
.Ve
.PP
This completely clears a list of parameters. It sometimes useful for
resetting parameters that you don't want passed down between script
invocations.
.PP
If you are using the function call interface, use \*(L"\fIDelete()\fR\*(R" instead
to avoid conflicts with Perl's built-in delete operator.
.Sh "\s-1DELETING\s0 \s-1ALL\s0 \s-1PARAMETERS:\s0"
.IX Subsection "DELETING ALL PARAMETERS:"
.Vb 1
\& $query->delete_all();
.Ve
.PP
This clears the \s-1CGI\s0 object completely. It might be useful to ensure
that all the defaults are taken when you create a fill-out form.
.PP
Use \fIDelete_all()\fR instead if you are using the function call interface.
.Sh "\s-1HANDLING\s0 NON-URLENCODED \s-1ARGUMENTS\s0"
.IX Subsection "HANDLING NON-URLENCODED ARGUMENTS"
If POSTed data is not of type application/x\-www\-form\-urlencoded or
multipart/form\-data, then the POSTed data will not be processed, but
instead be returned as-is in a parameter named \s-1POSTDATA\s0. To retrieve
it, use code like this:
.PP
.Vb 1
\& my $data = $query->param('POSTDATA');
.Ve
.PP
(If you don't know what the preceding means, don't worry about it. It
only affects people trying to use \s-1CGI\s0 for \s-1XML\s0 processing and other
specialized tasks.)
.Sh "\s-1DIRECT\s0 \s-1ACCESS\s0 \s-1TO\s0 \s-1THE\s0 \s-1PARAMETER\s0 \s-1LIST:\s0"
.IX Subsection "DIRECT ACCESS TO THE PARAMETER LIST:"
.Vb 2
\& $q->param_fetch('address')->[1] = '1313 Mockingbird Lane';
\& unshift @{$q->param_fetch(-name=>'address')},'George Munster';
.Ve
.PP
If you need access to the parameter list in a way that isn't covered
by the methods above, you can obtain a direct reference to it by
calling the \fB\f(BIparam_fetch()\fB\fR method with the name of the . This
will return an array reference to the named parameters, which you then
can manipulate in any way you like.
.PP
You can also use a named argument style using the \fB\-name\fR argument.
.Sh "\s-1FETCHING\s0 \s-1THE\s0 \s-1PARAMETER\s0 \s-1LIST\s0 \s-1AS\s0 A \s-1HASH:\s0"
.IX Subsection "FETCHING THE PARAMETER LIST AS A HASH:"
.Vb 4
\& $params = $q->Vars;
\& print $params->{'address'};
\& @foo = split("\e0",$params->{'foo'});
\& %params = $q->Vars;
.Ve
.PP
.Vb 2
\& use CGI ':cgi-lib';
\& $params = Vars;
.Ve
.PP
Many people want to fetch the entire parameter list as a hash in which
the keys are the names of the \s-1CGI\s0 parameters, and the values are the
parameters' values. The \fIVars()\fR method does this. Called in a scalar
context, it returns the parameter list as a tied hash reference.
Changing a key changes the value of the parameter in the underlying
\&\s-1CGI\s0 parameter list. Called in a list context, it returns the
parameter list as an ordinary hash. This allows you to read the
contents of the parameter list, but not to change it.
.PP
When using this, the thing you must watch out for are multivalued \s-1CGI\s0
parameters. Because a hash cannot distinguish between scalar and
list context, multivalued parameters will be returned as a packed
string, separated by the \*(L"\e0\*(R" (null) character. You must split this
packed string in order to get at the individual values. This is the
convention introduced long ago by Steve Brenner in his cgi\-lib.pl
module for Perl version 4.
.PP
If you wish to use \fIVars()\fR as a function, import the \fI:cgi\-lib\fR set of
function calls (also see the section on CGI-LIB compatibility).
.Sh "\s-1SAVING\s0 \s-1THE\s0 \s-1STATE\s0 \s-1OF\s0 \s-1THE\s0 \s-1SCRIPT\s0 \s-1TO\s0 A \s-1FILE:\s0"
.IX Subsection "SAVING THE STATE OF THE SCRIPT TO A FILE:"
.Vb 1
\& $query->save(\e*FILEHANDLE)
.Ve
.PP
This will write the current state of the form to the provided
filehandle. You can read it back in by providing a filehandle
to the \fInew()\fR method. Note that the filehandle can be a file, a pipe,
or whatever!
.PP
The format of the saved file is:
.PP
.Vb 5
\& NAME1=VALUE1
\& NAME1=VALUE1'
\& NAME2=VALUE2
\& NAME3=VALUE3
\& =
.Ve
.PP
Both name and value are \s-1URL\s0 escaped. Multi-valued \s-1CGI\s0 parameters are
represented as repeated names. A session record is delimited by a
single = symbol. You can write out multiple records and read them
back in with several calls to \fBnew\fR. You can do this across several
sessions by opening the file in append mode, allowing you to create
primitive guest books, or to keep a history of users' queries. Here's
a short example of creating multiple session records:
.PP
.Vb 1
\& use CGI;
.Ve
.PP
.Vb 8
\& open (OUT,">>test.out") || die;
\& $records = 5;
\& foreach (0..$records) {
\& my $q = new CGI;
\& $q->param(-name=>'counter',-value=>$_);
\& $q->save(\e*OUT);
\& }
\& close OUT;
.Ve
.PP
.Vb 6
\& # reopen for reading
\& open (IN,"test.out") || die;
\& while (!eof(IN)) {
\& my $q = new CGI(\e*IN);
\& print $q->param('counter'),"\en";
\& }
.Ve
.PP
The file format used for save/restore is identical to that used by the
Whitehead Genome Center's data exchange format \*(L"Boulderio\*(R", and can be
manipulated and even databased using Boulderio utilities. See
.PP
.Vb 1
\& http://stein.cshl.org/boulder/
.Ve
.PP
for further details.
.PP
If you wish to use this method from the function-oriented (non\-OO)
interface, the exported name for this method is \fB\f(BIsave_parameters()\fB\fR.
.Sh "\s-1RETRIEVING\s0 \s-1CGI\s0 \s-1ERRORS\s0"
.IX Subsection "RETRIEVING CGI ERRORS"
Errors can occur while processing user input, particularly when
processing uploaded files. When these errors occur, \s-1CGI\s0 will stop
processing and return an empty parameter list. You can test for
the existence and nature of errors using the \fI\fIcgi_error()\fI\fR function.
The error messages are formatted as \s-1HTTP\s0 status codes. You can either
incorporate the error text into an \s-1HTML\s0 page, or use it as the value
of the \s-1HTTP\s0 status:
.PP
.Vb 8
\& my $error = $q->cgi_error;
\& if ($error) {
\& print $q->header(-status=>$error),
\& $q->start_html('Problems'),
\& $q->h2('Request not processed'),
\& $q->strong($error);
\& exit 0;
\& }
.Ve
.PP
When using the function-oriented interface (see the next section),
errors may only occur the first time you call \fI\fIparam()\fI\fR. Be ready
for this!
.Sh "\s-1USING\s0 \s-1THE\s0 FUNCTION-ORIENTED \s-1INTERFACE\s0"
.IX Subsection "USING THE FUNCTION-ORIENTED INTERFACE"
To use the function-oriented interface, you must specify which \s-1CGI\s0.pm
routines or sets of routines to import into your script's namespace.
There is a small overhead associated with this importation, but it
isn't much.
.PP
.Vb 1
\& use CGI ;
.Ve
.PP
The listed methods will be imported into the current package; you can
call them directly without creating a \s-1CGI\s0 object first. This example
shows how to import the \fB\f(BIparam()\fB\fR and \fB\f(BIheader()\fB\fR
methods, and then use them directly:
.PP
.Vb 3
\& use CGI 'param','header';
\& print header('text/plain');
\& $zipcode = param('zipcode');
.Ve
.PP
More frequently, you'll import common sets of functions by referring
to the groups by name. All function sets are preceded with a \*(L":\*(R"
character as in \*(L":html3\*(R" (for tags defined in the \s-1HTML\s0 3 standard).
.PP
Here is a list of the function sets you can import:
.IP "\fB:cgi\fR" 4
.IX Item ":cgi"
Import all CGI-handling methods, such as \fB\f(BIparam()\fB\fR, \fB\f(BIpath_info()\fB\fR
and the like.
.IP "\fB:form\fR" 4
.IX Item ":form"
Import all fill-out form generating methods, such as \fB\f(BItextfield()\fB\fR.
.IP "\fB:html2\fR" 4
.IX Item ":html2"
Import all methods that generate \s-1HTML\s0 2.0 standard elements.
.IP "\fB:html3\fR" 4
.IX Item ":html3"
Import all methods that generate \s-1HTML\s0 3.0 elements (such as
, and ).
.IP "\fB:html4\fR" 4
.IX Item ":html4"
Import all methods that generate \s-1HTML\s0 4 elements (such as
, and ).
.IP "\fB:netscape\fR" 4
.IX Item ":netscape"
Import all methods that generate Netscape-specific \s-1HTML\s0 extensions.
.IP "\fB:html\fR" 4
.IX Item ":html"
Import all HTML-generating shortcuts (i.e. 'html2' + 'html3' +
\&'netscape')...
.IP "\fB:standard\fR" 4
.IX Item ":standard"
Import \*(L"standard\*(R" features, 'html2', 'html3', 'html4', 'form' and 'cgi'.
.IP "\fB:all\fR" 4
.IX Item ":all"
Import all the available methods. For the full list, see the \s-1CGI\s0.pm
code, where the variable \f(CW%EXPORT_TAGS\fR is defined.
.PP
If you import a function name that is not part of \s-1CGI\s0.pm, the module
will treat it as a new \s-1HTML\s0 tag and generate the appropriate
subroutine. You can then use it like any other \s-1HTML\s0 tag. This is to
provide for the rapidly-evolving \s-1HTML\s0 \*(L"standard.\*(R" For example, say
Microsoft comes out with a new tag called (which causes the
user's desktop to be flooded with a rotating gradient fill until his
machine reboots). You don't need to wait for a new version of \s-1CGI\s0.pm
to start using it immediately:
.PP
.Vb 2
\& use CGI qw/:standard :html3 gradient/;
\& print gradient({-start=>'red',-end=>'blue'});
.Ve
.PP
Note that in the interests of execution speed \s-1CGI\s0.pm does \fBnot\fR use
the standard Exporter syntax for specifying load symbols. This may
change in the future.
.PP
If you import any of the state-maintaining \s-1CGI\s0 or form-generating
methods, a default \s-1CGI\s0 object will be created and initialized
automatically the first time you use any of the methods that require
one to be present. This includes \fB\f(BIparam()\fB\fR, \fB\f(BItextfield()\fB\fR,
\&\fB\f(BIsubmit()\fB\fR and the like. (If you need direct access to the \s-1CGI\s0
object, you can find it in the global variable \fB$CGI::Q\fR). By
importing \s-1CGI\s0.pm methods, you can create visually elegant scripts:
.PP
.Vb 17
\& use CGI qw/:standard/;
\& print
\& header,
\& start_html('Simple Script'),
\& h1('Simple Script'),
\& start_form,
\& "What's your name? ",textfield('name'),p,
\& "What's the combination?",
\& checkbox_group(-name=>'words',
\& -values=>['eenie','meenie','minie','moe'],
\& -defaults=>['eenie','moe']),p,
\& "What's your favorite color?",
\& popup_menu(-name=>'color',
\& -values=>['red','green','blue','chartreuse']),p,
\& submit,
\& end_form,
\& hr,"\en";
.Ve
.PP
.Vb 7
\& if (param) {
\& print
\& "Your name is ",em(param('name')),p,
\& "The keywords are: ",em(join(", ",param('words'))),p,
\& "Your favorite color is ",em(param('color')),".\en";
\& }
\& print end_html;
.Ve
.Sh "\s-1PRAGMAS\s0"
.IX Subsection "PRAGMAS"
In addition to the function sets, there are a number of pragmas that
you can import. Pragmas, which are always preceded by a hyphen,
change the way that \s-1CGI\s0.pm functions in various ways. Pragmas,
function sets, and individual functions can all be imported in the
same \fIuse()\fR line. For example, the following use statement imports the
standard set of functions and enables debugging mode (pragma
\&\-debug):
.PP
.Vb 1
\& use CGI qw/:standard -debug/;
.Ve
.PP
The current list of pragmas is as follows:
.IP "\-any" 4
.IX Item "-any"
When you \fIuse \s-1CGI\s0 \-any\fR, then any method that the query object
doesn't recognize will be interpreted as a new \s-1HTML\s0 tag. This allows
you to support the next \fIad hoc\fR Netscape or Microsoft \s-1HTML\s0
extension. This lets you go wild with new and unsupported tags:
.Sp
.Vb 3
\& use CGI qw(-any);
\& $q=new CGI;
\& print $q->gradient({speed=>'fast',start=>'red',end=>'blue'});
.Ve
.Sp
Since using any causes any mistyped method name
to be interpreted as an \s-1HTML\s0 tag, use it with care or not at
all.
.IP "\-compile" 4
.IX Item "-compile"
This causes the indicated autoloaded methods to be compiled up front,
rather than deferred to later. This is useful for scripts that run
for an extended period of time under FastCGI or mod_perl, and for
those destined to be crunched by Malcom Beattie's Perl compiler. Use
it in conjunction with the methods or method families you plan to use.
.Sp
.Vb 1
\& use CGI qw(-compile :standard :html3);
.Ve
.Sp
or even
.Sp
.Vb 1
\& use CGI qw(-compile :all);
.Ve
.Sp
Note that using the \-compile pragma in this way will always have
the effect of importing the compiled functions into the current
namespace. If you want to compile without importing use the
\&\fIcompile()\fR method instead:
.Sp
.Vb 2
\& use CGI();
\& CGI->compile();
.Ve
.Sp
This is particularly useful in a mod_perl environment, in which you
might want to precompile all \s-1CGI\s0 routines in a startup script, and
then import the functions individually in each mod_perl script.
.IP "\-nosticky" 4
.IX Item "-nosticky"
By default the \s-1CGI\s0 module implements a state-preserving behavior
called \*(L"sticky\*(R" fields. The way this works is that if you are
regenerating a form, the methods that generate the form field values
will interrogate \fIparam()\fR to see if similarly-named parameters are
present in the query string. If they find a like-named parameter, they
will use it to set their default values.
.Sp
Sometimes this isn't what you want. The \fB\-nosticky\fR pragma prevents
this behavior. You can also selectively change the sticky behavior in
each element that you generate.
.IP "\-tabindex" 4
.IX Item "-tabindex"
Automatically add tab index attributes to each form field. With this
option turned off, you can still add tab indexes manually by passing a
\&\-tabindex option to each field-generating method.
.IP "\-no_undef_params" 4
.IX Item "-no_undef_params"
This keeps \s-1CGI\s0.pm from including undef params in the parameter list.
.IP "\-no_xhtml" 4
.IX Item "-no_xhtml"
By default, \s-1CGI\s0.pm versions 2.69 and higher emit \s-1XHTML\s0
(http://www.w3.org/TR/xhtml1/). The \-no_xhtml pragma disables this
feature. Thanks to Michalis Kabrianis for this
feature.
.Sp
If \fIstart_html()\fR's \-dtd parameter specifies an \s-1HTML\s0 2.0 or 3.2 \s-1DTD\s0,
\&\s-1XHTML\s0 will automatically be disabled without needing to use this
pragma.
.IP "\-nph" 4
.IX Item "-nph"
This makes \s-1CGI\s0.pm produce a header appropriate for an \s-1NPH\s0 (no
parsed header) script. You may need to do other things as well
to tell the server that the script is \s-1NPH\s0. See the discussion
of \s-1NPH\s0 scripts below.
.IP "\-newstyle_urls" 4
.IX Item "-newstyle_urls"
Separate the name=value pairs in \s-1CGI\s0 parameter query strings with
semicolons rather than ampersands. For example:
.Sp
.Vb 1
\& ?name=fred;age=24;favorite_color=3
.Ve
.Sp
Semicolon-delimited query strings are always accepted, but will not be
emitted by \fIself_url()\fR and \fIquery_string()\fR unless the \-newstyle_urls
pragma is specified.
.Sp
This became the default in version 2.64.
.IP "\-oldstyle_urls" 4
.IX Item "-oldstyle_urls"
Separate the name=value pairs in \s-1CGI\s0 parameter query strings with
ampersands rather than semicolons. This is no longer the default.
.IP "\-autoload" 4
.IX Item "-autoload"
This overrides the autoloader so that any function in your program
that is not recognized is referred to \s-1CGI\s0.pm for possible evaluation.
This allows you to use all the \s-1CGI\s0.pm functions without adding them to
your symbol table, which is of concern for mod_perl users who are
worried about memory consumption. \fIWarning:\fR when
\&\fI\-autoload\fR is in effect, you cannot use \*(L"poetry mode\*(R"
(functions without the parenthesis). Use \fI\fIhr()\fI\fR rather
than \fIhr\fR, or add something like \fIuse subs qw/hr p header/\fR
to the top of your script.
.IP "\-no_debug" 4
.IX Item "-no_debug"
This turns off the command-line processing features. If you want to
run a \s-1CGI\s0.pm script from the command line to produce \s-1HTML\s0, and you
don't want it to read \s-1CGI\s0 parameters from the command line or \s-1STDIN\s0,
then use this pragma:
.Sp
.Vb 1
\& use CGI qw(-no_debug :standard);
.Ve
.IP "\-debug" 4
.IX Item "-debug"
This turns on full debugging. In addition to reading \s-1CGI\s0 arguments
from the command-line processing, \s-1CGI\s0.pm will pause and try to read
arguments from \s-1STDIN\s0, producing the message \*(L"(offline mode: enter
name=value pairs on standard input)\*(R" features.
.Sp
See the section on debugging for more details.
.IP "\-private_tempfiles" 4
.IX Item "-private_tempfiles"
\&\s-1CGI\s0.pm can process uploaded file. Ordinarily it spools the uploaded
file to a temporary directory, then deletes the file when done.
However, this opens the risk of eavesdropping as described in the file
upload section. Another \s-1CGI\s0 script author could peek at this data
during the upload, even if it is confidential information. On Unix
systems, the \-private_tempfiles pragma will cause the temporary file
to be unlinked as soon as it is opened and before any data is written
into it, reducing, but not eliminating the risk of eavesdropping
(there is still a potential race condition). To make life harder for
the attacker, the program chooses tempfile names by calculating a 32
bit checksum of the incoming \s-1HTTP\s0 headers.
.Sp
To ensure that the temporary file cannot be read by other \s-1CGI\s0 scripts,
use suEXEC or a \s-1CGI\s0 wrapper program to run your script. The temporary
file is created with mode 0600 (neither world nor group readable).
.Sp
The temporary directory is selected using the following algorithm:
.Sp
.Vb 2
\& 1. if the current user (e.g. "nobody") has a directory named
\& "tmp" in its home directory, use that (Unix systems only).
.Ve
.Sp
.Vb 2
\& 2. if the environment variable TMPDIR exists, use the location
\& indicated.
.Ve
.Sp
.Vb 2
\& 3. Otherwise try the locations /usr/tmp, /var/tmp, C:\etemp,
\& /tmp, /temp, ::Temporary Items, and \eWWW_ROOT.
.Ve
.Sp
Each of these locations is checked that it is a directory and is
writable. If not, the algorithm tries the next choice.
.Sh "\s-1SPECIAL\s0 \s-1FORMS\s0 \s-1FOR\s0 \s-1IMPORTING\s0 HTML-TAG \s-1FUNCTIONS\s0"
.IX Subsection "SPECIAL FORMS FOR IMPORTING HTML-TAG FUNCTIONS"
Many of the methods generate \s-1HTML\s0 tags. As described below, tag
functions automatically generate both the opening and closing tags.
For example:
.PP
.Vb 1
\& print h1('Level 1 Header');
.Ve
.PP
produces
.PP
.Vb 1
\&
Level 1 Header
.Ve
.PP
There will be some times when you want to produce the start and end
tags yourself. In this case, you can use the form start_\fItag_name\fR
and end_\fItag_name\fR, as in:
.PP
.Vb 1
\& print start_h1,'Level 1 Header',end_h1;
.Ve
.PP
With a few exceptions (described below), start_\fItag_name\fR and
end_\fItag_name\fR functions are not generated automatically when you
\&\fIuse \s-1CGI\s0\fR. However, you can specify the tags you want to generate
\&\fIstart/end\fR functions for by putting an asterisk in front of their
name, or, alternatively, requesting either "start_\fItag_name\fR\*(L" or
\&\*(R"end_\fItag_name\fR" in the import list.
.PP
Example:
.PP
.Vb 1
\& use CGI qw/:standard *table start_ul/;
.Ve
.PP
In this example, the following functions are generated in addition to
the standard ones:
.IP "1. \fIstart_table()\fR (generates a
tag)" 4
.IX Item "1. start_table() (generates a
tag)"
.PD 0
.IP "2. \fIend_table()\fR (generates a
tag)" 4
.IX Item "2. end_table() (generates a
tag)"
.IP "3. \fIstart_ul()\fR (generates a
tag)" 4
.IX Item "3. start_ul() (generates a
tag)"
.IP "4. \fIend_ul()\fR (generates a
tag)" 4
.IX Item "4. end_ul() (generates a
tag)"
.PD
.SH "GENERATING DYNAMIC DOCUMENTS"
.IX Header "GENERATING DYNAMIC DOCUMENTS"
Most of \s-1CGI\s0.pm's functions deal with creating documents on the fly.
Generally you will produce the \s-1HTTP\s0 header first, followed by the
document itself. \s-1CGI\s0.pm provides functions for generating \s-1HTTP\s0
headers of various types as well as for generating \s-1HTML\s0. For creating
\&\s-1GIF\s0 images, see the \s-1GD\s0.pm module.
.PP
Each of these functions produces a fragment of \s-1HTML\s0 or \s-1HTTP\s0 which you
can print out directly so that it displays in the browser window,
append to a string, or save to a file for later use.
.Sh "\s-1CREATING\s0 A \s-1STANDARD\s0 \s-1HTTP\s0 \s-1HEADER:\s0"
.IX Subsection "CREATING A STANDARD HTTP HEADER:"
Normally the first thing you will do in any \s-1CGI\s0 script is print out an
\&\s-1HTTP\s0 header. This tells the browser what type of document to expect,
and gives other optional information, such as the language, expiration
date, and whether to cache the document. The header can also be
manipulated for special purposes, such as server push and pay per view
pages.
.PP
.Vb 1
\& print header;
.Ve
.PP
.Vb 1
\& -or-
.Ve
.PP
.Vb 1
\& print header('image/gif');
.Ve
.PP
.Vb 1
\& -or-
.Ve
.PP
.Vb 1
\& print header('text/html','204 No response');
.Ve
.PP
.Vb 1
\& -or-
.Ve
.PP
.Vb 8
\& print header(-type=>'image/gif',
\& -nph=>1,
\& -status=>'402 Payment required',
\& -expires=>'+3d',
\& -cookie=>$cookie,
\& -charset=>'utf-7',
\& -attachment=>'foo.gif',
\& -Cost=>'$2.00');
.Ve
.PP
\&\fIheader()\fR returns the Content\-type: header. You can provide your own
\&\s-1MIME\s0 type if you choose, otherwise it defaults to text/html. An
optional second parameter specifies the status code and a human-readable
message. For example, you can specify 204, \*(L"No response\*(R" to create a
script that tells the browser to do nothing at all.
.PP
The last example shows the named argument style for passing arguments
to the \s-1CGI\s0 methods using named parameters. Recognized parameters are
\&\fB\-type\fR, \fB\-status\fR, \fB\-expires\fR, and \fB\-cookie\fR. Any other named
parameters will be stripped of their initial hyphens and turned into
header fields, allowing you to specify any \s-1HTTP\s0 header you desire.
Internal underscores will be turned into hyphens:
.PP
.Vb 1
\& print header(-Content_length=>3002);
.Ve
.PP
Most browsers will not cache the output from \s-1CGI\s0 scripts. Every time
the browser reloads the page, the script is invoked anew. You can
change this behavior with the \fB\-expires\fR parameter. When you specify
an absolute or relative expiration interval with this parameter, some
browsers and proxy servers will cache the script's output until the
indicated expiration date. The following forms are all valid for the
\&\-expires field:
.PP
.Vb 8
\& +30s 30 seconds from now
\& +10m ten minutes from now
\& +1h one hour from now
\& -1d yesterday (i.e. "ASAP!")
\& now immediately
\& +3M in three months
\& +10y in ten years time
\& Thursday, 25-Apr-1999 00:40:33 GMT at the indicated time & date
.Ve
.PP
The \fB\-cookie\fR parameter generates a header that tells the browser to provide
a \*(L"magic cookie\*(R" during all subsequent transactions with your script.
Netscape cookies have a special format that includes interesting attributes
such as expiration time. Use the \fIcookie()\fR method to create and retrieve
session cookies.
.PP
The \fB\-nph\fR parameter, if set to a true value, will issue the correct
headers to work with a \s-1NPH\s0 (no\-parse\-header) script. This is important
to use with certain servers that expect all their scripts to be \s-1NPH\s0.
.PP
The \fB\-charset\fR parameter can be used to control the character set
sent to the browser. If not provided, defaults to \s-1ISO\-8859\-1\s0. As a
side effect, this sets the \fIcharset()\fR method as well.
.PP
The \fB\-attachment\fR parameter can be used to turn the page into an
attachment. Instead of displaying the page, some browsers will prompt
the user to save it to disk. The value of the argument is the
suggested name for the saved file. In order for this to work, you may
have to set the \fB\-type\fR to \*(L"application/octet\-stream\*(R".
.PP
The \fB\-p3p\fR parameter will add a P3P tag to the outgoing header. The
parameter can be an arrayref or a space-delimited string of P3P tags.
For example:
.PP
.Vb 2
\& print header(-p3p=>[qw(CAO DSP LAW CURa)]);
\& print header(-p3p=>'CAO DSP LAW CURa');
.Ve
.PP
In either case, the outgoing header will be formatted as:
.PP
.Vb 1
\& P3P: policyref="/w3c/p3p.xml" cp="CAO DSP LAW CURa"
.Ve
.Sh "\s-1GENERATING\s0 A \s-1REDIRECTION\s0 \s-1HEADER\s0"
.IX Subsection "GENERATING A REDIRECTION HEADER"
.Vb 1
\& print redirect('http://somewhere.else/in/movie/land');
.Ve
.PP
Sometimes you don't want to produce a document yourself, but simply
redirect the browser elsewhere, perhaps choosing a \s-1URL\s0 based on the
time of day or the identity of the user.
.PP
The \fIredirect()\fR function redirects the browser to a different \s-1URL\s0. If
you use redirection like this, you should \fBnot\fR print out a header as
well.
.PP
You should always use full URLs (including the http: or ftp: part) in
redirection requests. Relative URLs will not work correctly.
.PP
You can also use named arguments:
.PP
.Vb 3
\& print redirect(-uri=>'http://somewhere.else/in/movie/land',
\& -nph=>1,
\& -status=>301);
.Ve
.PP
The \fB\-nph\fR parameter, if set to a true value, will issue the correct
headers to work with a \s-1NPH\s0 (no\-parse\-header) script. This is important
to use with certain servers, such as Microsoft \s-1IIS\s0, which
expect all their scripts to be \s-1NPH\s0.
.PP
The \fB\-status\fR parameter will set the status of the redirect. \s-1HTTP\s0
defines three different possible redirection status codes:
.PP
.Vb 3
\& 301 Moved Permanently
\& 302 Found
\& 303 See Other
.Ve
.PP
The default if not specified is 302, which means \*(L"moved temporarily.\*(R"
You may change the status to another status code if you wish. Be
advised that changing the status to anything other than 301, 302 or
303 will probably break redirection.
.Sh "\s-1CREATING\s0 \s-1THE\s0 \s-1HTML\s0 \s-1DOCUMENT\s0 \s-1HEADER\s0"
.IX Subsection "CREATING THE HTML DOCUMENT HEADER"
.Vb 8
\& print start_html(-title=>'Secrets of the Pyramids',
\& -author=>'fred@capricorn.org',
\& -base=>'true',
\& -target=>'_blank',
\& -meta=>{'keywords'=>'pharaoh secret mummy',
\& 'copyright'=>'copyright 1996 King Tut'},
\& -style=>{'src'=>'/styles/style1.css'},
\& -BGCOLOR=>'blue');
.Ve
.PP
After creating the \s-1HTTP\s0 header, most \s-1CGI\s0 scripts will start writing
out an \s-1HTML\s0 document. The \fIstart_html()\fR routine creates the top of the
page, along with a lot of optional information that controls the
page's appearance and behavior.
.PP
This method returns a canned \s-1HTML\s0 header and the opening tag.
All parameters are optional. In the named parameter form, recognized
parameters are \-title, \-author, \-base, \-xbase, \-dtd, \-lang and \-target
(see below for the explanation). Any additional parameters you
provide, such as the Netscape unofficial \s-1BGCOLOR\s0 attribute, are added
to the tag. Additional parameters must be proceeded by a
hyphen.
.PP
The argument \fB\-xbase\fR allows you to provide an \s-1HREF\s0 for the tag
different from the current location, as in
.PP
.Vb 1
\& -xbase=>"http://home.mcom.com/"
.Ve
.PP
All relative links will be interpreted relative to this tag.
.PP
The argument \fB\-target\fR allows you to provide a default target frame
for all the links and fill-out forms on the page. \fBThis is a
non-standard \s-1HTTP\s0 feature which only works with Netscape browsers!\fR
See the Netscape documentation on frames for details of how to
manipulate this.
.PP
.Vb 1
\& -target=>"answer_window"
.Ve
.PP
All relative links will be interpreted relative to this tag.
You add arbitrary meta information to the header with the \fB\-meta\fR
argument. This argument expects a reference to an associative array
containing name/value pairs of meta information. These will be turned
into a series of header tags that look something like this:
.PP
.Vb 2
\&
\&
.Ve
.PP
To create an HTTP-EQUIV type of tag, use \fB\-head\fR, described
below.
.PP
The \fB\-style\fR argument is used to incorporate cascading stylesheets
into your code. See the section on \s-1CASCADING\s0 \s-1STYLESHEETS\s0 for more
information.
.PP
The \fB\-lang\fR argument is used to incorporate a language attribute into
the tag. For example:
.PP
.Vb 1
\& print $q->start_html(-lang=>'fr-CA');
.Ve
.PP
The default if not specified is \*(L"en\-US\*(R" for \s-1US\s0 English, unless the
\&\-dtd parameter specifies an \s-1HTML\s0 2.0 or 3.2 \s-1DTD\s0, in which case the
lang attribute is left off. You can force the lang attribute to left
off in other cases by passing an empty string (\-lang=>'').
.PP
The \fB\-encoding\fR argument can be used to specify the character set for
\&\s-1XHTML\s0. It defaults to iso\-8859\-1 if not specified.
.PP
The \fB\-declare_xml\fR argument, when used in conjunction with \s-1XHTML\s0,
will put a declaration at the top of the \s-1HTML\s0 header. The sole
purpose of this declaration is to declare the character set
encoding. In the absence of \-declare_xml, the output \s-1HTML\s0 will contain
a tag that specifies the encoding, allowing the \s-1HTML\s0 to pass
most validators. The default for \-declare_xml is false.
.PP
You can place other arbitrary \s-1HTML\s0 elements to the section with the
\&\fB\-head\fR tag. For example, to place the rarely-used element in the
head section, use this:
.PP
.Vb 2
\& print start_html(-head=>Link({-rel=>'next',
\& -href=>'http://www.capricorn.com/s2.html'}));
.Ve
.PP
To incorporate multiple \s-1HTML\s0 elements into the section, just pass an
array reference:
.PP
.Vb 7
\& print start_html(-head=>[
\& Link({-rel=>'next',
\& -href=>'http://www.capricorn.com/s2.html'}),
\& Link({-rel=>'previous',
\& -href=>'http://www.capricorn.com/s1.html'})
\& ]
\& );
.Ve
.PP
And here's how to create an HTTP-EQUIV tag:
.PP
.Vb 2
\& print start_html(-head=>meta({-http_equiv => 'Content-Type',
\& -content => 'text/html'}))
.Ve
.PP
\&\s-1JAVASCRIPTING:\s0 The \fB\-script\fR, \fB\-noScript\fR, \fB\-onLoad\fR,
\&\fB\-onMouseOver\fR, \fB\-onMouseOut\fR and \fB\-onUnload\fR parameters are used
to add Netscape JavaScript calls to your pages. \fB\-script\fR should
point to a block of text containing JavaScript function definitions.
This block will be placed within a