.\" 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 , and
tags. .IP "\fB:html\fR" 4 .IX Item ":html" Import all HTML-generating shortcuts (i.e. 'html2', 'html3', 'html4' and '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=>\*(Aqred\*(Aq,\-end=>\*(Aqblue\*(Aq}); .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\f(CB$CGI::Q\fB\fR). By importing \s-1CGI\s0.pm methods, you can create visually elegant scripts: .PP .Vb 10 \& use CGI qw/:standard/; \& print \& header, \& start_html(\*(AqSimple Script\*(Aq), \& h1(\*(AqSimple Script\*(Aq), \& start_form, \& "What\*(Aqs your name? ",textfield(\*(Aqname\*(Aq),p, \& "What\*(Aqs the combination?", \& checkbox_group(\-name=>\*(Aqwords\*(Aq, \& \-values=>[\*(Aqeenie\*(Aq,\*(Aqmeenie\*(Aq,\*(Aqminie\*(Aq,\*(Aqmoe\*(Aq], \& \-defaults=>[\*(Aqeenie\*(Aq,\*(Aqmoe\*(Aq]),p, \& "What\*(Aqs your favorite color?", \& popup_menu(\-name=>\*(Aqcolor\*(Aq, \& \-values=>[\*(Aqred\*(Aq,\*(Aqgreen\*(Aq,\*(Aqblue\*(Aq,\*(Aqchartreuse\*(Aq]),p, \& submit, \& end_form, \& hr,"\en"; \& \& if (param) { \& print \& "Your name is ",em(param(\*(Aqname\*(Aq)),p, \& "The keywords are: ",em(join(", ",param(\*(Aqwords\*(Aq))),p, \& "Your favorite color is ",em(param(\*(Aqcolor\*(Aq)),".\en"; \& } \& print end_html; .Ve .SS "\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 \s-1HTML\s0 extension. This lets you go wild with new and unsupported tags: .Sp .Vb 3 \& use CGI qw(\-any); \& $q=CGI\->new; \& print $q\->gradient({speed=>\*(Aqfast\*(Aq,start=>\*(Aqred\*(Aq,end=>\*(Aqblue\*(Aq}); .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 Malcolm 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, 3.2, 4.0 or 4.01 \s-1DTD\s0, \&\s-1XHTML\s0 will automatically be disabled without needing to use this pragma. .IP "\-utf8" 4 .IX Item "-utf8" This makes \s-1CGI\s0.pm treat all parameters as \s-1UTF\-8\s0 strings. Use this with care, as it will interfere with the processing of binary uploads. It is better to manually select which fields are expected to return utf\-8 strings and convert them using code like this: .Sp .Vb 2 \& use Encode; \& my $arg = decode utf8=>param(\*(Aqfoo\*(Aq); .Ve .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, and will be emitted by \&\fIself_url()\fR and \fIquery_string()\fR. newstyle_urls 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 1 \& 1. if $CGITempFile::TMPDIRECTORY is already set, use that \& \& 2. if the environment variable TMPDIR exists, use the location \& indicated. \& \& 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. .SS "\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(\*(AqLevel 1 Header\*(Aq); .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,\*(AqLevel 1 Header\*(Aq,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)" .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. .SS "\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; \& \& \-or\- \& \& print header(\*(Aqimage/gif\*(Aq); \& \& \-or\- \& \& print header(\*(Aqtext/html\*(Aq,\*(Aq204 No response\*(Aq); \& \& \-or\- \& \& print header(\-type=>\*(Aqimage/gif\*(Aq, \& \-nph=>1, \& \-status=>\*(Aq402 Payment required\*(Aq, \& \-expires=>\*(Aq+3d\*(Aq, \& \-cookie=>$cookie, \& \-charset=>\*(Aqutf\-7\*(Aq, \& \-attachment=>\*(Aqfoo.gif\*(Aq, \& \-Cost=>\*(Aq$2.00\*(Aq); .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. Some 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=>\*(AqCAO DSP LAW CURa\*(Aq); .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 .PP \&\s-1CGI\s0.pm will accept valid multi-line headers when each line is separated with a \&\s-1CRLF\s0 value (\*(L"\er\en\*(R" on most platforms) followed by at least one space. For example: .PP .Vb 1 \& print header( \-ingredients => "ham\er\en\eseggs\er\en\esbacon" ); .Ve .PP Invalid multi-line header input will trigger in an exception. When multi-line headers are received, \s-1CGI\s0.pm will always output them back as a single line, according to the folding rules of \s-1RFC\s0 2616: the newlines will be removed, while the white space remains. .SS "\s-1GENERATING\s0 A \s-1REDIRECTION\s0 \s-1HEADER\s0" .IX Subsection "GENERATING A REDIRECTION HEADER" .Vb 1 \& print $q\->redirect(\*(Aqhttp://somewhere.else/in/movie/land\*(Aq); .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 method 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 4 \& print $q\->redirect( \& \-uri=>\*(Aqhttp://somewhere.else/in/movie/land\*(Aq, \& \-nph=>1, \& \-status=>301); .Ve .PP All names arguments recognized by \fIheader()\fR are also recognized by \&\fIredirect()\fR. However, most \s-1HTTP\s0 headers, including those generated by \&\-cookie and \-target, are ignored by the browser. .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. .SS "\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=>\*(AqSecrets of the Pyramids\*(Aq, \& \-author=>\*(Aqfred@capricorn.org\*(Aq, \& \-base=>\*(Aqtrue\*(Aq, \& \-target=>\*(Aq_blank\*(Aq, \& \-meta=>{\*(Aqkeywords\*(Aq=>\*(Aqpharaoh secret mummy\*(Aq, \& \*(Aqcopyright\*(Aq=>\*(Aqcopyright 1996 King Tut\*(Aq}, \& \-style=>{\*(Aqsrc\*(Aq=>\*(Aq/styles/style1.css\*(Aq}, \& \-BGCOLOR=>\*(Aqblue\*(Aq); .Ve .PP 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 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 some browsers!\fR .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 a hash 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=>\*(Aqfr\-CA\*(Aq); .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\-dtd\fR argument can be used to specify a public \s-1DTD\s0 identifier string. For example: .PP .Vb 1 \& \-dtd => \*(Aq\-//W3C//DTD HTML 4.01 Transitional//EN\*(Aq) .Ve .PP Alternatively, it can take public and system \s-1DTD\s0 identifiers as an array: .PP .Vb 1 \& dtd => [ \*(Aq\-//W3C//DTD HTML 4.01 Transitional//EN\*(Aq, \*(Aqhttp://www.w3.org/TR/html4/loose.dtd\*(Aq ] .Ve .PP For the public \s-1DTD\s0 identifier to be considered, it must be valid. Otherwise it will be replaced by the default \s-1DTD\s0. If the public \s-1DTD\s0 contains '\s-1XHTML\s0', \s-1CGI\s0.pm will emit \s-1XML\s0. .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 a element in the head section, use this: .PP .Vb 2 \& print start_html(\-head=>Link({\-rel=>\*(Aqshortcut icon\*(Aq, \& \-href=>\*(Aqfavicon.ico\*(Aq})); .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=>\*(Aqnext\*(Aq, \& \-href=>\*(Aqhttp://www.capricorn.com/s2.html\*(Aq}), \& Link({\-rel=>\*(Aqprevious\*(Aq, \& \-href=>\*(Aqhttp://www.capricorn.com/s1.html\*(Aq}) \& ] \& ); .Ve .PP And here's how to create an HTTP-EQUIV tag: .PP .Vb 2 \& print start_html(\-head=>meta({\-http_equiv => \*(AqContent\-Type\*(Aq, \& \-content => \*(Aqtext/html\*(Aq})) .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 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