.\" Automatically generated by Pod::Man 2.25 (Pod::Simple 3.16)
.\"
.\" Standard preamble:
.\" ========================================================================
.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. \*(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-
.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\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.ie \nF \{\
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. nr % 0
. rr F
.\}
.el \{\
. de IX
..
.\}
.\"
.\" 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 3perl"
.TH CGI 3perl "2016-02-27" "perl v5.14.2" "Perl Programmers Reference Guide"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
CGI \- Handle Common Gateway Interface requests and responses
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
.Vb 1
\& use CGI;
\&
\& my $q = CGI\->new;
\&
\& # Process an HTTP request
\& @values = $q\->param(\*(Aqform_field\*(Aq);
\&
\& $fh = $q\->upload(\*(Aqfile_field\*(Aq);
\&
\& $riddle = $query\->cookie(\*(Aqriddle_name\*(Aq);
\& %answers = $query\->cookie(\*(Aqanswers\*(Aq);
\&
\& # Prepare various HTTP responses
\& print $q\->header();
\& print $q\->header(\*(Aqapplication/json\*(Aq);
\&
\& $cookie1 = $q\->cookie(\-name=>\*(Aqriddle_name\*(Aq, \-value=>"The Sphynx\*(Aqs Question");
\& $cookie2 = $q\->cookie(\-name=>\*(Aqanswers\*(Aq, \-value=>\e%answers);
\& print $q\->header(
\& \-type => \*(Aqimage/gif\*(Aq,
\& \-expires => \*(Aq+3d\*(Aq,
\& \-cookie => [$cookie1,$cookie2]
\& );
\&
\& print $q\->redirect(\*(Aqhttp://somewhere.else/in/movie/land\*(Aq);
.Ve
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
\&\s-1CGI\s0.pm is a stable, complete and mature solution for processing and preparing
\&\s-1HTTP\s0 requests and responses. Major features including processing form
submissions, file uploads, reading and writing cookies, query string generation
and manipulation, and processing and preparing \s-1HTTP\s0 headers. Some \s-1HTML\s0
generation utilities are included as well.
.PP
\&\s-1CGI\s0.pm performs very well in in a vanilla \s-1CGI\s0.pm environment and also comes
with built-in support for mod_perl and mod_perl2 as well as FastCGI.
.PP
It has the benefit of having developed and refined over 10 years with input
from dozens of contributors and being deployed on thousands of websites.
\&\s-1CGI\s0.pm has been included in the Perl distribution since Perl 5.4, and has
become a de-facto standard.
.SS "\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 = CGI\->new; # create new CGI object
\& print $q\->header, # create the HTTP header
\& $q\->start_html(\*(Aqhello world\*(Aq), # start the HTML
\& $q\->h1(\*(Aqhello world\*(Aq), # 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(\*(Aqhello world\*(Aq), # start the HTML
\& h1(\*(Aqhello world\*(Aq), # 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
.SS "\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=>\*(Aqimage/gif\*(Aq,\-expires=>\*(Aq+3d\*(Aq);
.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(\*(Aqtext/html\*(Aq);
.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=>\*(Aqveggie\*(Aq,\-value=>\*(Aqtomato\*(Aq);
\& $q\->param(\-name=>\*(Aqveggie\*(Aq,\-value=>[\*(Aqtomato\*(Aq,\*(Aqtomahto\*(Aq,\*(Aqpotato\*(Aq,\*(Aqpotahto\*(Aq]);
.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(\*(Aqsome\*(Aq,\*(Aqcontents\*(Aq);
some contents
\& h1({\-align=>left});
\& h1({\-align=>left},\*(Aqcontents\*(Aq);
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=>\*(Aqimage/gif\*(Aq,\-expires=>\*(Aq+3d\*(Aq} );
.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 => \*(Aqtext/html\*(Aq,
\& \-cost => \*(AqThree smackers\*(Aq,
\& \-annoyance_level => \*(Aqhigh\*(Aq,
\& \-complaints_to => \*(Aqbit bucket\*(Aq);
.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".
.SS "\s-1CREATING\s0 A \s-1NEW\s0 \s-1QUERY\s0 \s-1OBJECT\s0 (OBJECT-ORIENTED \s-1STYLE\s0):"
.IX Subsection "CREATING A NEW QUERY OBJECT (OBJECT-ORIENTED STYLE):"
.Vb 1
\& $query = CGI\->new;
.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.
.PP
Any filehandles from file uploads will have their position reset to
the beginning of the file.
.SS "\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 = CGI\->new(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 = CGI\->new(\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 a hash
reference:
.PP
.Vb 4
\& $query = CGI\->new( {\*(Aqdinosaur\*(Aq=>\*(Aqbarney\*(Aq,
\& \*(Aqsong\*(Aq=>\*(AqI love you\*(Aq,
\& \*(Aqfriends\*(Aq=>[qw/Jessica George Nancy/]}
\& );
.Ve
.PP
or from a properly formatted, URL-escaped query string:
.PP
.Vb 1
\& $query = CGI\->new(\*(Aqdinosaur=barney&color=purple\*(Aq);
.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 = CGI\->new;
\& $new_query = CGI\->new($old_query);
.Ve
.PP
To create an empty query, initialize it from an empty string or hash:
.PP
.Vb 1
\& $empty_query = CGI\->new("");
\&
\& \-or\-
\&
\& $empty_query = CGI\->new({});
.Ve
.SS "\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.
.SS "\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).
.SS "\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(\*(Aqfoo\*(Aq);
\&
\& \-or\-
\&
\& $value = $query\->param(\*(Aqfoo\*(Aq);
.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", it will be returned as an empty string.
.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.
.SS "\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(\*(Aqfoo\*(Aq,\*(Aqan\*(Aq,\*(Aqarray\*(Aq,\*(Aqof\*(Aq,\*(Aqvalues\*(Aq);
.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=>\*(Aqfoo\*(Aq,\-values=>[\*(Aqan\*(Aq,\*(Aqarray\*(Aq,\*(Aqof\*(Aq,\*(Aqvalues\*(Aq]);
\&
\& \-or\-
\&
\& $query\->param(\-name=>\*(Aqfoo\*(Aq,\-value=>\*(Aqthe value\*(Aq);
.Ve
.SS "\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=>\*(Aqfoo\*(Aq,\-values=>[\*(Aqyet\*(Aq,\*(Aqmore\*(Aq,\*(Aqvalues\*(Aq]);
.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.
.SS "\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(\*(AqR\*(Aq);
.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.
.SS "\s-1DELETING\s0 A \s-1PARAMETER\s0 \s-1COMPLETELY:\s0"
.IX Subsection "DELETING A PARAMETER COMPLETELY:"
.Vb 1
\& $query\->delete(\*(Aqfoo\*(Aq,\*(Aqbar\*(Aq,\*(Aqbaz\*(Aq);
.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.
.SS "\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.
.SS "\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(\*(AqPOSTDATA\*(Aq);
.Ve
.PP
Likewise if PUTed data can be retrieved with code like this:
.PP
.Vb 1
\& my $data = $query\->param(\*(AqPUTDATA\*(Aq);
.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.)
.SS "\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(\*(Aqaddress\*(Aq)\->[1] = \*(Aq1313 Mockingbird Lane\*(Aq;
\& unshift @{$q\->param_fetch(\-name=>\*(Aqaddress\*(Aq)},\*(AqGeorge Munster\*(Aq;
.Ve
.PP
If you need access to the parameter list in a way that isn't covered
by the methods given in the previous sections, you can obtain a direct
reference to it by
calling the \fB\f(BIparam_fetch()\fB\fR method with the name of the parameter. This
will return an array reference to the named parameter, 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.
.SS "\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\->{\*(Aqaddress\*(Aq};
\& @foo = split("\e0",$params\->{\*(Aqfoo\*(Aq});
\& %params = $q\->Vars;
\&
\& use CGI \*(Aq:cgi\-lib\*(Aq;
\& $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).
.SS "\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\*(Aq
\& 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;
\&
\& open (OUT,\*(Aq>>\*(Aq,\*(Aqtest.out\*(Aq) || die;
\& $records = 5;
\& for (0..$records) {
\& my $q = CGI\->new;
\& $q\->param(\-name=>\*(Aqcounter\*(Aq,\-value=>$_);
\& $q\->save(\e*OUT);
\& }
\& close OUT;
\&
\& # reopen for reading
\& open (IN,\*(Aq<\*(Aq,\*(Aqtest.out\*(Aq) || die;
\& while (!eof(IN)) {
\& my $q = CGI\->new(\e*IN);
\& print $q\->param(\*(Aqcounter\*(Aq),"\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.
.SS "\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(\*(AqProblems\*(Aq),
\& $q\->h2(\*(AqRequest not processed\*(Aq),
\& $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!
.SS "\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 \*(Aqparam\*(Aq,\*(Aqheader\*(Aq;
\& print header(\*(Aqtext/plain\*(Aq);
\& $zipcode = param(\*(Aqzipcode\*(Aq);
.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 the