Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / man / man3 / CGI.3
CommitLineData
920dae64
AT
1.\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32
2.\"
3.\" Standard preamble:
4.\" ========================================================================
5.de Sh \" Subsection heading
6.br
7.if t .Sp
8.ne 5
9.PP
10\fB\\$1\fR
11.PP
12..
13.de Sp \" Vertical space (when we can't use .PP)
14.if t .sp .5v
15.if n .sp
16..
17.de Vb \" Begin verbatim text
18.ft CW
19.nf
20.ne \\$1
21..
22.de Ve \" End verbatim text
23.ft R
24.fi
25..
26.\" Set up some character translations and predefined strings. \*(-- will
27.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28.\" double quote, and \*(R" will give a right double quote. | will give a
29.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
30.\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
31.\" expand to `' in nroff, nothing in troff, for use with C<>.
32.tr \(*W-|\(bv\*(Tr
33.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34.ie n \{\
35. ds -- \(*W-
36. ds PI pi
37. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
39. ds L" ""
40. ds R" ""
41. ds C` ""
42. ds C' ""
43'br\}
44.el\{\
45. ds -- \|\(em\|
46. ds PI \(*p
47. ds L" ``
48. ds R" ''
49'br\}
50.\"
51.\" If the F register is turned on, we'll generate index entries on stderr for
52.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53.\" entries marked with X<> in POD. Of course, you'll have to process the
54.\" output yourself in some meaningful fashion.
55.if \nF \{\
56. de IX
57. tm Index:\\$1\t\\n%\t"\\$2"
58..
59. nr % 0
60. rr F
61.\}
62.\"
63.\" For nroff, turn off justification. Always turn off hyphenation; it makes
64.\" way too many mistakes in technical documents.
65.hy 0
66.if n .na
67.\"
68.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69.\" Fear. Run. Save yourself. No user-serviceable parts.
70. \" fudge factors for nroff and troff
71.if n \{\
72. ds #H 0
73. ds #V .8m
74. ds #F .3m
75. ds #[ \f1
76. ds #] \fP
77.\}
78.if t \{\
79. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80. ds #V .6m
81. ds #F 0
82. ds #[ \&
83. ds #] \&
84.\}
85. \" simple accents for nroff and troff
86.if n \{\
87. ds ' \&
88. ds ` \&
89. ds ^ \&
90. ds , \&
91. ds ~ ~
92. ds /
93.\}
94.if t \{\
95. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101.\}
102. \" troff and (daisy-wheel) nroff accents
103.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110.ds ae a\h'-(\w'a'u*4/10)'e
111.ds Ae A\h'-(\w'A'u*4/10)'E
112. \" corrections for vroff
113.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115. \" for low resolution devices (crt and lpr)
116.if \n(.H>23 .if \n(.V>19 \
117\{\
118. ds : e
119. ds 8 ss
120. ds o a
121. ds d- d\h'-1'\(ga
122. ds D- D\h'-1'\(hy
123. ds th \o'bp'
124. ds Th \o'LP'
125. ds ae ae
126. ds Ae AE
127.\}
128.rm #[ #] #H #V #F C
129.\" ========================================================================
130.\"
131.IX Title "CGI 3"
132.TH CGI 3 "2001-09-21" "perl v5.8.8" "Perl Programmers Reference Guide"
133.SH "NAME"
134CGI \- Simple Common Gateway Interface Class
135.SH "SYNOPSIS"
136.IX Header "SYNOPSIS"
137.Vb 2
138\& # CGI script that creates a fill-out form
139\& # and echoes back its values.
140.Ve
141.PP
142.Vb 16
143\& use CGI qw/:standard/;
144\& print header,
145\& start_html('A Simple Example'),
146\& h1('A Simple Example'),
147\& start_form,
148\& "What's your name? ",textfield('name'),p,
149\& "What's the combination?", p,
150\& checkbox_group(-name=>'words',
151\& -values=>['eenie','meenie','minie','moe'],
152\& -defaults=>['eenie','minie']), p,
153\& "What's your favorite color? ",
154\& popup_menu(-name=>'color',
155\& -values=>['red','green','blue','chartreuse']),p,
156\& submit,
157\& end_form,
158\& hr;
159.Ve
160.PP
161.Vb 9
162\& if (param()) {
163\& my $name = param('name');
164\& my $keywords = join ', ',param('words');
165\& my $color = param('color');
166\& print "Your name is",em(escapeHTML($name)),p,
167\& "The keywords are: ",em(escapeHTML($keywords)),p,
168\& "Your favorite color is ",em(escapeHTML($color)),
169\& hr;
170\& }
171.Ve
172.SH "ABSTRACT"
173.IX Header "ABSTRACT"
174This perl library uses perl5 objects to make it easy to create Web
175fill-out forms and parse their contents. This package defines \s-1CGI\s0
176objects, entities that contain the values of the current query string
177and other state variables. Using a \s-1CGI\s0 object's methods, you can
178examine keywords and parameters passed to your script, and create
179forms whose initial values are taken from the current query (thereby
180preserving state information). The module provides shortcut functions
181that produce boilerplate \s-1HTML\s0, reducing typing and coding errors. It
182also provides functionality for some of the more advanced features of
183\&\s-1CGI\s0 scripting, including support for file uploads, cookies, cascading
184style sheets, server push, and frames.
185.PP
186\&\s-1CGI\s0.pm also provides a simple function-oriented programming style for
187those who don't need its object-oriented features.
188.PP
189The current version of \s-1CGI\s0.pm is available at
190.PP
191.Vb 2
192\& http://www.genome.wi.mit.edu/ftp/pub/software/WWW/cgi_docs.html
193\& ftp://ftp-genome.wi.mit.edu/pub/software/WWW/
194.Ve
195.SH "DESCRIPTION"
196.IX Header "DESCRIPTION"
197.Sh "\s-1PROGRAMMING\s0 \s-1STYLE\s0"
198.IX Subsection "PROGRAMMING STYLE"
199There are two styles of programming with \s-1CGI\s0.pm, an object-oriented
200style and a function-oriented style. In the object-oriented style you
201create one or more \s-1CGI\s0 objects and then use object methods to create
202the various elements of the page. Each \s-1CGI\s0 object starts out with the
203list of named parameters that were passed to your \s-1CGI\s0 script by the
204server. You can modify the objects, save them to a file or database
205and recreate them. Because each object corresponds to the \*(L"state\*(R" of
206the \s-1CGI\s0 script, and because each object's parameter list is
207independent of the others, this allows you to save the state of the
208script and restore it later.
209.PP
210For example, using the object oriented style, here is how you create
211a simple \*(L"Hello World\*(R" \s-1HTML\s0 page:
212.PP
213.Vb 7
214\& #!/usr/local/bin/perl -w
215\& use CGI; # load CGI routines
216\& $q = new CGI; # create new CGI object
217\& print $q->header, # create the HTTP header
218\& $q->start_html('hello world'), # start the HTML
219\& $q->h1('hello world'), # level 1 header
220\& $q->end_html; # end the HTML
221.Ve
222.PP
223In the function-oriented style, there is one default \s-1CGI\s0 object that
224you rarely deal with directly. Instead you just call functions to
225retrieve \s-1CGI\s0 parameters, create \s-1HTML\s0 tags, manage cookies, and so
226on. This provides you with a cleaner programming interface, but
227limits you to using one \s-1CGI\s0 object at a time. The following example
228prints the same page, but uses the function-oriented interface.
229The main differences are that we now need to import a set of functions
230into our name space (usually the \*(L"standard\*(R" functions), and we don't
231need to create the \s-1CGI\s0 object.
232.PP
233.Vb 6
234\& #!/usr/local/bin/perl
235\& use CGI qw/:standard/; # load standard CGI routines
236\& print header, # create the HTTP header
237\& start_html('hello world'), # start the HTML
238\& h1('hello world'), # level 1 header
239\& end_html; # end the HTML
240.Ve
241.PP
242The examples in this document mainly use the object-oriented style.
243See \s-1HOW\s0 \s-1TO\s0 \s-1IMPORT\s0 \s-1FUNCTIONS\s0 for important information on
244function-oriented programming in \s-1CGI\s0.pm
245.Sh "\s-1CALLING\s0 \s-1CGI\s0.PM \s-1ROUTINES\s0"
246.IX Subsection "CALLING CGI.PM ROUTINES"
247Most \s-1CGI\s0.pm routines accept several arguments, sometimes as many as 20
248optional ones! To simplify this interface, all routines use a named
249argument calling style that looks like this:
250.PP
251.Vb 1
252\& print $q->header(-type=>'image/gif',-expires=>'+3d');
253.Ve
254.PP
255Each argument name is preceded by a dash. Neither case nor order
256matters in the argument list. \-type, \-Type, and \-TYPE are all
257acceptable. In fact, only the first argument needs to begin with a
258dash. If a dash is present in the first argument, \s-1CGI\s0.pm assumes
259dashes for the subsequent ones.
260.PP
261Several routines are commonly called with just one argument. In the
262case of these routines you can provide the single argument without an
263argument name. \fIheader()\fR happens to be one of these routines. In this
264case, the single argument is the document type.
265.PP
266.Vb 1
267\& print $q->header('text/html');
268.Ve
269.PP
270Other such routines are documented below.
271.PP
272Sometimes named arguments expect a scalar, sometimes a reference to an
273array, and sometimes a reference to a hash. Often, you can pass any
274type of argument and the routine will do whatever is most appropriate.
275For example, the \fIparam()\fR routine is used to set a \s-1CGI\s0 parameter to a
276single or a multi-valued value. The two cases are shown below:
277.PP
278.Vb 2
279\& $q->param(-name=>'veggie',-value=>'tomato');
280\& $q->param(-name=>'veggie',-value=>['tomato','tomahto','potato','potahto']);
281.Ve
282.PP
283A large number of routines in \s-1CGI\s0.pm actually aren't specifically
284defined in the module, but are generated automatically as needed.
285These are the \*(L"\s-1HTML\s0 shortcuts,\*(R" routines that generate \s-1HTML\s0 tags for
286use in dynamically-generated pages. \s-1HTML\s0 tags have both attributes
287(the attribute=\*(L"value\*(R" pairs within the tag itself) and contents (the
288part between the opening and closing pairs.) To distinguish between
289attributes and contents, \s-1CGI\s0.pm uses the convention of passing \s-1HTML\s0
290attributes as a hash reference as the first argument, and the
291contents, if any, as any subsequent arguments. It works out like
292this:
293.PP
294.Vb 6
295\& Code Generated HTML
296\& ---- --------------
297\& h1() <h1>
298\& h1('some','contents'); <h1>some contents</h1>
299\& h1({-align=>left}); <h1 align="LEFT">
300\& h1({-align=>left},'contents'); <h1 align="LEFT">contents</h1>
301.Ve
302.PP
303\&\s-1HTML\s0 tags are described in more detail later.
304.PP
305Many newcomers to \s-1CGI\s0.pm are puzzled by the difference between the
306calling conventions for the \s-1HTML\s0 shortcuts, which require curly braces
307around the \s-1HTML\s0 tag attributes, and the calling conventions for other
308routines, which manage to generate attributes without the curly
309brackets. Don't be confused. As a convenience the curly braces are
310optional in all but the \s-1HTML\s0 shortcuts. If you like, you can use
311curly braces when calling any routine that takes named arguments. For
312example:
313.PP
314.Vb 1
315\& print $q->header( {-type=>'image/gif',-expires=>'+3d'} );
316.Ve
317.PP
318If you use the \fB\-w\fR switch, you will be warned that some \s-1CGI\s0.pm argument
319names conflict with built-in Perl functions. The most frequent of
320these is the \-values argument, used to create multi-valued menus,
321radio button clusters and the like. To get around this warning, you
322have several choices:
323.IP "1." 4
324Use another name for the argument, if one is available.
325For example, \-value is an alias for \-values.
326.IP "2." 4
327Change the capitalization, e.g. \-Values
328.IP "3." 4
329Put quotes around the argument name, e.g. '\-values'
330.PP
331Many routines will do something useful with a named argument that it
332doesn't recognize. For example, you can produce non-standard \s-1HTTP\s0
333header fields by providing them as named arguments:
334.PP
335.Vb 4
336\& print $q->header(-type => 'text/html',
337\& -cost => 'Three smackers',
338\& -annoyance_level => 'high',
339\& -complaints_to => 'bit bucket');
340.Ve
341.PP
342This will produce the following nonstandard \s-1HTTP\s0 header:
343.PP
344.Vb 5
345\& HTTP/1.0 200 OK
346\& Cost: Three smackers
347\& Annoyance-level: high
348\& Complaints-to: bit bucket
349\& Content-type: text/html
350.Ve
351.PP
352Notice the way that underscores are translated automatically into
353hyphens. HTML-generating routines perform a different type of
354translation.
355.PP
356This feature allows you to keep up with the rapidly changing \s-1HTTP\s0 and
357\&\s-1HTML\s0 \*(L"standards\*(R".
358.Sh "\s-1CREATING\s0 A \s-1NEW\s0 \s-1QUERY\s0 \s-1OBJECT\s0 (\s-1OBJECT\-ORIENTED\s0 \s-1STYLE\s0):"
359.IX Subsection "CREATING A NEW QUERY OBJECT (OBJECT-ORIENTED STYLE):"
360.Vb 1
361\& $query = new CGI;
362.Ve
363.PP
364This will parse the input (from both \s-1POST\s0 and \s-1GET\s0 methods) and store
365it into a perl5 object called \f(CW$query\fR.
366.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"
367.IX Subsection "CREATING A NEW QUERY OBJECT FROM AN INPUT FILE"
368.Vb 1
369\& $query = new CGI(INPUTFILE);
370.Ve
371.PP
372If you provide a file handle to the \fInew()\fR method, it will read
373parameters from the file (or \s-1STDIN\s0, or whatever). The file can be in
374any of the forms describing below under debugging (i.e. a series of
375newline delimited TAG=VALUE pairs will work). Conveniently, this type
376of file is created by the \fIsave()\fR method (see below). Multiple records
377can be saved and restored.
378.PP
379Perl purists will be pleased to know that this syntax accepts
380references to file handles, or even references to filehandle globs,
381which is the \*(L"official\*(R" way to pass a filehandle:
382.PP
383.Vb 1
384\& $query = new CGI(\e*STDIN);
385.Ve
386.PP
387You can also initialize the \s-1CGI\s0 object with a FileHandle or IO::File
388object.
389.PP
390If you are using the function-oriented interface and want to
391initialize \s-1CGI\s0 state from a file handle, the way to do this is with
392\&\fB\f(BIrestore_parameters()\fB\fR. This will (re)initialize the
393default \s-1CGI\s0 object from the indicated file handle.
394.PP
395.Vb 3
396\& open (IN,"test.in") || die;
397\& restore_parameters(IN);
398\& close IN;
399.Ve
400.PP
401You can also initialize the query object from an associative array
402reference:
403.PP
404.Vb 4
405\& $query = new CGI( {'dinosaur'=>'barney',
406\& 'song'=>'I love you',
407\& 'friends'=>[qw/Jessica George Nancy/]}
408\& );
409.Ve
410.PP
411or from a properly formatted, URL-escaped query string:
412.PP
413.Vb 1
414\& $query = new CGI('dinosaur=barney&color=purple');
415.Ve
416.PP
417or from a previously existing \s-1CGI\s0 object (currently this clones the
418parameter list, but none of the other object-specific fields, such as
419autoescaping):
420.PP
421.Vb 2
422\& $old_query = new CGI;
423\& $new_query = new CGI($old_query);
424.Ve
425.PP
426To create an empty query, initialize it from an empty string or hash:
427.PP
428.Vb 1
429\& $empty_query = new CGI("");
430.Ve
431.PP
432.Vb 1
433\& -or-
434.Ve
435.PP
436.Vb 1
437\& $empty_query = new CGI({});
438.Ve
439.Sh "\s-1FETCHING\s0 A \s-1LIST\s0 \s-1OF\s0 \s-1KEYWORDS\s0 \s-1FROM\s0 \s-1THE\s0 \s-1QUERY:\s0"
440.IX Subsection "FETCHING A LIST OF KEYWORDS FROM THE QUERY:"
441.Vb 1
442\& @keywords = $query->keywords
443.Ve
444.PP
445If the script was invoked as the result of an <\s-1ISINDEX\s0> search, the
446parsed keywords can be obtained as an array using the \fIkeywords()\fR method.
447.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"
448.IX Subsection "FETCHING THE NAMES OF ALL THE PARAMETERS PASSED TO YOUR SCRIPT:"
449.Vb 1
450\& @names = $query->param
451.Ve
452.PP
453If the script was invoked with a parameter list
454(e.g. \*(L"name1=value1&name2=value2&name3=value3\*(R"), the \fIparam()\fR method
455will return the parameter names as a list. If the script was invoked
456as an <\s-1ISINDEX\s0> script and contains a string without ampersands
457(e.g. \*(L"value1+value2+value3\*(R") , there will be a single parameter named
458\&\*(L"keywords\*(R" containing the \*(L"+\*(R"\-delimited keywords.
459.PP
460\&\s-1NOTE:\s0 As of version 1.5, the array of parameter names returned will
461be in the same order as they were submitted by the browser.
462Usually this order is the same as the order in which the
463parameters are defined in the form (however, this isn't part
464of the spec, and so isn't guaranteed).
465.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"
466.IX Subsection "FETCHING THE VALUE OR VALUES OF A SINGLE NAMED PARAMETER:"
467.Vb 1
468\& @values = $query->param('foo');
469.Ve
470.PP
471.Vb 1
472\& -or-
473.Ve
474.PP
475.Vb 1
476\& $value = $query->param('foo');
477.Ve
478.PP
479Pass the \fIparam()\fR method a single argument to fetch the value of the
480named parameter. If the parameter is multivalued (e.g. from multiple
481selections in a scrolling list), you can ask to receive an array. Otherwise
482the method will return a single value.
483.PP
484If a value is not given in the query string, as in the queries
485\&\*(L"name1=&name2=\*(R" or \*(L"name1&name2\*(R", it will be returned as an empty
486string. This feature is new in 2.63.
487.PP
488If the parameter does not exist at all, then \fIparam()\fR will return undef
489in a scalar context, and the empty list in a list context.
490.Sh "\s-1SETTING\s0 \s-1THE\s0 \s-1VALUE\s0(S) \s-1OF\s0 A \s-1NAMED\s0 \s-1PARAMETER:\s0"
491.IX Subsection "SETTING THE VALUE(S) OF A NAMED PARAMETER:"
492.Vb 1
493\& $query->param('foo','an','array','of','values');
494.Ve
495.PP
496This sets the value for the named parameter 'foo' to an array of
497values. This is one way to change the value of a field \s-1AFTER\s0
498the script has been invoked once before. (Another way is with
499the \-override parameter accepted by all methods that generate
500form elements.)
501.PP
502\&\fIparam()\fR also recognizes a named parameter style of calling described
503in more detail later:
504.PP
505.Vb 1
506\& $query->param(-name=>'foo',-values=>['an','array','of','values']);
507.Ve
508.PP
509.Vb 1
510\& -or-
511.Ve
512.PP
513.Vb 1
514\& $query->param(-name=>'foo',-value=>'the value');
515.Ve
516.Sh "\s-1APPENDING\s0 \s-1ADDITIONAL\s0 \s-1VALUES\s0 \s-1TO\s0 A \s-1NAMED\s0 \s-1PARAMETER:\s0"
517.IX Subsection "APPENDING ADDITIONAL VALUES TO A NAMED PARAMETER:"
518.Vb 1
519\& $query->append(-name=>'foo',-values=>['yet','more','values']);
520.Ve
521.PP
522This adds a value or list of values to the named parameter. The
523values are appended to the end of the parameter if it already exists.
524Otherwise the parameter is created. Note that this method only
525recognizes the named argument calling syntax.
526.Sh "\s-1IMPORTING\s0 \s-1ALL\s0 \s-1PARAMETERS\s0 \s-1INTO\s0 A \s-1NAMESPACE:\s0"
527.IX Subsection "IMPORTING ALL PARAMETERS INTO A NAMESPACE:"
528.Vb 1
529\& $query->import_names('R');
530.Ve
531.PP
532This creates a series of variables in the 'R' namespace. For example,
533\&\f(CW$R::foo\fR, \f(CW@R:foo\fR. For keyword lists, a variable \f(CW@R::keywords\fR will appear.
534If no namespace is given, this method will assume 'Q'.
535\&\s-1WARNING:\s0 don't import anything into 'main'; this is a major security
536risk!!!!
537.PP
538\&\s-1NOTE\s0 1: Variable names are transformed as necessary into legal Perl
539variable names. All non-legal characters are transformed into
540underscores. If you need to keep the original names, you should use
541the \fIparam()\fR method instead to access \s-1CGI\s0 variables by name.
542.PP
543\&\s-1NOTE\s0 2: In older versions, this method was called \fB\f(BIimport()\fB\fR. As of version 2.20,
544this name has been removed completely to avoid conflict with the built-in
545Perl module \fBimport\fR operator.
546.Sh "\s-1DELETING\s0 A \s-1PARAMETER\s0 \s-1COMPLETELY:\s0"
547.IX Subsection "DELETING A PARAMETER COMPLETELY:"
548.Vb 1
549\& $query->delete('foo','bar','baz');
550.Ve
551.PP
552This completely clears a list of parameters. It sometimes useful for
553resetting parameters that you don't want passed down between script
554invocations.
555.PP
556If you are using the function call interface, use \*(L"\fIDelete()\fR\*(R" instead
557to avoid conflicts with Perl's built-in delete operator.
558.Sh "\s-1DELETING\s0 \s-1ALL\s0 \s-1PARAMETERS:\s0"
559.IX Subsection "DELETING ALL PARAMETERS:"
560.Vb 1
561\& $query->delete_all();
562.Ve
563.PP
564This clears the \s-1CGI\s0 object completely. It might be useful to ensure
565that all the defaults are taken when you create a fill-out form.
566.PP
567Use \fIDelete_all()\fR instead if you are using the function call interface.
568.Sh "\s-1HANDLING\s0 NON-URLENCODED \s-1ARGUMENTS\s0"
569.IX Subsection "HANDLING NON-URLENCODED ARGUMENTS"
570If POSTed data is not of type application/x\-www\-form\-urlencoded or
571multipart/form\-data, then the POSTed data will not be processed, but
572instead be returned as-is in a parameter named \s-1POSTDATA\s0. To retrieve
573it, use code like this:
574.PP
575.Vb 1
576\& my $data = $query->param('POSTDATA');
577.Ve
578.PP
579(If you don't know what the preceding means, don't worry about it. It
580only affects people trying to use \s-1CGI\s0 for \s-1XML\s0 processing and other
581specialized tasks.)
582.Sh "\s-1DIRECT\s0 \s-1ACCESS\s0 \s-1TO\s0 \s-1THE\s0 \s-1PARAMETER\s0 \s-1LIST:\s0"
583.IX Subsection "DIRECT ACCESS TO THE PARAMETER LIST:"
584.Vb 2
585\& $q->param_fetch('address')->[1] = '1313 Mockingbird Lane';
586\& unshift @{$q->param_fetch(-name=>'address')},'George Munster';
587.Ve
588.PP
589If you need access to the parameter list in a way that isn't covered
590by the methods above, you can obtain a direct reference to it by
591calling the \fB\f(BIparam_fetch()\fB\fR method with the name of the . This
592will return an array reference to the named parameters, which you then
593can manipulate in any way you like.
594.PP
595You can also use a named argument style using the \fB\-name\fR argument.
596.Sh "\s-1FETCHING\s0 \s-1THE\s0 \s-1PARAMETER\s0 \s-1LIST\s0 \s-1AS\s0 A \s-1HASH:\s0"
597.IX Subsection "FETCHING THE PARAMETER LIST AS A HASH:"
598.Vb 4
599\& $params = $q->Vars;
600\& print $params->{'address'};
601\& @foo = split("\e0",$params->{'foo'});
602\& %params = $q->Vars;
603.Ve
604.PP
605.Vb 2
606\& use CGI ':cgi-lib';
607\& $params = Vars;
608.Ve
609.PP
610Many people want to fetch the entire parameter list as a hash in which
611the keys are the names of the \s-1CGI\s0 parameters, and the values are the
612parameters' values. The \fIVars()\fR method does this. Called in a scalar
613context, it returns the parameter list as a tied hash reference.
614Changing a key changes the value of the parameter in the underlying
615\&\s-1CGI\s0 parameter list. Called in a list context, it returns the
616parameter list as an ordinary hash. This allows you to read the
617contents of the parameter list, but not to change it.
618.PP
619When using this, the thing you must watch out for are multivalued \s-1CGI\s0
620parameters. Because a hash cannot distinguish between scalar and
621list context, multivalued parameters will be returned as a packed
622string, separated by the \*(L"\e0\*(R" (null) character. You must split this
623packed string in order to get at the individual values. This is the
624convention introduced long ago by Steve Brenner in his cgi\-lib.pl
625module for Perl version 4.
626.PP
627If you wish to use \fIVars()\fR as a function, import the \fI:cgi\-lib\fR set of
628function calls (also see the section on CGI-LIB compatibility).
629.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"
630.IX Subsection "SAVING THE STATE OF THE SCRIPT TO A FILE:"
631.Vb 1
632\& $query->save(\e*FILEHANDLE)
633.Ve
634.PP
635This will write the current state of the form to the provided
636filehandle. You can read it back in by providing a filehandle
637to the \fInew()\fR method. Note that the filehandle can be a file, a pipe,
638or whatever!
639.PP
640The format of the saved file is:
641.PP
642.Vb 5
643\& NAME1=VALUE1
644\& NAME1=VALUE1'
645\& NAME2=VALUE2
646\& NAME3=VALUE3
647\& =
648.Ve
649.PP
650Both name and value are \s-1URL\s0 escaped. Multi-valued \s-1CGI\s0 parameters are
651represented as repeated names. A session record is delimited by a
652single = symbol. You can write out multiple records and read them
653back in with several calls to \fBnew\fR. You can do this across several
654sessions by opening the file in append mode, allowing you to create
655primitive guest books, or to keep a history of users' queries. Here's
656a short example of creating multiple session records:
657.PP
658.Vb 1
659\& use CGI;
660.Ve
661.PP
662.Vb 8
663\& open (OUT,">>test.out") || die;
664\& $records = 5;
665\& foreach (0..$records) {
666\& my $q = new CGI;
667\& $q->param(-name=>'counter',-value=>$_);
668\& $q->save(\e*OUT);
669\& }
670\& close OUT;
671.Ve
672.PP
673.Vb 6
674\& # reopen for reading
675\& open (IN,"test.out") || die;
676\& while (!eof(IN)) {
677\& my $q = new CGI(\e*IN);
678\& print $q->param('counter'),"\en";
679\& }
680.Ve
681.PP
682The file format used for save/restore is identical to that used by the
683Whitehead Genome Center's data exchange format \*(L"Boulderio\*(R", and can be
684manipulated and even databased using Boulderio utilities. See
685.PP
686.Vb 1
687\& http://stein.cshl.org/boulder/
688.Ve
689.PP
690for further details.
691.PP
692If you wish to use this method from the function-oriented (non\-OO)
693interface, the exported name for this method is \fB\f(BIsave_parameters()\fB\fR.
694.Sh "\s-1RETRIEVING\s0 \s-1CGI\s0 \s-1ERRORS\s0"
695.IX Subsection "RETRIEVING CGI ERRORS"
696Errors can occur while processing user input, particularly when
697processing uploaded files. When these errors occur, \s-1CGI\s0 will stop
698processing and return an empty parameter list. You can test for
699the existence and nature of errors using the \fI\fIcgi_error()\fI\fR function.
700The error messages are formatted as \s-1HTTP\s0 status codes. You can either
701incorporate the error text into an \s-1HTML\s0 page, or use it as the value
702of the \s-1HTTP\s0 status:
703.PP
704.Vb 8
705\& my $error = $q->cgi_error;
706\& if ($error) {
707\& print $q->header(-status=>$error),
708\& $q->start_html('Problems'),
709\& $q->h2('Request not processed'),
710\& $q->strong($error);
711\& exit 0;
712\& }
713.Ve
714.PP
715When using the function-oriented interface (see the next section),
716errors may only occur the first time you call \fI\fIparam()\fI\fR. Be ready
717for this!
718.Sh "\s-1USING\s0 \s-1THE\s0 FUNCTION-ORIENTED \s-1INTERFACE\s0"
719.IX Subsection "USING THE FUNCTION-ORIENTED INTERFACE"
720To use the function-oriented interface, you must specify which \s-1CGI\s0.pm
721routines or sets of routines to import into your script's namespace.
722There is a small overhead associated with this importation, but it
723isn't much.
724.PP
725.Vb 1
726\& use CGI <list of methods>;
727.Ve
728.PP
729The listed methods will be imported into the current package; you can
730call them directly without creating a \s-1CGI\s0 object first. This example
731shows how to import the \fB\f(BIparam()\fB\fR and \fB\f(BIheader()\fB\fR
732methods, and then use them directly:
733.PP
734.Vb 3
735\& use CGI 'param','header';
736\& print header('text/plain');
737\& $zipcode = param('zipcode');
738.Ve
739.PP
740More frequently, you'll import common sets of functions by referring
741to the groups by name. All function sets are preceded with a \*(L":\*(R"
742character as in \*(L":html3\*(R" (for tags defined in the \s-1HTML\s0 3 standard).
743.PP
744Here is a list of the function sets you can import:
745.IP "\fB:cgi\fR" 4
746.IX Item ":cgi"
747Import all CGI-handling methods, such as \fB\f(BIparam()\fB\fR, \fB\f(BIpath_info()\fB\fR
748and the like.
749.IP "\fB:form\fR" 4
750.IX Item ":form"
751Import all fill-out form generating methods, such as \fB\f(BItextfield()\fB\fR.
752.IP "\fB:html2\fR" 4
753.IX Item ":html2"
754Import all methods that generate \s-1HTML\s0 2.0 standard elements.
755.IP "\fB:html3\fR" 4
756.IX Item ":html3"
757Import all methods that generate \s-1HTML\s0 3.0 elements (such as
758<table>, <super> and <sub>).
759.IP "\fB:html4\fR" 4
760.IX Item ":html4"
761Import all methods that generate \s-1HTML\s0 4 elements (such as
762<abbrev>, <acronym> and <thead>).
763.IP "\fB:netscape\fR" 4
764.IX Item ":netscape"
765Import all methods that generate Netscape-specific \s-1HTML\s0 extensions.
766.IP "\fB:html\fR" 4
767.IX Item ":html"
768Import all HTML-generating shortcuts (i.e. 'html2' + 'html3' +
769\&'netscape')...
770.IP "\fB:standard\fR" 4
771.IX Item ":standard"
772Import \*(L"standard\*(R" features, 'html2', 'html3', 'html4', 'form' and 'cgi'.
773.IP "\fB:all\fR" 4
774.IX Item ":all"
775Import all the available methods. For the full list, see the \s-1CGI\s0.pm
776code, where the variable \f(CW%EXPORT_TAGS\fR is defined.
777.PP
778If you import a function name that is not part of \s-1CGI\s0.pm, the module
779will treat it as a new \s-1HTML\s0 tag and generate the appropriate
780subroutine. You can then use it like any other \s-1HTML\s0 tag. This is to
781provide for the rapidly-evolving \s-1HTML\s0 \*(L"standard.\*(R" For example, say
782Microsoft comes out with a new tag called <gradient> (which causes the
783user's desktop to be flooded with a rotating gradient fill until his
784machine reboots). You don't need to wait for a new version of \s-1CGI\s0.pm
785to start using it immediately:
786.PP
787.Vb 2
788\& use CGI qw/:standard :html3 gradient/;
789\& print gradient({-start=>'red',-end=>'blue'});
790.Ve
791.PP
792Note that in the interests of execution speed \s-1CGI\s0.pm does \fBnot\fR use
793the standard Exporter syntax for specifying load symbols. This may
794change in the future.
795.PP
796If you import any of the state-maintaining \s-1CGI\s0 or form-generating
797methods, a default \s-1CGI\s0 object will be created and initialized
798automatically the first time you use any of the methods that require
799one to be present. This includes \fB\f(BIparam()\fB\fR, \fB\f(BItextfield()\fB\fR,
800\&\fB\f(BIsubmit()\fB\fR and the like. (If you need direct access to the \s-1CGI\s0
801object, you can find it in the global variable \fB$CGI::Q\fR). By
802importing \s-1CGI\s0.pm methods, you can create visually elegant scripts:
803.PP
804.Vb 17
805\& use CGI qw/:standard/;
806\& print
807\& header,
808\& start_html('Simple Script'),
809\& h1('Simple Script'),
810\& start_form,
811\& "What's your name? ",textfield('name'),p,
812\& "What's the combination?",
813\& checkbox_group(-name=>'words',
814\& -values=>['eenie','meenie','minie','moe'],
815\& -defaults=>['eenie','moe']),p,
816\& "What's your favorite color?",
817\& popup_menu(-name=>'color',
818\& -values=>['red','green','blue','chartreuse']),p,
819\& submit,
820\& end_form,
821\& hr,"\en";
822.Ve
823.PP
824.Vb 7
825\& if (param) {
826\& print
827\& "Your name is ",em(param('name')),p,
828\& "The keywords are: ",em(join(", ",param('words'))),p,
829\& "Your favorite color is ",em(param('color')),".\en";
830\& }
831\& print end_html;
832.Ve
833.Sh "\s-1PRAGMAS\s0"
834.IX Subsection "PRAGMAS"
835In addition to the function sets, there are a number of pragmas that
836you can import. Pragmas, which are always preceded by a hyphen,
837change the way that \s-1CGI\s0.pm functions in various ways. Pragmas,
838function sets, and individual functions can all be imported in the
839same \fIuse()\fR line. For example, the following use statement imports the
840standard set of functions and enables debugging mode (pragma
841\&\-debug):
842.PP
843.Vb 1
844\& use CGI qw/:standard -debug/;
845.Ve
846.PP
847The current list of pragmas is as follows:
848.IP "\-any" 4
849.IX Item "-any"
850When you \fIuse \s-1CGI\s0 \-any\fR, then any method that the query object
851doesn't recognize will be interpreted as a new \s-1HTML\s0 tag. This allows
852you to support the next \fIad hoc\fR Netscape or Microsoft \s-1HTML\s0
853extension. This lets you go wild with new and unsupported tags:
854.Sp
855.Vb 3
856\& use CGI qw(-any);
857\& $q=new CGI;
858\& print $q->gradient({speed=>'fast',start=>'red',end=>'blue'});
859.Ve
860.Sp
861Since using <cite>any</cite> causes any mistyped method name
862to be interpreted as an \s-1HTML\s0 tag, use it with care or not at
863all.
864.IP "\-compile" 4
865.IX Item "-compile"
866This causes the indicated autoloaded methods to be compiled up front,
867rather than deferred to later. This is useful for scripts that run
868for an extended period of time under FastCGI or mod_perl, and for
869those destined to be crunched by Malcom Beattie's Perl compiler. Use
870it in conjunction with the methods or method families you plan to use.
871.Sp
872.Vb 1
873\& use CGI qw(-compile :standard :html3);
874.Ve
875.Sp
876or even
877.Sp
878.Vb 1
879\& use CGI qw(-compile :all);
880.Ve
881.Sp
882Note that using the \-compile pragma in this way will always have
883the effect of importing the compiled functions into the current
884namespace. If you want to compile without importing use the
885\&\fIcompile()\fR method instead:
886.Sp
887.Vb 2
888\& use CGI();
889\& CGI->compile();
890.Ve
891.Sp
892This is particularly useful in a mod_perl environment, in which you
893might want to precompile all \s-1CGI\s0 routines in a startup script, and
894then import the functions individually in each mod_perl script.
895.IP "\-nosticky" 4
896.IX Item "-nosticky"
897By default the \s-1CGI\s0 module implements a state-preserving behavior
898called \*(L"sticky\*(R" fields. The way this works is that if you are
899regenerating a form, the methods that generate the form field values
900will interrogate \fIparam()\fR to see if similarly-named parameters are
901present in the query string. If they find a like-named parameter, they
902will use it to set their default values.
903.Sp
904Sometimes this isn't what you want. The \fB\-nosticky\fR pragma prevents
905this behavior. You can also selectively change the sticky behavior in
906each element that you generate.
907.IP "\-tabindex" 4
908.IX Item "-tabindex"
909Automatically add tab index attributes to each form field. With this
910option turned off, you can still add tab indexes manually by passing a
911\&\-tabindex option to each field-generating method.
912.IP "\-no_undef_params" 4
913.IX Item "-no_undef_params"
914This keeps \s-1CGI\s0.pm from including undef params in the parameter list.
915.IP "\-no_xhtml" 4
916.IX Item "-no_xhtml"
917By default, \s-1CGI\s0.pm versions 2.69 and higher emit \s-1XHTML\s0
918(http://www.w3.org/TR/xhtml1/). The \-no_xhtml pragma disables this
919feature. Thanks to Michalis Kabrianis <kabrianis@hellug.gr> for this
920feature.
921.Sp
922If \fIstart_html()\fR's \-dtd parameter specifies an \s-1HTML\s0 2.0 or 3.2 \s-1DTD\s0,
923\&\s-1XHTML\s0 will automatically be disabled without needing to use this
924pragma.
925.IP "\-nph" 4
926.IX Item "-nph"
927This makes \s-1CGI\s0.pm produce a header appropriate for an \s-1NPH\s0 (no
928parsed header) script. You may need to do other things as well
929to tell the server that the script is \s-1NPH\s0. See the discussion
930of \s-1NPH\s0 scripts below.
931.IP "\-newstyle_urls" 4
932.IX Item "-newstyle_urls"
933Separate the name=value pairs in \s-1CGI\s0 parameter query strings with
934semicolons rather than ampersands. For example:
935.Sp
936.Vb 1
937\& ?name=fred;age=24;favorite_color=3
938.Ve
939.Sp
940Semicolon-delimited query strings are always accepted, but will not be
941emitted by \fIself_url()\fR and \fIquery_string()\fR unless the \-newstyle_urls
942pragma is specified.
943.Sp
944This became the default in version 2.64.
945.IP "\-oldstyle_urls" 4
946.IX Item "-oldstyle_urls"
947Separate the name=value pairs in \s-1CGI\s0 parameter query strings with
948ampersands rather than semicolons. This is no longer the default.
949.IP "\-autoload" 4
950.IX Item "-autoload"
951This overrides the autoloader so that any function in your program
952that is not recognized is referred to \s-1CGI\s0.pm for possible evaluation.
953This allows you to use all the \s-1CGI\s0.pm functions without adding them to
954your symbol table, which is of concern for mod_perl users who are
955worried about memory consumption. \fIWarning:\fR when
956\&\fI\-autoload\fR is in effect, you cannot use \*(L"poetry mode\*(R"
957(functions without the parenthesis). Use \fI\fIhr()\fI\fR rather
958than \fIhr\fR, or add something like \fIuse subs qw/hr p header/\fR
959to the top of your script.
960.IP "\-no_debug" 4
961.IX Item "-no_debug"
962This turns off the command-line processing features. If you want to
963run a \s-1CGI\s0.pm script from the command line to produce \s-1HTML\s0, and you
964don't want it to read \s-1CGI\s0 parameters from the command line or \s-1STDIN\s0,
965then use this pragma:
966.Sp
967.Vb 1
968\& use CGI qw(-no_debug :standard);
969.Ve
970.IP "\-debug" 4
971.IX Item "-debug"
972This turns on full debugging. In addition to reading \s-1CGI\s0 arguments
973from the command-line processing, \s-1CGI\s0.pm will pause and try to read
974arguments from \s-1STDIN\s0, producing the message \*(L"(offline mode: enter
975name=value pairs on standard input)\*(R" features.
976.Sp
977See the section on debugging for more details.
978.IP "\-private_tempfiles" 4
979.IX Item "-private_tempfiles"
980\&\s-1CGI\s0.pm can process uploaded file. Ordinarily it spools the uploaded
981file to a temporary directory, then deletes the file when done.
982However, this opens the risk of eavesdropping as described in the file
983upload section. Another \s-1CGI\s0 script author could peek at this data
984during the upload, even if it is confidential information. On Unix
985systems, the \-private_tempfiles pragma will cause the temporary file
986to be unlinked as soon as it is opened and before any data is written
987into it, reducing, but not eliminating the risk of eavesdropping
988(there is still a potential race condition). To make life harder for
989the attacker, the program chooses tempfile names by calculating a 32
990bit checksum of the incoming \s-1HTTP\s0 headers.
991.Sp
992To ensure that the temporary file cannot be read by other \s-1CGI\s0 scripts,
993use suEXEC or a \s-1CGI\s0 wrapper program to run your script. The temporary
994file is created with mode 0600 (neither world nor group readable).
995.Sp
996The temporary directory is selected using the following algorithm:
997.Sp
998.Vb 2
999\& 1. if the current user (e.g. "nobody") has a directory named
1000\& "tmp" in its home directory, use that (Unix systems only).
1001.Ve
1002.Sp
1003.Vb 2
1004\& 2. if the environment variable TMPDIR exists, use the location
1005\& indicated.
1006.Ve
1007.Sp
1008.Vb 2
1009\& 3. Otherwise try the locations /usr/tmp, /var/tmp, C:\etemp,
1010\& /tmp, /temp, ::Temporary Items, and \eWWW_ROOT.
1011.Ve
1012.Sp
1013Each of these locations is checked that it is a directory and is
1014writable. If not, the algorithm tries the next choice.
1015.Sh "\s-1SPECIAL\s0 \s-1FORMS\s0 \s-1FOR\s0 \s-1IMPORTING\s0 HTML-TAG \s-1FUNCTIONS\s0"
1016.IX Subsection "SPECIAL FORMS FOR IMPORTING HTML-TAG FUNCTIONS"
1017Many of the methods generate \s-1HTML\s0 tags. As described below, tag
1018functions automatically generate both the opening and closing tags.
1019For example:
1020.PP
1021.Vb 1
1022\& print h1('Level 1 Header');
1023.Ve
1024.PP
1025produces
1026.PP
1027.Vb 1
1028\& <h1>Level 1 Header</h1>
1029.Ve
1030.PP
1031There will be some times when you want to produce the start and end
1032tags yourself. In this case, you can use the form start_\fItag_name\fR
1033and end_\fItag_name\fR, as in:
1034.PP
1035.Vb 1
1036\& print start_h1,'Level 1 Header',end_h1;
1037.Ve
1038.PP
1039With a few exceptions (described below), start_\fItag_name\fR and
1040end_\fItag_name\fR functions are not generated automatically when you
1041\&\fIuse \s-1CGI\s0\fR. However, you can specify the tags you want to generate
1042\&\fIstart/end\fR functions for by putting an asterisk in front of their
1043name, or, alternatively, requesting either "start_\fItag_name\fR\*(L" or
1044\&\*(R"end_\fItag_name\fR" in the import list.
1045.PP
1046Example:
1047.PP
1048.Vb 1
1049\& use CGI qw/:standard *table start_ul/;
1050.Ve
1051.PP
1052In this example, the following functions are generated in addition to
1053the standard ones:
1054.IP "1. \fIstart_table()\fR (generates a <table> tag)" 4
1055.IX Item "1. start_table() (generates a <table> tag)"
1056.PD 0
1057.IP "2. \fIend_table()\fR (generates a </table> tag)" 4
1058.IX Item "2. end_table() (generates a </table> tag)"
1059.IP "3. \fIstart_ul()\fR (generates a <ul> tag)" 4
1060.IX Item "3. start_ul() (generates a <ul> tag)"
1061.IP "4. \fIend_ul()\fR (generates a </ul> tag)" 4
1062.IX Item "4. end_ul() (generates a </ul> tag)"
1063.PD
1064.SH "GENERATING DYNAMIC DOCUMENTS"
1065.IX Header "GENERATING DYNAMIC DOCUMENTS"
1066Most of \s-1CGI\s0.pm's functions deal with creating documents on the fly.
1067Generally you will produce the \s-1HTTP\s0 header first, followed by the
1068document itself. \s-1CGI\s0.pm provides functions for generating \s-1HTTP\s0
1069headers of various types as well as for generating \s-1HTML\s0. For creating
1070\&\s-1GIF\s0 images, see the \s-1GD\s0.pm module.
1071.PP
1072Each of these functions produces a fragment of \s-1HTML\s0 or \s-1HTTP\s0 which you
1073can print out directly so that it displays in the browser window,
1074append to a string, or save to a file for later use.
1075.Sh "\s-1CREATING\s0 A \s-1STANDARD\s0 \s-1HTTP\s0 \s-1HEADER:\s0"
1076.IX Subsection "CREATING A STANDARD HTTP HEADER:"
1077Normally the first thing you will do in any \s-1CGI\s0 script is print out an
1078\&\s-1HTTP\s0 header. This tells the browser what type of document to expect,
1079and gives other optional information, such as the language, expiration
1080date, and whether to cache the document. The header can also be
1081manipulated for special purposes, such as server push and pay per view
1082pages.
1083.PP
1084.Vb 1
1085\& print header;
1086.Ve
1087.PP
1088.Vb 1
1089\& -or-
1090.Ve
1091.PP
1092.Vb 1
1093\& print header('image/gif');
1094.Ve
1095.PP
1096.Vb 1
1097\& -or-
1098.Ve
1099.PP
1100.Vb 1
1101\& print header('text/html','204 No response');
1102.Ve
1103.PP
1104.Vb 1
1105\& -or-
1106.Ve
1107.PP
1108.Vb 8
1109\& print header(-type=>'image/gif',
1110\& -nph=>1,
1111\& -status=>'402 Payment required',
1112\& -expires=>'+3d',
1113\& -cookie=>$cookie,
1114\& -charset=>'utf-7',
1115\& -attachment=>'foo.gif',
1116\& -Cost=>'$2.00');
1117.Ve
1118.PP
1119\&\fIheader()\fR returns the Content\-type: header. You can provide your own
1120\&\s-1MIME\s0 type if you choose, otherwise it defaults to text/html. An
1121optional second parameter specifies the status code and a human-readable
1122message. For example, you can specify 204, \*(L"No response\*(R" to create a
1123script that tells the browser to do nothing at all.
1124.PP
1125The last example shows the named argument style for passing arguments
1126to the \s-1CGI\s0 methods using named parameters. Recognized parameters are
1127\&\fB\-type\fR, \fB\-status\fR, \fB\-expires\fR, and \fB\-cookie\fR. Any other named
1128parameters will be stripped of their initial hyphens and turned into
1129header fields, allowing you to specify any \s-1HTTP\s0 header you desire.
1130Internal underscores will be turned into hyphens:
1131.PP
1132.Vb 1
1133\& print header(-Content_length=>3002);
1134.Ve
1135.PP
1136Most browsers will not cache the output from \s-1CGI\s0 scripts. Every time
1137the browser reloads the page, the script is invoked anew. You can
1138change this behavior with the \fB\-expires\fR parameter. When you specify
1139an absolute or relative expiration interval with this parameter, some
1140browsers and proxy servers will cache the script's output until the
1141indicated expiration date. The following forms are all valid for the
1142\&\-expires field:
1143.PP
1144.Vb 8
1145\& +30s 30 seconds from now
1146\& +10m ten minutes from now
1147\& +1h one hour from now
1148\& -1d yesterday (i.e. "ASAP!")
1149\& now immediately
1150\& +3M in three months
1151\& +10y in ten years time
1152\& Thursday, 25-Apr-1999 00:40:33 GMT at the indicated time & date
1153.Ve
1154.PP
1155The \fB\-cookie\fR parameter generates a header that tells the browser to provide
1156a \*(L"magic cookie\*(R" during all subsequent transactions with your script.
1157Netscape cookies have a special format that includes interesting attributes
1158such as expiration time. Use the \fIcookie()\fR method to create and retrieve
1159session cookies.
1160.PP
1161The \fB\-nph\fR parameter, if set to a true value, will issue the correct
1162headers to work with a \s-1NPH\s0 (no\-parse\-header) script. This is important
1163to use with certain servers that expect all their scripts to be \s-1NPH\s0.
1164.PP
1165The \fB\-charset\fR parameter can be used to control the character set
1166sent to the browser. If not provided, defaults to \s-1ISO\-8859\-1\s0. As a
1167side effect, this sets the \fIcharset()\fR method as well.
1168.PP
1169The \fB\-attachment\fR parameter can be used to turn the page into an
1170attachment. Instead of displaying the page, some browsers will prompt
1171the user to save it to disk. The value of the argument is the
1172suggested name for the saved file. In order for this to work, you may
1173have to set the \fB\-type\fR to \*(L"application/octet\-stream\*(R".
1174.PP
1175The \fB\-p3p\fR parameter will add a P3P tag to the outgoing header. The
1176parameter can be an arrayref or a space-delimited string of P3P tags.
1177For example:
1178.PP
1179.Vb 2
1180\& print header(-p3p=>[qw(CAO DSP LAW CURa)]);
1181\& print header(-p3p=>'CAO DSP LAW CURa');
1182.Ve
1183.PP
1184In either case, the outgoing header will be formatted as:
1185.PP
1186.Vb 1
1187\& P3P: policyref="/w3c/p3p.xml" cp="CAO DSP LAW CURa"
1188.Ve
1189.Sh "\s-1GENERATING\s0 A \s-1REDIRECTION\s0 \s-1HEADER\s0"
1190.IX Subsection "GENERATING A REDIRECTION HEADER"
1191.Vb 1
1192\& print redirect('http://somewhere.else/in/movie/land');
1193.Ve
1194.PP
1195Sometimes you don't want to produce a document yourself, but simply
1196redirect the browser elsewhere, perhaps choosing a \s-1URL\s0 based on the
1197time of day or the identity of the user.
1198.PP
1199The \fIredirect()\fR function redirects the browser to a different \s-1URL\s0. If
1200you use redirection like this, you should \fBnot\fR print out a header as
1201well.
1202.PP
1203You should always use full URLs (including the http: or ftp: part) in
1204redirection requests. Relative URLs will not work correctly.
1205.PP
1206You can also use named arguments:
1207.PP
1208.Vb 3
1209\& print redirect(-uri=>'http://somewhere.else/in/movie/land',
1210\& -nph=>1,
1211\& -status=>301);
1212.Ve
1213.PP
1214The \fB\-nph\fR parameter, if set to a true value, will issue the correct
1215headers to work with a \s-1NPH\s0 (no\-parse\-header) script. This is important
1216to use with certain servers, such as Microsoft \s-1IIS\s0, which
1217expect all their scripts to be \s-1NPH\s0.
1218.PP
1219The \fB\-status\fR parameter will set the status of the redirect. \s-1HTTP\s0
1220defines three different possible redirection status codes:
1221.PP
1222.Vb 3
1223\& 301 Moved Permanently
1224\& 302 Found
1225\& 303 See Other
1226.Ve
1227.PP
1228The default if not specified is 302, which means \*(L"moved temporarily.\*(R"
1229You may change the status to another status code if you wish. Be
1230advised that changing the status to anything other than 301, 302 or
1231303 will probably break redirection.
1232.Sh "\s-1CREATING\s0 \s-1THE\s0 \s-1HTML\s0 \s-1DOCUMENT\s0 \s-1HEADER\s0"
1233.IX Subsection "CREATING THE HTML DOCUMENT HEADER"
1234.Vb 8
1235\& print start_html(-title=>'Secrets of the Pyramids',
1236\& -author=>'fred@capricorn.org',
1237\& -base=>'true',
1238\& -target=>'_blank',
1239\& -meta=>{'keywords'=>'pharaoh secret mummy',
1240\& 'copyright'=>'copyright 1996 King Tut'},
1241\& -style=>{'src'=>'/styles/style1.css'},
1242\& -BGCOLOR=>'blue');
1243.Ve
1244.PP
1245After creating the \s-1HTTP\s0 header, most \s-1CGI\s0 scripts will start writing
1246out an \s-1HTML\s0 document. The \fIstart_html()\fR routine creates the top of the
1247page, along with a lot of optional information that controls the
1248page's appearance and behavior.
1249.PP
1250This method returns a canned \s-1HTML\s0 header and the opening <body> tag.
1251All parameters are optional. In the named parameter form, recognized
1252parameters are \-title, \-author, \-base, \-xbase, \-dtd, \-lang and \-target
1253(see below for the explanation). Any additional parameters you
1254provide, such as the Netscape unofficial \s-1BGCOLOR\s0 attribute, are added
1255to the <body> tag. Additional parameters must be proceeded by a
1256hyphen.
1257.PP
1258The argument \fB\-xbase\fR allows you to provide an \s-1HREF\s0 for the <base> tag
1259different from the current location, as in
1260.PP
1261.Vb 1
1262\& -xbase=>"http://home.mcom.com/"
1263.Ve
1264.PP
1265All relative links will be interpreted relative to this tag.
1266.PP
1267The argument \fB\-target\fR allows you to provide a default target frame
1268for all the links and fill-out forms on the page. \fBThis is a
1269non-standard \s-1HTTP\s0 feature which only works with Netscape browsers!\fR
1270See the Netscape documentation on frames for details of how to
1271manipulate this.
1272.PP
1273.Vb 1
1274\& -target=>"answer_window"
1275.Ve
1276.PP
1277All relative links will be interpreted relative to this tag.
1278You add arbitrary meta information to the header with the \fB\-meta\fR
1279argument. This argument expects a reference to an associative array
1280containing name/value pairs of meta information. These will be turned
1281into a series of header <meta> tags that look something like this:
1282.PP
1283.Vb 2
1284\& <meta name="keywords" content="pharaoh secret mummy">
1285\& <meta name="description" content="copyright 1996 King Tut">
1286.Ve
1287.PP
1288To create an HTTP-EQUIV type of <meta> tag, use \fB\-head\fR, described
1289below.
1290.PP
1291The \fB\-style\fR argument is used to incorporate cascading stylesheets
1292into your code. See the section on \s-1CASCADING\s0 \s-1STYLESHEETS\s0 for more
1293information.
1294.PP
1295The \fB\-lang\fR argument is used to incorporate a language attribute into
1296the <html> tag. For example:
1297.PP
1298.Vb 1
1299\& print $q->start_html(-lang=>'fr-CA');
1300.Ve
1301.PP
1302The default if not specified is \*(L"en\-US\*(R" for \s-1US\s0 English, unless the
1303\&\-dtd parameter specifies an \s-1HTML\s0 2.0 or 3.2 \s-1DTD\s0, in which case the
1304lang attribute is left off. You can force the lang attribute to left
1305off in other cases by passing an empty string (\-lang=>'').
1306.PP
1307The \fB\-encoding\fR argument can be used to specify the character set for
1308\&\s-1XHTML\s0. It defaults to iso\-8859\-1 if not specified.
1309.PP
1310The \fB\-declare_xml\fR argument, when used in conjunction with \s-1XHTML\s0,
1311will put a <?xml> declaration at the top of the \s-1HTML\s0 header. The sole
1312purpose of this declaration is to declare the character set
1313encoding. In the absence of \-declare_xml, the output \s-1HTML\s0 will contain
1314a <meta> tag that specifies the encoding, allowing the \s-1HTML\s0 to pass
1315most validators. The default for \-declare_xml is false.
1316.PP
1317You can place other arbitrary \s-1HTML\s0 elements to the <head> section with the
1318\&\fB\-head\fR tag. For example, to place the rarely-used <link> element in the
1319head section, use this:
1320.PP
1321.Vb 2
1322\& print start_html(-head=>Link({-rel=>'next',
1323\& -href=>'http://www.capricorn.com/s2.html'}));
1324.Ve
1325.PP
1326To incorporate multiple \s-1HTML\s0 elements into the <head> section, just pass an
1327array reference:
1328.PP
1329.Vb 7
1330\& print start_html(-head=>[
1331\& Link({-rel=>'next',
1332\& -href=>'http://www.capricorn.com/s2.html'}),
1333\& Link({-rel=>'previous',
1334\& -href=>'http://www.capricorn.com/s1.html'})
1335\& ]
1336\& );
1337.Ve
1338.PP
1339And here's how to create an HTTP-EQUIV <meta> tag:
1340.PP
1341.Vb 2
1342\& print start_html(-head=>meta({-http_equiv => 'Content-Type',
1343\& -content => 'text/html'}))
1344.Ve
1345.PP
1346\&\s-1JAVASCRIPTING:\s0 The \fB\-script\fR, \fB\-noScript\fR, \fB\-onLoad\fR,
1347\&\fB\-onMouseOver\fR, \fB\-onMouseOut\fR and \fB\-onUnload\fR parameters are used
1348to add Netscape JavaScript calls to your pages. \fB\-script\fR should
1349point to a block of text containing JavaScript function definitions.
1350This block will be placed within a <script> block inside the \s-1HTML\s0 (not
1351\&\s-1HTTP\s0) header. The block is placed in the header in order to give your
1352page a fighting chance of having all its JavaScript functions in place
1353even if the user presses the stop button before the page has loaded
1354completely. \s-1CGI\s0.pm attempts to format the script in such a way that
1355JavaScript-naive browsers will not choke on the code: unfortunately
1356there are some browsers, such as Chimera for Unix, that get confused
1357by it nevertheless.
1358.PP
1359The \fB\-onLoad\fR and \fB\-onUnload\fR parameters point to fragments of JavaScript
1360code to execute when the page is respectively opened and closed by the
1361browser. Usually these parameters are calls to functions defined in the
1362\&\fB\-script\fR field:
1363.PP
1364.Vb 20
1365\& $query = new CGI;
1366\& print header;
1367\& $JSCRIPT=<<END;
1368\& // Ask a silly question
1369\& function riddle_me_this() {
1370\& var r = prompt("What walks on four legs in the morning, " +
1371\& "two legs in the afternoon, " +
1372\& "and three legs in the evening?");
1373\& response(r);
1374\& }
1375\& // Get a silly answer
1376\& function response(answer) {
1377\& if (answer == "man")
1378\& alert("Right you are!");
1379\& else
1380\& alert("Wrong! Guess again.");
1381\& }
1382\& END
1383\& print start_html(-title=>'The Riddle of the Sphinx',
1384\& -script=>$JSCRIPT);
1385.Ve
1386.PP
1387Use the \fB\-noScript\fR parameter to pass some \s-1HTML\s0 text that will be displayed on
1388browsers that do not have JavaScript (or browsers where JavaScript is turned
1389off).
1390.PP
1391Netscape 3.0 recognizes several attributes of the <script> tag,
1392including \s-1LANGUAGE\s0 and \s-1SRC\s0. The latter is particularly interesting,
1393as it allows you to keep the JavaScript code in a file or \s-1CGI\s0 script
1394rather than cluttering up each page with the source. To use these
1395attributes pass a \s-1HASH\s0 reference in the \fB\-script\fR parameter containing
1396one or more of \-language, \-src, or \-code:
1397.PP
1398.Vb 4
1399\& print $q->start_html(-title=>'The Riddle of the Sphinx',
1400\& -script=>{-language=>'JAVASCRIPT',
1401\& -src=>'/javascript/sphinx.js'}
1402\& );
1403.Ve
1404.PP
1405.Vb 4
1406\& print $q->(-title=>'The Riddle of the Sphinx',
1407\& -script=>{-language=>'PERLSCRIPT',
1408\& -code=>'print "hello world!\en;"'}
1409\& );
1410.Ve
1411.PP
1412A final feature allows you to incorporate multiple <script> sections into the
1413header. Just pass the list of script sections as an array reference.
1414this allows you to specify different source files for different dialects
1415of JavaScript. Example:
1416.PP
1417.Vb 16
1418\& print $q->start_html(-title=>'The Riddle of the Sphinx',
1419\& -script=>[
1420\& { -language => 'JavaScript1.0',
1421\& -src => '/javascript/utilities10.js'
1422\& },
1423\& { -language => 'JavaScript1.1',
1424\& -src => '/javascript/utilities11.js'
1425\& },
1426\& { -language => 'JavaScript1.2',
1427\& -src => '/javascript/utilities12.js'
1428\& },
1429\& { -language => 'JavaScript28.2',
1430\& -src => '/javascript/utilities219.js'
1431\& }
1432\& ]
1433\& );
1434.Ve
1435.PP
1436If this looks a bit extreme, take my advice and stick with straight \s-1CGI\s0 scripting.
1437.PP
1438See
1439.PP
1440.Vb 1
1441\& http://home.netscape.com/eng/mozilla/2.0/handbook/javascript/
1442.Ve
1443.PP
1444for more information about JavaScript.
1445.PP
1446The old-style positional parameters are as follows:
1447.IP "\fBParameters:\fR" 4
1448.IX Item "Parameters:"
1449.PD 0
1450.IP "1." 4
1451.PD
1452The title
1453.IP "2." 4
1454The author's e\-mail address (will create a <link rev=\*(L"\s-1MADE\s0\*(R"> tag if present
1455.IP "3." 4
1456A 'true' flag if you want to include a <base> tag in the header. This
1457helps resolve relative addresses to absolute ones when the document is moved,
1458but makes the document hierarchy non\-portable. Use with care!
1459.IP "4, 5, 6..." 4
1460.IX Item "4, 5, 6..."
1461Any other parameters you want to include in the <body> tag. This is a good
1462place to put Netscape extensions, such as colors and wallpaper patterns.
1463.Sh "\s-1ENDING\s0 \s-1THE\s0 \s-1HTML\s0 \s-1DOCUMENT:\s0"
1464.IX Subsection "ENDING THE HTML DOCUMENT:"
1465.Vb 1
1466\& print end_html
1467.Ve
1468.PP
1469This ends an \s-1HTML\s0 document by printing the </body></html> tags.
1470.Sh "\s-1CREATING\s0 A SELF-REFERENCING \s-1URL\s0 \s-1THAT\s0 \s-1PRESERVES\s0 \s-1STATE\s0 \s-1INFORMATION:\s0"
1471.IX Subsection "CREATING A SELF-REFERENCING URL THAT PRESERVES STATE INFORMATION:"
1472.Vb 2
1473\& $myself = self_url;
1474\& print q(<a href="$myself">I'm talking to myself.</a>);
1475.Ve
1476.PP
1477\&\fIself_url()\fR will return a \s-1URL\s0, that, when selected, will reinvoke
1478this script with all its state information intact. This is most
1479useful when you want to jump around within the document using
1480internal anchors but you don't want to disrupt the current contents
1481of the form(s). Something like this will do the trick.
1482.PP
1483.Vb 4
1484\& $myself = self_url;
1485\& print "<a href=\e"$myself#table1\e">See table 1</a>";
1486\& print "<a href=\e"$myself#table2\e">See table 2</a>";
1487\& print "<a href=\e"$myself#yourself\e">See for yourself</a>";
1488.Ve
1489.PP
1490If you want more control over what's returned, using the \fB\f(BIurl()\fB\fR
1491method instead.
1492.PP
1493You can also retrieve the unprocessed query string with \fIquery_string()\fR:
1494.PP
1495.Vb 1
1496\& $the_string = query_string;
1497.Ve
1498.Sh "\s-1OBTAINING\s0 \s-1THE\s0 \s-1SCRIPT\s0'S \s-1URL\s0"
1499.IX Subsection "OBTAINING THE SCRIPT'S URL"
1500.Vb 7
1501\& $full_url = url();
1502\& $full_url = url(-full=>1); #alternative syntax
1503\& $relative_url = url(-relative=>1);
1504\& $absolute_url = url(-absolute=>1);
1505\& $url_with_path = url(-path_info=>1);
1506\& $url_with_path_and_query = url(-path_info=>1,-query=>1);
1507\& $netloc = url(-base => 1);
1508.Ve
1509.PP
1510\&\fB\f(BIurl()\fB\fR returns the script's \s-1URL\s0 in a variety of formats. Called
1511without any arguments, it returns the full form of the \s-1URL\s0, including
1512host name and port number
1513.PP
1514.Vb 1
1515\& http://your.host.com/path/to/script.cgi
1516.Ve
1517.PP
1518You can modify this format with the following named arguments:
1519.IP "\fB\-absolute\fR" 4
1520.IX Item "-absolute"
1521If true, produce an absolute \s-1URL\s0, e.g.
1522.Sp
1523.Vb 1
1524\& /path/to/script.cgi
1525.Ve
1526.IP "\fB\-relative\fR" 4
1527.IX Item "-relative"
1528Produce a relative \s-1URL\s0. This is useful if you want to reinvoke your
1529script with different parameters. For example:
1530.Sp
1531.Vb 1
1532\& script.cgi
1533.Ve
1534.IP "\fB\-full\fR" 4
1535.IX Item "-full"
1536Produce the full \s-1URL\s0, exactly as if called without any arguments.
1537This overrides the \-relative and \-absolute arguments.
1538.IP "\fB\-path\fR (\fB\-path_info\fR)" 4
1539.IX Item "-path (-path_info)"
1540Append the additional path information to the \s-1URL\s0. This can be
1541combined with \fB\-full\fR, \fB\-absolute\fR or \fB\-relative\fR. \fB\-path_info\fR
1542is provided as a synonym.
1543.IP "\fB\-query\fR (\fB\-query_string\fR)" 4
1544.IX Item "-query (-query_string)"
1545Append the query string to the \s-1URL\s0. This can be combined with
1546\&\fB\-full\fR, \fB\-absolute\fR or \fB\-relative\fR. \fB\-query_string\fR is provided
1547as a synonym.
1548.IP "\fB\-base\fR" 4
1549.IX Item "-base"
1550Generate just the protocol and net location, as in http://www.foo.com:8000
1551.IP "\fB\-rewrite\fR" 4
1552.IX Item "-rewrite"
1553If Apache's mod_rewrite is turned on, then the script name and path
1554info probably won't match the request that the user sent. Set
1555\&\-rewrite=>1 (default) to return URLs that match what the user sent
1556(the original request \s-1URI\s0). Set \-rewrite\->0 to return URLs that match
1557the \s-1URL\s0 after mod_rewrite's rules have run. Because the additional
1558path information only makes sense in the context of the rewritten \s-1URL\s0,
1559\&\-rewrite is set to false when you request path info in the \s-1URL\s0.
1560.Sh "\s-1MIXING\s0 \s-1POST\s0 \s-1AND\s0 \s-1URL\s0 \s-1PARAMETERS\s0"
1561.IX Subsection "MIXING POST AND URL PARAMETERS"
1562.Vb 1
1563\& $color = url_param('color');
1564.Ve
1565.PP
1566It is possible for a script to receive \s-1CGI\s0 parameters in the \s-1URL\s0 as
1567well as in the fill-out form by creating a form that POSTs to a \s-1URL\s0
1568containing a query string (a \*(L"?\*(R" mark followed by arguments). The
1569\&\fB\f(BIparam()\fB\fR method will always return the contents of the POSTed
1570fill-out form, ignoring the \s-1URL\s0's query string. To retrieve \s-1URL\s0
1571parameters, call the \fB\f(BIurl_param()\fB\fR method. Use it in the same way as
1572\&\fB\f(BIparam()\fB\fR. The main difference is that it allows you to read the
1573parameters, but not set them.
1574.PP
1575Under no circumstances will the contents of the \s-1URL\s0 query string
1576interfere with similarly-named \s-1CGI\s0 parameters in POSTed forms. If you
1577try to mix a \s-1URL\s0 query string with a form submitted with the \s-1GET\s0
1578method, the results will not be what you expect.
1579.SH "CREATING STANDARD HTML ELEMENTS:"
1580.IX Header "CREATING STANDARD HTML ELEMENTS:"
1581\&\s-1CGI\s0.pm defines general \s-1HTML\s0 shortcut methods for most, if not all of
1582the \s-1HTML\s0 3 and \s-1HTML\s0 4 tags. \s-1HTML\s0 shortcuts are named after a single
1583\&\s-1HTML\s0 element and return a fragment of \s-1HTML\s0 text that you can then
1584print or manipulate as you like. Each shortcut returns a fragment of
1585\&\s-1HTML\s0 code that you can append to a string, save to a file, or, most
1586commonly, print out so that it displays in the browser window.
1587.PP
1588This example shows how to use the \s-1HTML\s0 methods:
1589.PP
1590.Vb 7
1591\& print $q->blockquote(
1592\& "Many years ago on the island of",
1593\& $q->a({href=>"http://crete.org/"},"Crete"),
1594\& "there lived a Minotaur named",
1595\& $q->strong("Fred."),
1596\& ),
1597\& $q->hr;
1598.Ve
1599.PP
1600This results in the following \s-1HTML\s0 code (extra newlines have been
1601added for readability):
1602.PP
1603.Vb 6
1604\& <blockquote>
1605\& Many years ago on the island of
1606\& <a href="http://crete.org/">Crete</a> there lived
1607\& a minotaur named <strong>Fred.</strong>
1608\& </blockquote>
1609\& <hr>
1610.Ve
1611.PP
1612If you find the syntax for calling the \s-1HTML\s0 shortcuts awkward, you can
1613import them into your namespace and dispense with the object syntax
1614completely (see the next section for more details):
1615.PP
1616.Vb 8
1617\& use CGI ':standard';
1618\& print blockquote(
1619\& "Many years ago on the island of",
1620\& a({href=>"http://crete.org/"},"Crete"),
1621\& "there lived a minotaur named",
1622\& strong("Fred."),
1623\& ),
1624\& hr;
1625.Ve
1626.Sh "\s-1PROVIDING\s0 \s-1ARGUMENTS\s0 \s-1TO\s0 \s-1HTML\s0 \s-1SHORTCUTS\s0"
1627.IX Subsection "PROVIDING ARGUMENTS TO HTML SHORTCUTS"
1628The \s-1HTML\s0 methods will accept zero, one or multiple arguments. If you
1629provide no arguments, you get a single tag:
1630.PP
1631.Vb 1
1632\& print hr; # <hr>
1633.Ve
1634.PP
1635If you provide one or more string arguments, they are concatenated
1636together with spaces and placed between opening and closing tags:
1637.PP
1638.Vb 1
1639\& print h1("Chapter","1"); # <h1>Chapter 1</h1>"
1640.Ve
1641.PP
1642If the first argument is an associative array reference, then the keys
1643and values of the associative array become the \s-1HTML\s0 tag's attributes:
1644.PP
1645.Vb 2
1646\& print a({-href=>'fred.html',-target=>'_new'},
1647\& "Open a new frame");
1648.Ve
1649.PP
1650.Vb 1
1651\& <a href="fred.html",target="_new">Open a new frame</a>
1652.Ve
1653.PP
1654You may dispense with the dashes in front of the attribute names if
1655you prefer:
1656.PP
1657.Vb 1
1658\& print img {src=>'fred.gif',align=>'LEFT'};
1659.Ve
1660.PP
1661.Vb 1
1662\& <img align="LEFT" src="fred.gif">
1663.Ve
1664.PP
1665Sometimes an \s-1HTML\s0 tag attribute has no argument. For example, ordered
1666lists can be marked as \s-1COMPACT\s0. The syntax for this is an argument that
1667that points to an undef string:
1668.PP
1669.Vb 1
1670\& print ol({compact=>undef},li('one'),li('two'),li('three'));
1671.Ve
1672.PP
1673Prior to \s-1CGI\s0.pm version 2.41, providing an empty ('') string as an
1674attribute argument was the same as providing undef. However, this has
1675changed in order to accommodate those who want to create tags of the form
1676<img alt="">. The difference is shown in these two pieces of code:
1677.PP
1678.Vb 3
1679\& CODE RESULT
1680\& img({alt=>undef}) <img alt>
1681\& img({alt=>''}) <img alt="">
1682.Ve
1683.Sh "\s-1THE\s0 \s-1DISTRIBUTIVE\s0 \s-1PROPERTY\s0 \s-1OF\s0 \s-1HTML\s0 \s-1SHORTCUTS\s0"
1684.IX Subsection "THE DISTRIBUTIVE PROPERTY OF HTML SHORTCUTS"
1685One of the cool features of the \s-1HTML\s0 shortcuts is that they are
1686distributive. If you give them an argument consisting of a
1687\&\fBreference\fR to a list, the tag will be distributed across each
1688element of the list. For example, here's one way to make an ordered
1689list:
1690.PP
1691.Vb 3
1692\& print ul(
1693\& li({-type=>'disc'},['Sneezy','Doc','Sleepy','Happy'])
1694\& );
1695.Ve
1696.PP
1697This example will result in \s-1HTML\s0 output that looks like this:
1698.PP
1699.Vb 6
1700\& <ul>
1701\& <li type="disc">Sneezy</li>
1702\& <li type="disc">Doc</li>
1703\& <li type="disc">Sleepy</li>
1704\& <li type="disc">Happy</li>
1705\& </ul>
1706.Ve
1707.PP
1708This is extremely useful for creating tables. For example:
1709.PP
1710.Vb 11
1711\& print table({-border=>undef},
1712\& caption('When Should You Eat Your Vegetables?'),
1713\& Tr({-align=>CENTER,-valign=>TOP},
1714\& [
1715\& th(['Vegetable', 'Breakfast','Lunch','Dinner']),
1716\& td(['Tomatoes' , 'no', 'yes', 'yes']),
1717\& td(['Broccoli' , 'no', 'no', 'yes']),
1718\& td(['Onions' , 'yes','yes', 'yes'])
1719\& ]
1720\& )
1721\& );
1722.Ve
1723.Sh "\s-1HTML\s0 \s-1SHORTCUTS\s0 \s-1AND\s0 \s-1LIST\s0 \s-1INTERPOLATION\s0"
1724.IX Subsection "HTML SHORTCUTS AND LIST INTERPOLATION"
1725Consider this bit of code:
1726.PP
1727.Vb 1
1728\& print blockquote(em('Hi'),'mom!'));
1729.Ve
1730.PP
1731It will ordinarily return the string that you probably expect, namely:
1732.PP
1733.Vb 1
1734\& <blockquote><em>Hi</em> mom!</blockquote>
1735.Ve
1736.PP
1737Note the space between the element \*(L"Hi\*(R" and the element \*(L"mom!\*(R".
1738\&\s-1CGI\s0.pm puts the extra space there using array interpolation, which is
1739controlled by the magic $\*(L" variable. Sometimes this extra space is
1740not what you want, for example, when you are trying to align a series
1741of images. In this case, you can simply change the value of $\*(R" to an
1742empty string.
1743.PP
1744.Vb 4
1745\& {
1746\& local($") = '';
1747\& print blockquote(em('Hi'),'mom!'));
1748\& }
1749.Ve
1750.PP
1751I suggest you put the code in a block as shown here. Otherwise the
1752change to $" will affect all subsequent code until you explicitly
1753reset it.
1754.Sh "NON-STANDARD \s-1HTML\s0 \s-1SHORTCUTS\s0"
1755.IX Subsection "NON-STANDARD HTML SHORTCUTS"
1756A few \s-1HTML\s0 tags don't follow the standard pattern for various
1757reasons.
1758.PP
1759\&\fB\f(BIcomment()\fB\fR generates an \s-1HTML\s0 comment (<!\-\- comment \-\->). Call it
1760like
1761.PP
1762.Vb 1
1763\& print comment('here is my comment');
1764.Ve
1765.PP
1766Because of conflicts with built-in Perl functions, the following functions
1767begin with initial caps:
1768.PP
1769.Vb 6
1770\& Select
1771\& Tr
1772\& Link
1773\& Delete
1774\& Accept
1775\& Sub
1776.Ve
1777.PP
1778In addition, \fIstart_html()\fR, \fIend_html()\fR, \fIstart_form()\fR, \fIend_form()\fR,
1779\&\fIstart_multipart_form()\fR and all the fill-out form tags are special.
1780See their respective sections.
1781.Sh "\s-1AUTOESCAPING\s0 \s-1HTML\s0"
1782.IX Subsection "AUTOESCAPING HTML"
1783By default, all \s-1HTML\s0 that is emitted by the form-generating functions
1784is passed through a function called \fIescapeHTML()\fR:
1785.ie n .IP "$escaped_string = escapeHTML(""unescaped string"");" 4
1786.el .IP "$escaped_string = escapeHTML(``unescaped string'');" 4
1787.IX Item "$escaped_string = escapeHTML(unescaped string);"
1788Escape \s-1HTML\s0 formatting characters in a string.
1789.PP
1790Provided that you have specified a character set of \s-1ISO\-8859\-1\s0 (the
1791default), the standard \s-1HTML\s0 escaping rules will be used. The \*(L"<\*(R"
1792character becomes \*(L"&lt;\*(R", \*(L">\*(R" becomes \*(L"&gt;\*(R", \*(L"&\*(R" becomes \*(L"&amp;\*(R", and
1793the quote character becomes \*(L"&quot;\*(R". In addition, the hexadecimal
17940x8b and 0x9b characters, which some browsers incorrectly interpret
1795as the left and right angle-bracket characters, are replaced by their
1796numeric character entities (\*(L"&#8249\*(R" and \*(L"&#8250;\*(R"). If you manually change
1797the charset, either by calling the \fIcharset()\fR method explicitly or by
1798passing a \-charset argument to \fIheader()\fR, then \fBall\fR characters will
1799be replaced by their numeric entities, since \s-1CGI\s0.pm has no lookup
1800table for all the possible encodings.
1801.PP
1802The automatic escaping does not apply to other shortcuts, such as
1803\&\fIh1()\fR. You should call \fIescapeHTML()\fR yourself on untrusted data in
1804order to protect your pages against nasty tricks that people may enter
1805into guestbooks, etc.. To change the character set, use \fIcharset()\fR.
1806To turn autoescaping off completely, use \fIautoEscape\fR\|(0):
1807.IP "$charset = charset([$charset]);" 4
1808.IX Item "$charset = charset([$charset]);"
1809Get or set the current character set.
1810.IP "$flag = autoEscape([$flag]);" 4
1811.IX Item "$flag = autoEscape([$flag]);"
1812Get or set the value of the autoescape flag.
1813.Sh "PRETTY-PRINTING \s-1HTML\s0"
1814.IX Subsection "PRETTY-PRINTING HTML"
1815By default, all the \s-1HTML\s0 produced by these functions comes out as one
1816long line without carriage returns or indentation. This is yuck, but
1817it does reduce the size of the documents by 10\-20%. To get
1818pretty-printed output, please use CGI::Pretty, a subclass
1819contributed by Brian Paulsen.
1820.SH "CREATING FILL-OUT FORMS:"
1821.IX Header "CREATING FILL-OUT FORMS:"
1822\&\fIGeneral note\fR The various form-creating methods all return strings
1823to the caller, containing the tag or tags that will create the requested
1824form element. You are responsible for actually printing out these strings.
1825It's set up this way so that you can place formatting tags
1826around the form elements.
1827.PP
1828\&\fIAnother note\fR The default values that you specify for the forms are only
1829used the \fBfirst\fR time the script is invoked (when there is no query
1830string). On subsequent invocations of the script (when there is a query
1831string), the former values are used even if they are blank.
1832.PP
1833If you want to change the value of a field from its previous value, you have two
1834choices:
1835.PP
1836(1) call the \fIparam()\fR method to set it.
1837.PP
1838(2) use the \-override (alias \-force) parameter (a new feature in version 2.15).
1839This forces the default value to be used, regardless of the previous value:
1840.PP
1841.Vb 5
1842\& print textfield(-name=>'field_name',
1843\& -default=>'starting value',
1844\& -override=>1,
1845\& -size=>50,
1846\& -maxlength=>80);
1847.Ve
1848.PP
1849\&\fIYet another note\fR By default, the text and labels of form elements are
1850escaped according to \s-1HTML\s0 rules. This means that you can safely use
1851\&\*(L"<\s-1CLICK\s0 \s-1ME\s0>\*(R" as the label for a button. However, it also interferes with
1852your ability to incorporate special \s-1HTML\s0 character sequences, such as &Aacute;,
1853into your fields. If you wish to turn off automatic escaping, call the
1854\&\fIautoEscape()\fR method with a false value immediately after creating the \s-1CGI\s0 object:
1855.PP
1856.Vb 2
1857\& $query = new CGI;
1858\& autoEscape(undef);
1859.Ve
1860.PP
1861\&\fIA Lurking Trap!\fR Some of the form-element generating methods return
1862multiple tags. In a scalar context, the tags will be concatenated
1863together with spaces, or whatever is the current value of the $"
1864global. In a list context, the methods will return a list of
1865elements, allowing you to modify them if you wish. Usually you will
1866not notice this behavior, but beware of this:
1867.PP
1868.Vb 1
1869\& printf("%s\en",end_form())
1870.Ve
1871.PP
1872\&\fIend_form()\fR produces several tags, and only the first of them will be
1873printed because the format only expects one value.
1874.PP
1875<p>
1876.Sh "\s-1CREATING\s0 \s-1AN\s0 \s-1ISINDEX\s0 \s-1TAG\s0"
1877.IX Subsection "CREATING AN ISINDEX TAG"
1878.Vb 1
1879\& print isindex(-action=>$action);
1880.Ve
1881.PP
1882.Vb 1
1883\& -or-
1884.Ve
1885.PP
1886.Vb 1
1887\& print isindex($action);
1888.Ve
1889.PP
1890Prints out an <isindex> tag. Not very exciting. The parameter
1891\&\-action specifies the \s-1URL\s0 of the script to process the query. The
1892default is to process the query with the current script.
1893.Sh "\s-1STARTING\s0 \s-1AND\s0 \s-1ENDING\s0 A \s-1FORM\s0"
1894.IX Subsection "STARTING AND ENDING A FORM"
1895.Vb 5
1896\& print start_form(-method=>$method,
1897\& -action=>$action,
1898\& -enctype=>$encoding);
1899\& <... various form stuff ...>
1900\& print endform;
1901.Ve
1902.PP
1903.Vb 1
1904\& -or-
1905.Ve
1906.PP
1907.Vb 3
1908\& print start_form($method,$action,$encoding);
1909\& <... various form stuff ...>
1910\& print endform;
1911.Ve
1912.PP
1913\&\fIstart_form()\fR will return a <form> tag with the optional method,
1914action and form encoding that you specify. The defaults are:
1915.PP
1916.Vb 3
1917\& method: POST
1918\& action: this script
1919\& enctype: application/x-www-form-urlencoded
1920.Ve
1921.PP
1922\&\fIendform()\fR returns the closing </form> tag.
1923.PP
1924\&\fIStart_form()\fR's enctype argument tells the browser how to package the various
1925fields of the form before sending the form to the server. Two
1926values are possible:
1927.PP
1928\&\fBNote:\fR This method was previously named \fIstartform()\fR, and \fIstartform()\fR
1929is still recognized as an alias.
1930.IP "\fBapplication/x\-www\-form\-urlencoded\fR" 4
1931.IX Item "application/x-www-form-urlencoded"
1932This is the older type of encoding used by all browsers prior to
1933Netscape 2.0. It is compatible with many \s-1CGI\s0 scripts and is
1934suitable for short fields containing text data. For your
1935convenience, \s-1CGI\s0.pm stores the name of this encoding
1936type in \fB&CGI::URL_ENCODED\fR.
1937.IP "\fBmultipart/form\-data\fR" 4
1938.IX Item "multipart/form-data"
1939This is the newer type of encoding introduced by Netscape 2.0.
1940It is suitable for forms that contain very large fields or that
1941are intended for transferring binary data. Most importantly,
1942it enables the \*(L"file upload\*(R" feature of Netscape 2.0 forms. For
1943your convenience, \s-1CGI\s0.pm stores the name of this encoding type
1944in \fB&CGI::MULTIPART\fR
1945.Sp
1946Forms that use this type of encoding are not easily interpreted
1947by \s-1CGI\s0 scripts unless they use \s-1CGI\s0.pm or another library designed
1948to handle them.
1949.Sp
1950If \s-1XHTML\s0 is activated (the default), then forms will be automatically
1951created using this type of encoding.
1952.PP
1953For compatibility, the \fIstart_form()\fR method uses the older form of
1954encoding by default. If you want to use the newer form of encoding
1955by default, you can call \fB\f(BIstart_multipart_form()\fB\fR instead of
1956\&\fB\f(BIstart_form()\fB\fR.
1957.PP
1958\&\s-1JAVASCRIPTING:\s0 The \fB\-name\fR and \fB\-onSubmit\fR parameters are provided
1959for use with JavaScript. The \-name parameter gives the
1960form a name so that it can be identified and manipulated by
1961JavaScript functions. \-onSubmit should point to a JavaScript
1962function that will be executed just before the form is submitted to your
1963server. You can use this opportunity to check the contents of the form
1964for consistency and completeness. If you find something wrong, you
1965can put up an alert box or maybe fix things up yourself. You can
1966abort the submission by returning false from this function.
1967.PP
1968Usually the bulk of JavaScript functions are defined in a <script>
1969block in the \s-1HTML\s0 header and \-onSubmit points to one of these function
1970call. See \fIstart_html()\fR for details.
1971.Sh "\s-1FORM\s0 \s-1ELEMENTS\s0"
1972.IX Subsection "FORM ELEMENTS"
1973After starting a form, you will typically create one or more
1974textfields, popup menus, radio groups and other form elements. Each
1975of these elements takes a standard set of named arguments. Some
1976elements also have optional arguments. The standard arguments are as
1977follows:
1978.IP "\fB\-name\fR" 4
1979.IX Item "-name"
1980The name of the field. After submission this name can be used to
1981retrieve the field's value using the \fIparam()\fR method.
1982.IP "\fB\-value\fR, \fB\-values\fR" 4
1983.IX Item "-value, -values"
1984The initial value of the field which will be returned to the script
1985after form submission. Some form elements, such as text fields, take
1986a single scalar \-value argument. Others, such as popup menus, take a
1987reference to an array of values. The two arguments are synonyms.
1988.IP "\fB\-tabindex\fR" 4
1989.IX Item "-tabindex"
1990A numeric value that sets the order in which the form element receives
1991focus when the user presses the tab key. Elements with lower values
1992receive focus first.
1993.IP "\fB\-id\fR" 4
1994.IX Item "-id"
1995A string identifier that can be used to identify this element to
1996JavaScript and \s-1DHTML\s0.
1997.IP "\fB\-override\fR" 4
1998.IX Item "-override"
1999A boolean, which, if true, forces the element to take on the value
2000specified by \fB\-value\fR, overriding the sticky behavior described
2001earlier for the \fB\-no_sticky\fR pragma.
2002.IP "\fB\-onChange\fR, \fB\-onFocus\fR, \fB\-onBlur\fR, \fB\-onMouseOver\fR, \fB\-onMouseOut\fR, \fB\-onSelect\fR" 4
2003.IX Item "-onChange, -onFocus, -onBlur, -onMouseOver, -onMouseOut, -onSelect"
2004These are used to assign JavaScript event handlers. See the
2005JavaScripting section for more details.
2006.PP
2007Other common arguments are described in the next section. In addition
2008to these, all attributes described in the \s-1HTML\s0 specifications are
2009supported.
2010.Sh "\s-1CREATING\s0 A \s-1TEXT\s0 \s-1FIELD\s0"
2011.IX Subsection "CREATING A TEXT FIELD"
2012.Vb 5
2013\& print textfield(-name=>'field_name',
2014\& -value=>'starting value',
2015\& -size=>50,
2016\& -maxlength=>80);
2017\& -or-
2018.Ve
2019.PP
2020.Vb 1
2021\& print textfield('field_name','starting value',50,80);
2022.Ve
2023.PP
2024\&\fItextfield()\fR will return a text input field.
2025.IP "\fBParameters\fR" 4
2026.IX Item "Parameters"
2027.PD 0
2028.IP "1." 4
2029.PD
2030The first parameter is the required name for the field (\-name).
2031.IP "2." 4
2032The optional second parameter is the default starting value for the field
2033contents (\-value, formerly known as \-default).
2034.IP "3." 4
2035The optional third parameter is the size of the field in
2036 characters (\-size).
2037.IP "4." 4
2038The optional fourth parameter is the maximum number of characters the
2039 field will accept (\-maxlength).
2040.PP
2041As with all these methods, the field will be initialized with its
2042previous contents from earlier invocations of the script.
2043When the form is processed, the value of the text field can be
2044retrieved with:
2045.PP
2046.Vb 1
2047\& $value = param('foo');
2048.Ve
2049.PP
2050If you want to reset it from its initial value after the script has been
2051called once, you can do so like this:
2052.PP
2053.Vb 1
2054\& param('foo',"I'm taking over this value!");
2055.Ve
2056.Sh "\s-1CREATING\s0 A \s-1BIG\s0 \s-1TEXT\s0 \s-1FIELD\s0"
2057.IX Subsection "CREATING A BIG TEXT FIELD"
2058.Vb 4
2059\& print textarea(-name=>'foo',
2060\& -default=>'starting value',
2061\& -rows=>10,
2062\& -columns=>50);
2063.Ve
2064.PP
2065.Vb 1
2066\& -or
2067.Ve
2068.PP
2069.Vb 1
2070\& print textarea('foo','starting value',10,50);
2071.Ve
2072.PP
2073\&\fItextarea()\fR is just like textfield, but it allows you to specify
2074rows and columns for a multiline text entry box. You can provide
2075a starting value for the field, which can be long and contain
2076multiple lines.
2077.Sh "\s-1CREATING\s0 A \s-1PASSWORD\s0 \s-1FIELD\s0"
2078.IX Subsection "CREATING A PASSWORD FIELD"
2079.Vb 5
2080\& print password_field(-name=>'secret',
2081\& -value=>'starting value',
2082\& -size=>50,
2083\& -maxlength=>80);
2084\& -or-
2085.Ve
2086.PP
2087.Vb 1
2088\& print password_field('secret','starting value',50,80);
2089.Ve
2090.PP
2091\&\fIpassword_field()\fR is identical to \fItextfield()\fR, except that its contents
2092will be starred out on the web page.
2093.Sh "\s-1CREATING\s0 A \s-1FILE\s0 \s-1UPLOAD\s0 \s-1FIELD\s0"
2094.IX Subsection "CREATING A FILE UPLOAD FIELD"
2095.Vb 5
2096\& print filefield(-name=>'uploaded_file',
2097\& -default=>'starting value',
2098\& -size=>50,
2099\& -maxlength=>80);
2100\& -or-
2101.Ve
2102.PP
2103.Vb 1
2104\& print filefield('uploaded_file','starting value',50,80);
2105.Ve
2106.PP
2107\&\fIfilefield()\fR will return a file upload field for Netscape 2.0 browsers.
2108In order to take full advantage of this \fIyou must use the new
2109multipart encoding scheme\fR for the form. You can do this either
2110by calling \fB\f(BIstart_form()\fB\fR with an encoding type of \fB&CGI::MULTIPART\fR,
2111or by calling the new method \fB\f(BIstart_multipart_form()\fB\fR instead of
2112vanilla \fB\f(BIstart_form()\fB\fR.
2113.IP "\fBParameters\fR" 4
2114.IX Item "Parameters"
2115.PD 0
2116.IP "1." 4
2117.PD
2118The first parameter is the required name for the field (\-name).
2119.IP "2." 4
2120The optional second parameter is the starting value for the field contents
2121to be used as the default file name (\-default).
2122.Sp
2123For security reasons, browsers don't pay any attention to this field,
2124and so the starting value will always be blank. Worse, the field
2125loses its \*(L"sticky\*(R" behavior and forgets its previous contents. The
2126starting value field is called for in the \s-1HTML\s0 specification, however,
2127and possibly some browser will eventually provide support for it.
2128.IP "3." 4
2129The optional third parameter is the size of the field in
2130characters (\-size).
2131.IP "4." 4
2132The optional fourth parameter is the maximum number of characters the
2133field will accept (\-maxlength).
2134.PP
2135When the form is processed, you can retrieve the entered filename
2136by calling \fIparam()\fR:
2137.PP
2138.Vb 1
2139\& $filename = param('uploaded_file');
2140.Ve
2141.PP
2142Different browsers will return slightly different things for the
2143name. Some browsers return the filename only. Others return the full
2144path to the file, using the path conventions of the user's machine.
2145Regardless, the name returned is always the name of the file on the
2146\&\fIuser's\fR machine, and is unrelated to the name of the temporary file
2147that \s-1CGI\s0.pm creates during upload spooling (see below).
2148.PP
2149The filename returned is also a file handle. You can read the contents
2150of the file using standard Perl file reading calls:
2151.PP
2152.Vb 4
2153\& # Read a text file and print it out
2154\& while (<$filename>) {
2155\& print;
2156\& }
2157.Ve
2158.PP
2159.Vb 5
2160\& # Copy a binary file to somewhere safe
2161\& open (OUTFILE,">>/usr/local/web/users/feedback");
2162\& while ($bytesread=read($filename,$buffer,1024)) {
2163\& print OUTFILE $buffer;
2164\& }
2165.Ve
2166.PP
2167However, there are problems with the dual nature of the upload fields.
2168If you \f(CW\*(C`use strict\*(C'\fR, then Perl will complain when you try to use a
2169string as a filehandle. You can get around this by placing the file
2170reading code in a block containing the \f(CW\*(C`no strict\*(C'\fR pragma. More
2171seriously, it is possible for the remote user to type garbage into the
2172upload field, in which case what you get from \fIparam()\fR is not a
2173filehandle at all, but a string.
2174.PP
2175To be safe, use the \fI\fIupload()\fI\fR function (new in version 2.47). When
2176called with the name of an upload field, \fI\fIupload()\fI\fR returns a
2177filehandle, or undef if the parameter is not a valid filehandle.
2178.PP
2179.Vb 4
2180\& $fh = upload('uploaded_file');
2181\& while (<$fh>) {
2182\& print;
2183\& }
2184.Ve
2185.PP
2186In an list context, \fIupload()\fR will return an array of filehandles.
2187This makes it possible to create forms that use the same name for
2188multiple upload fields.
2189.PP
2190This is the recommended idiom.
2191.PP
2192When a file is uploaded the browser usually sends along some
2193information along with it in the format of headers. The information
2194usually includes the \s-1MIME\s0 content type. Future browsers may send
2195other information as well (such as modification date and size). To
2196retrieve this information, call \fIuploadInfo()\fR. It returns a reference to
2197an associative array containing all the document headers.
2198.PP
2199.Vb 5
2200\& $filename = param('uploaded_file');
2201\& $type = uploadInfo($filename)->{'Content-Type'};
2202\& unless ($type eq 'text/html') {
2203\& die "HTML FILES ONLY!";
2204\& }
2205.Ve
2206.PP
2207If you are using a machine that recognizes \*(L"text\*(R" and \*(L"binary\*(R" data
2208modes, be sure to understand when and how to use them (see the Camel book).
2209Otherwise you may find that binary files are corrupted during file
2210uploads.
2211.PP
2212There are occasionally problems involving parsing the uploaded file.
2213This usually happens when the user presses \*(L"Stop\*(R" before the upload is
2214finished. In this case, \s-1CGI\s0.pm will return undef for the name of the
2215uploaded file and set \fI\fIcgi_error()\fI\fR to the string \*(L"400 Bad request
2216(malformed multipart \s-1POST\s0)\*(R". This error message is designed so that
2217you can incorporate it into a status code to be sent to the browser.
2218Example:
2219.PP
2220.Vb 5
2221\& $file = upload('uploaded_file');
2222\& if (!$file && cgi_error) {
2223\& print header(-status=>cgi_error);
2224\& exit 0;
2225\& }
2226.Ve
2227.PP
2228You are free to create a custom \s-1HTML\s0 page to complain about the error,
2229if you wish.
2230.PP
2231You can set up a callback that will be called whenever a file upload
2232is being read during the form processing. This is much like the
2233\&\s-1UPLOAD_HOOK\s0 facility available in Apache::Request, with the exception
2234that the first argument to the callback is an Apache::Upload object,
2235here it's the remote filename.
2236.PP
2237.Vb 1
2238\& $q = CGI->new(\e&hook,$data);
2239.Ve
2240.PP
2241.Vb 5
2242\& sub hook
2243\& {
2244\& my ($filename, $buffer, $bytes_read, $data) = @_;
2245\& print "Read $bytes_read bytes of $filename\en";
2246\& }
2247.Ve
2248.PP
2249If using the function-oriented interface, call the \fICGI::upload_hook()\fR
2250method before calling \fIparam()\fR or any other \s-1CGI\s0 functions:
2251.PP
2252.Vb 1
2253\& CGI::upload_hook(\e&hook,$data);
2254.Ve
2255.PP
2256This method is not exported by default. You will have to import it
2257explicitly if you wish to use it without the \s-1CGI::\s0 prefix.
2258.PP
2259If you are using \s-1CGI\s0.pm on a Windows platform and find that binary
2260files get slightly larger when uploaded but that text files remain the
2261same, then you have forgotten to activate binary mode on the output
2262filehandle. Be sure to call \fIbinmode()\fR on any handle that you create
2263to write the uploaded file to disk.
2264.PP
2265\&\s-1JAVASCRIPTING:\s0 The \fB\-onChange\fR, \fB\-onFocus\fR, \fB\-onBlur\fR,
2266\&\fB\-onMouseOver\fR, \fB\-onMouseOut\fR and \fB\-onSelect\fR parameters are
2267recognized. See \fItextfield()\fR for details.
2268.Sh "\s-1CREATING\s0 A \s-1POPUP\s0 \s-1MENU\s0"
2269.IX Subsection "CREATING A POPUP MENU"
2270.Vb 3
2271\& print popup_menu('menu_name',
2272\& ['eenie','meenie','minie'],
2273\& 'meenie');
2274.Ve
2275.PP
2276.Vb 1
2277\& -or-
2278.Ve
2279.PP
2280.Vb 7
2281\& %labels = ('eenie'=>'your first choice',
2282\& 'meenie'=>'your second choice',
2283\& 'minie'=>'your third choice');
2284\& %attributes = ('eenie'=>{'class'=>'class of first choice'});
2285\& print popup_menu('menu_name',
2286\& ['eenie','meenie','minie'],
2287\& 'meenie',\e%labels,\e%attributes);
2288.Ve
2289.PP
2290.Vb 1
2291\& -or (named parameter style)-
2292.Ve
2293.PP
2294.Vb 5
2295\& print popup_menu(-name=>'menu_name',
2296\& -values=>['eenie','meenie','minie'],
2297\& -default=>'meenie',
2298\& -labels=>\e%labels,
2299\& -attributes=>\e%attributes);
2300.Ve
2301.PP
2302\&\fIpopup_menu()\fR creates a menu.
2303.IP "1." 4
2304The required first argument is the menu's name (\-name).
2305.IP "2." 4
2306The required second argument (\-values) is an array \fBreference\fR
2307containing the list of menu items in the menu. You can pass the
2308method an anonymous array, as shown in the example, or a reference to
2309a named array, such as \*(L"\e@foo\*(R".
2310.IP "3." 4
2311The optional third parameter (\-default) is the name of the default
2312menu choice. If not specified, the first item will be the default.
2313The values of the previous choice will be maintained across queries.
2314.IP "4." 4
2315The optional fourth parameter (\-labels) is provided for people who
2316want to use different values for the user-visible label inside the
2317popup menu and the value returned to your script. It's a pointer to an
2318associative array relating menu values to user-visible labels. If you
2319leave this parameter blank, the menu values will be displayed by
2320default. (You can also leave a label undefined if you want to).
2321.IP "5." 4
2322The optional fifth parameter (\-attributes) is provided to assign
2323any of the common \s-1HTML\s0 attributes to an individual menu item. It's
2324a pointer to an associative array relating menu values to another
2325associative array with the attribute's name as the key and the
2326attribute's value as the value.
2327.PP
2328When the form is processed, the selected value of the popup menu can
2329be retrieved using:
2330.PP
2331.Vb 1
2332\& $popup_menu_value = param('menu_name');
2333.Ve
2334.Sh "\s-1CREATING\s0 \s-1AN\s0 \s-1OPTION\s0 \s-1GROUP\s0"
2335.IX Subsection "CREATING AN OPTION GROUP"
2336Named parameter style
2337.PP
2338.Vb 9
2339\& print popup_menu(-name=>'menu_name',
2340\& -values=>[qw/eenie meenie minie/,
2341\& optgroup(-name=>'optgroup_name',
2342\& -values => ['moe','catch'],
2343\& -attributes=>{'catch'=>{'class'=>'red'}})],
2344\& -labels=>{'eenie'=>'one',
2345\& 'meenie'=>'two',
2346\& 'minie'=>'three'},
2347\& -default=>'meenie');
2348.Ve
2349.PP
2350.Vb 6
2351\& Old style
2352\& print popup_menu('menu_name',
2353\& ['eenie','meenie','minie',
2354\& optgroup('optgroup_name', ['moe', 'catch'],
2355\& {'catch'=>{'class'=>'red'}})],'meenie',
2356\& {'eenie'=>'one','meenie'=>'two','minie'=>'three'});
2357.Ve
2358.PP
2359\&\fIoptgroup()\fR creates an option group within a popup menu.
2360.IP "1." 4
2361The required first argument (\fB\-name\fR) is the label attribute of the
2362optgroup and is \fBnot\fR inserted in the parameter list of the query.
2363.IP "2." 4
2364The required second argument (\fB\-values\fR) is an array reference
2365containing the list of menu items in the menu. You can pass the
2366method an anonymous array, as shown in the example, or a reference
2367to a named array, such as \e@foo. If you pass a \s-1HASH\s0 reference,
2368the keys will be used for the menu values, and the values will be
2369used for the menu labels (see \-labels below).
2370.IP "3." 4
2371The optional third parameter (\fB\-labels\fR) allows you to pass a reference
2372to an associative array containing user-visible labels for one or more
2373of the menu items. You can use this when you want the user to see one
2374menu string, but have the browser return your program a different one.
2375If you don't specify this, the value string will be used instead
2376(\*(L"eenie\*(R", \*(L"meenie\*(R" and \*(L"minie\*(R" in this example). This is equivalent
2377to using a hash reference for the \-values parameter.
2378.IP "4." 4
2379An optional fourth parameter (\fB\-labeled\fR) can be set to a true value
2380and indicates that the values should be used as the label attribute
2381for each option element within the optgroup.
2382.IP "5." 4
2383An optional fifth parameter (\-novals) can be set to a true value and
2384indicates to suppress the val attribut in each option element within
2385the optgroup.
2386.Sp
2387See the discussion on optgroup at W3C
2388(http://www.w3.org/TR/REC\-html40/interact/forms.html#edef\-OPTGROUP)
2389for details.
2390.IP "6." 4
2391An optional sixth parameter (\-attributes) is provided to assign
2392any of the common \s-1HTML\s0 attributes to an individual menu item. It's
2393a pointer to an associative array relating menu values to another
2394associative array with the attribute's name as the key and the
2395attribute's value as the value.
2396.Sh "\s-1CREATING\s0 A \s-1SCROLLING\s0 \s-1LIST\s0"
2397.IX Subsection "CREATING A SCROLLING LIST"
2398.Vb 4
2399\& print scrolling_list('list_name',
2400\& ['eenie','meenie','minie','moe'],
2401\& ['eenie','moe'],5,'true',{'moe'=>{'class'=>'red'}});
2402\& -or-
2403.Ve
2404.PP
2405.Vb 4
2406\& print scrolling_list('list_name',
2407\& ['eenie','meenie','minie','moe'],
2408\& ['eenie','moe'],5,'true',
2409\& \e%labels,%attributes);
2410.Ve
2411.PP
2412.Vb 1
2413\& -or-
2414.Ve
2415.PP
2416.Vb 7
2417\& print scrolling_list(-name=>'list_name',
2418\& -values=>['eenie','meenie','minie','moe'],
2419\& -default=>['eenie','moe'],
2420\& -size=>5,
2421\& -multiple=>'true',
2422\& -labels=>\e%labels,
2423\& -attributes=>\e%attributes);
2424.Ve
2425.PP
2426\&\fIscrolling_list()\fR creates a scrolling list.
2427.IP "\fBParameters:\fR" 4
2428.IX Item "Parameters:"
2429.PD 0
2430.IP "1." 4
2431.PD
2432The first and second arguments are the list name (\-name) and values
2433(\-values). As in the popup menu, the second argument should be an
2434array reference.
2435.IP "2." 4
2436The optional third argument (\-default) can be either a reference to a
2437list containing the values to be selected by default, or can be a
2438single value to select. If this argument is missing or undefined,
2439then nothing is selected when the list first appears. In the named
2440parameter version, you can use the synonym \*(L"\-defaults\*(R" for this
2441parameter.
2442.IP "3." 4
2443The optional fourth argument is the size of the list (\-size).
2444.IP "4." 4
2445The optional fifth argument can be set to true to allow multiple
2446simultaneous selections (\-multiple). Otherwise only one selection
2447will be allowed at a time.
2448.IP "5." 4
2449The optional sixth argument is a pointer to an associative array
2450containing long user-visible labels for the list items (\-labels).
2451If not provided, the values will be displayed.
2452.IP "6." 4
2453The optional sixth parameter (\-attributes) is provided to assign
2454any of the common \s-1HTML\s0 attributes to an individual menu item. It's
2455a pointer to an associative array relating menu values to another
2456associative array with the attribute's name as the key and the
2457attribute's value as the value.
2458.Sp
2459When this form is processed, all selected list items will be returned as
2460a list under the parameter name 'list_name'. The values of the
2461selected items can be retrieved with:
2462.Sp
2463.Vb 1
2464\& @selected = param('list_name');
2465.Ve
2466.Sh "\s-1CREATING\s0 A \s-1GROUP\s0 \s-1OF\s0 \s-1RELATED\s0 \s-1CHECKBOXES\s0"
2467.IX Subsection "CREATING A GROUP OF RELATED CHECKBOXES"
2468.Vb 6
2469\& print checkbox_group(-name=>'group_name',
2470\& -values=>['eenie','meenie','minie','moe'],
2471\& -default=>['eenie','moe'],
2472\& -linebreak=>'true',
2473\& -labels=>\e%labels,
2474\& -attributes=>\e%attributes);
2475.Ve
2476.PP
2477.Vb 4
2478\& print checkbox_group('group_name',
2479\& ['eenie','meenie','minie','moe'],
2480\& ['eenie','moe'],'true',\e%labels,
2481\& {'moe'=>{'class'=>'red'}});
2482.Ve
2483.PP
2484.Vb 1
2485\& HTML3-COMPATIBLE BROWSERS ONLY:
2486.Ve
2487.PP
2488.Vb 3
2489\& print checkbox_group(-name=>'group_name',
2490\& -values=>['eenie','meenie','minie','moe'],
2491\& -rows=2,-columns=>2);
2492.Ve
2493.PP
2494\&\fIcheckbox_group()\fR creates a list of checkboxes that are related
2495by the same name.
2496.IP "\fBParameters:\fR" 4
2497.IX Item "Parameters:"
2498.PD 0
2499.IP "1." 4
2500.PD
2501The first and second arguments are the checkbox name and values,
2502respectively (\-name and \-values). As in the popup menu, the second
2503argument should be an array reference. These values are used for the
2504user-readable labels printed next to the checkboxes as well as for the
2505values passed to your script in the query string.
2506.IP "2." 4
2507The optional third argument (\-default) can be either a reference to a
2508list containing the values to be checked by default, or can be a
2509single value to checked. If this argument is missing or undefined,
2510then nothing is selected when the list first appears.
2511.IP "3." 4
2512The optional fourth argument (\-linebreak) can be set to true to place
2513line breaks between the checkboxes so that they appear as a vertical
2514list. Otherwise, they will be strung together on a horizontal line.
2515.PP
2516The optional b<\-labels> argument is a pointer to an associative array
2517relating the checkbox values to the user-visible labels that will be
2518printed next to them. If not provided, the values will be used as the
2519default.
2520.PP
2521Modern browsers can take advantage of the optional parameters
2522\&\fB\-rows\fR, and \fB\-columns\fR. These parameters cause \fIcheckbox_group()\fR to
2523return an \s-1HTML3\s0 compatible table containing the checkbox group
2524formatted with the specified number of rows and columns. You can
2525provide just the \-columns parameter if you wish; checkbox_group will
2526calculate the correct number of rows for you.
2527.PP
2528The optional \fB\-attributes\fR argument is provided to assign any of the
2529common \s-1HTML\s0 attributes to an individual menu item. It's a pointer to
2530an associative array relating menu values to another associative array
2531with the attribute's name as the key and the attribute's value as the
2532value.
2533.PP
2534The optional \fB\-tabindex\fR argument can be used to control the order in which
2535radio buttons receive focus when the user presses the tab button. If
2536passed a scalar numeric value, the first element in the group will
2537receive this tab index and subsequent elements will be incremented by
2538one. If given a reference to an array of radio button values, then
2539the indexes will be jiggered so that the order specified in the array
2540will correspond to the tab order. You can also pass a reference to a
2541hash in which the hash keys are the radio button values and the values
2542are the tab indexes of each button. Examples:
2543.PP
2544.Vb 3
2545\& -tabindex => 100 # this group starts at index 100 and counts up
2546\& -tabindex => ['moe','minie','eenie','meenie'] # tab in this order
2547\& -tabindex => {meenie=>100,moe=>101,minie=>102,eenie=>200} # tab in this order
2548.Ve
2549.PP
2550When the form is processed, all checked boxes will be returned as
2551a list under the parameter name 'group_name'. The values of the
2552\&\*(L"on\*(R" checkboxes can be retrieved with:
2553.PP
2554.Vb 1
2555\& @turned_on = param('group_name');
2556.Ve
2557.PP
2558The value returned by \fIcheckbox_group()\fR is actually an array of button
2559elements. You can capture them and use them within tables, lists,
2560or in other creative ways:
2561.PP
2562.Vb 2
2563\& @h = checkbox_group(-name=>'group_name',-values=>\e@values);
2564\& &use_in_creative_way(@h);
2565.Ve
2566.Sh "\s-1CREATING\s0 A \s-1STANDALONE\s0 \s-1CHECKBOX\s0"
2567.IX Subsection "CREATING A STANDALONE CHECKBOX"
2568.Vb 4
2569\& print checkbox(-name=>'checkbox_name',
2570\& -checked=>1,
2571\& -value=>'ON',
2572\& -label=>'CLICK ME');
2573.Ve
2574.PP
2575.Vb 1
2576\& -or-
2577.Ve
2578.PP
2579.Vb 1
2580\& print checkbox('checkbox_name','checked','ON','CLICK ME');
2581.Ve
2582.PP
2583\&\fIcheckbox()\fR is used to create an isolated checkbox that isn't logically
2584related to any others.
2585.IP "\fBParameters:\fR" 4
2586.IX Item "Parameters:"
2587.PD 0
2588.IP "1." 4
2589.PD
2590The first parameter is the required name for the checkbox (\-name). It
2591will also be used for the user-readable label printed next to the
2592checkbox.
2593.IP "2." 4
2594The optional second parameter (\-checked) specifies that the checkbox
2595is turned on by default. Synonyms are \-selected and \-on.
2596.IP "3." 4
2597The optional third parameter (\-value) specifies the value of the
2598checkbox when it is checked. If not provided, the word \*(L"on\*(R" is
2599assumed.
2600.IP "4." 4
2601The optional fourth parameter (\-label) is the user-readable label to
2602be attached to the checkbox. If not provided, the checkbox name is
2603used.
2604.PP
2605The value of the checkbox can be retrieved using:
2606.PP
2607.Vb 1
2608\& $turned_on = param('checkbox_name');
2609.Ve
2610.Sh "\s-1CREATING\s0 A \s-1RADIO\s0 \s-1BUTTON\s0 \s-1GROUP\s0"
2611.IX Subsection "CREATING A RADIO BUTTON GROUP"
2612.Vb 6
2613\& print radio_group(-name=>'group_name',
2614\& -values=>['eenie','meenie','minie'],
2615\& -default=>'meenie',
2616\& -linebreak=>'true',
2617\& -labels=>\e%labels,
2618\& -attributes=>\e%attributes);
2619.Ve
2620.PP
2621.Vb 1
2622\& -or-
2623.Ve
2624.PP
2625.Vb 2
2626\& print radio_group('group_name',['eenie','meenie','minie'],
2627\& 'meenie','true',\e%labels,\e%attributes);
2628.Ve
2629.PP
2630.Vb 1
2631\& HTML3-COMPATIBLE BROWSERS ONLY:
2632.Ve
2633.PP
2634.Vb 3
2635\& print radio_group(-name=>'group_name',
2636\& -values=>['eenie','meenie','minie','moe'],
2637\& -rows=2,-columns=>2);
2638.Ve
2639.PP
2640\&\fIradio_group()\fR creates a set of logically-related radio buttons
2641(turning one member of the group on turns the others off)
2642.IP "\fBParameters:\fR" 4
2643.IX Item "Parameters:"
2644.PD 0
2645.IP "1." 4
2646.PD
2647The first argument is the name of the group and is required (\-name).
2648.IP "2." 4
2649The second argument (\-values) is the list of values for the radio
2650buttons. The values and the labels that appear on the page are
2651identical. Pass an array \fIreference\fR in the second argument, either
2652using an anonymous array, as shown, or by referencing a named array as
2653in \*(L"\e@foo\*(R".
2654.IP "3." 4
2655The optional third parameter (\-default) is the name of the default
2656button to turn on. If not specified, the first item will be the
2657default. You can provide a nonexistent button name, such as \*(L"\-\*(R" to
2658start up with no buttons selected.
2659.IP "4." 4
2660The optional fourth parameter (\-linebreak) can be set to 'true' to put
2661line breaks between the buttons, creating a vertical list.
2662.IP "5." 4
2663The optional fifth parameter (\-labels) is a pointer to an associative
2664array relating the radio button values to user-visible labels to be
2665used in the display. If not provided, the values themselves are
2666displayed.
2667.PP
2668All modern browsers can take advantage of the optional parameters
2669\&\fB\-rows\fR, and \fB\-columns\fR. These parameters cause \fIradio_group()\fR to
2670return an \s-1HTML3\s0 compatible table containing the radio group formatted
2671with the specified number of rows and columns. You can provide just
2672the \-columns parameter if you wish; radio_group will calculate the
2673correct number of rows for you.
2674.PP
2675To include row and column headings in the returned table, you
2676can use the \fB\-rowheader\fR and \fB\-colheader\fR parameters. Both
2677of these accept a pointer to an array of headings to use.
2678The headings are just decorative. They don't reorganize the
2679interpretation of the radio buttons \*(-- they're still a single named
2680unit.
2681.PP
2682The optional \fB\-tabindex\fR argument can be used to control the order in which
2683radio buttons receive focus when the user presses the tab button. If
2684passed a scalar numeric value, the first element in the group will
2685receive this tab index and subsequent elements will be incremented by
2686one. If given a reference to an array of radio button values, then
2687the indexes will be jiggered so that the order specified in the array
2688will correspond to the tab order. You can also pass a reference to a
2689hash in which the hash keys are the radio button values and the values
2690are the tab indexes of each button. Examples:
2691.PP
2692.Vb 3
2693\& -tabindex => 100 # this group starts at index 100 and counts up
2694\& -tabindex => ['moe','minie','eenie','meenie'] # tab in this order
2695\& -tabindex => {meenie=>100,moe=>101,minie=>102,eenie=>200} # tab in this order
2696.Ve
2697.PP
2698The optional \fB\-attributes\fR argument is provided to assign any of the
2699common \s-1HTML\s0 attributes to an individual menu item. It's a pointer to
2700an associative array relating menu values to another associative array
2701with the attribute's name as the key and the attribute's value as the
2702value.
2703.PP
2704When the form is processed, the selected radio button can
2705be retrieved using:
2706.PP
2707.Vb 1
2708\& $which_radio_button = param('group_name');
2709.Ve
2710.PP
2711The value returned by \fIradio_group()\fR is actually an array of button
2712elements. You can capture them and use them within tables, lists,
2713or in other creative ways:
2714.PP
2715.Vb 2
2716\& @h = radio_group(-name=>'group_name',-values=>\e@values);
2717\& &use_in_creative_way(@h);
2718.Ve
2719.Sh "\s-1CREATING\s0 A \s-1SUBMIT\s0 \s-1BUTTON\s0"
2720.IX Subsection "CREATING A SUBMIT BUTTON"
2721.Vb 2
2722\& print submit(-name=>'button_name',
2723\& -value=>'value');
2724.Ve
2725.PP
2726.Vb 1
2727\& -or-
2728.Ve
2729.PP
2730.Vb 1
2731\& print submit('button_name','value');
2732.Ve
2733.PP
2734\&\fIsubmit()\fR will create the query submission button. Every form
2735should have one of these.
2736.IP "\fBParameters:\fR" 4
2737.IX Item "Parameters:"
2738.PD 0
2739.IP "1." 4
2740.PD
2741The first argument (\-name) is optional. You can give the button a
2742name if you have several submission buttons in your form and you want
2743to distinguish between them.
2744.IP "2." 4
2745The second argument (\-value) is also optional. This gives the button
2746a value that will be passed to your script in the query string. The
2747name will also be used as the user-visible label.
2748.IP "3." 4
2749You can use \-label as an alias for \-value. I always get confused
2750about which of \-name and \-value changes the user-visible label on the
2751button.
2752.PP
2753You can figure out which button was pressed by using different
2754values for each one:
2755.PP
2756.Vb 1
2757\& $which_one = param('button_name');
2758.Ve
2759.Sh "\s-1CREATING\s0 A \s-1RESET\s0 \s-1BUTTON\s0"
2760.IX Subsection "CREATING A RESET BUTTON"
2761.Vb 1
2762\& print reset
2763.Ve
2764.PP
2765\&\fIreset()\fR creates the \*(L"reset\*(R" button. Note that it restores the
2766form to its value from the last time the script was called,
2767\&\s-1NOT\s0 necessarily to the defaults.
2768.PP
2769Note that this conflicts with the Perl \fIreset()\fR built\-in. Use
2770\&\fICORE::reset()\fR to get the original reset function.
2771.Sh "\s-1CREATING\s0 A \s-1DEFAULT\s0 \s-1BUTTON\s0"
2772.IX Subsection "CREATING A DEFAULT BUTTON"
2773.Vb 1
2774\& print defaults('button_label')
2775.Ve
2776.PP
2777\&\fIdefaults()\fR creates a button that, when invoked, will cause the
2778form to be completely reset to its defaults, wiping out all the
2779changes the user ever made.
2780.Sh "\s-1CREATING\s0 A \s-1HIDDEN\s0 \s-1FIELD\s0"
2781.IX Subsection "CREATING A HIDDEN FIELD"
2782.Vb 2
2783\& print hidden(-name=>'hidden_name',
2784\& -default=>['value1','value2'...]);
2785.Ve
2786.PP
2787.Vb 1
2788\& -or-
2789.Ve
2790.PP
2791.Vb 1
2792\& print hidden('hidden_name','value1','value2'...);
2793.Ve
2794.PP
2795\&\fIhidden()\fR produces a text field that can't be seen by the user. It
2796is useful for passing state variable information from one invocation
2797of the script to the next.
2798.IP "\fBParameters:\fR" 4
2799.IX Item "Parameters:"
2800.PD 0
2801.IP "1." 4
2802.PD
2803The first argument is required and specifies the name of this
2804field (\-name).
2805.IP "2." 4
2806The second argument is also required and specifies its value
2807(\-default). In the named parameter style of calling, you can provide
2808a single value here or a reference to a whole list
2809.PP
2810Fetch the value of a hidden field this way:
2811.PP
2812.Vb 1
2813\& $hidden_value = param('hidden_name');
2814.Ve
2815.PP
2816Note, that just like all the other form elements, the value of a
2817hidden field is \*(L"sticky\*(R". If you want to replace a hidden field with
2818some other values after the script has been called once you'll have to
2819do it manually:
2820.PP
2821.Vb 1
2822\& param('hidden_name','new','values','here');
2823.Ve
2824.Sh "\s-1CREATING\s0 A \s-1CLICKABLE\s0 \s-1IMAGE\s0 \s-1BUTTON\s0"
2825.IX Subsection "CREATING A CLICKABLE IMAGE BUTTON"
2826.Vb 3
2827\& print image_button(-name=>'button_name',
2828\& -src=>'/source/URL',
2829\& -align=>'MIDDLE');
2830.Ve
2831.PP
2832.Vb 1
2833\& -or-
2834.Ve
2835.PP
2836.Vb 1
2837\& print image_button('button_name','/source/URL','MIDDLE');
2838.Ve
2839.PP
2840\&\fIimage_button()\fR produces a clickable image. When it's clicked on the
2841position of the click is returned to your script as \*(L"button_name.x\*(R"
2842and \*(L"button_name.y\*(R", where \*(L"button_name\*(R" is the name you've assigned
2843to it.
2844.IP "\fBParameters:\fR" 4
2845.IX Item "Parameters:"
2846.PD 0
2847.IP "1." 4
2848.PD
2849The first argument (\-name) is required and specifies the name of this
2850field.
2851.IP "2." 4
2852The second argument (\-src) is also required and specifies the \s-1URL\s0
2853.IP "3. The third option (\-align, optional) is an alignment type, and may be \s-1TOP\s0, \s-1BOTTOM\s0 or \s-1MIDDLE\s0" 4
2854.IX Item "3. The third option (-align, optional) is an alignment type, and may be TOP, BOTTOM or MIDDLE"
2855.PP
2856Fetch the value of the button this way:
2857 \f(CW$x\fR = param('button_name.x');
2858 \f(CW$y\fR = param('button_name.y');
2859.Sh "\s-1CREATING\s0 A \s-1JAVASCRIPT\s0 \s-1ACTION\s0 \s-1BUTTON\s0"
2860.IX Subsection "CREATING A JAVASCRIPT ACTION BUTTON"
2861.Vb 3
2862\& print button(-name=>'button_name',
2863\& -value=>'user visible label',
2864\& -onClick=>"do_something()");
2865.Ve
2866.PP
2867.Vb 1
2868\& -or-
2869.Ve
2870.PP
2871.Vb 1
2872\& print button('button_name',"do_something()");
2873.Ve
2874.PP
2875\&\fIbutton()\fR produces a button that is compatible with Netscape 2.0's
2876JavaScript. When it's pressed the fragment of JavaScript code
2877pointed to by the \fB\-onClick\fR parameter will be executed. On
2878non-Netscape browsers this form element will probably not even
2879display.
2880.SH "HTTP COOKIES"
2881.IX Header "HTTP COOKIES"
2882Netscape browsers versions 1.1 and higher, and all versions of
2883Internet Explorer, support a so-called \*(L"cookie\*(R" designed to help
2884maintain state within a browser session. \s-1CGI\s0.pm has several methods
2885that support cookies.
2886.PP
2887A cookie is a name=value pair much like the named parameters in a \s-1CGI\s0
2888query string. \s-1CGI\s0 scripts create one or more cookies and send
2889them to the browser in the \s-1HTTP\s0 header. The browser maintains a list
2890of cookies that belong to a particular Web server, and returns them
2891to the \s-1CGI\s0 script during subsequent interactions.
2892.PP
2893In addition to the required name=value pair, each cookie has several
2894optional attributes:
2895.IP "1. an expiration time" 4
2896.IX Item "1. an expiration time"
2897This is a time/date string (in a special \s-1GMT\s0 format) that indicates
2898when a cookie expires. The cookie will be saved and returned to your
2899script until this expiration date is reached if the user exits
2900the browser and restarts it. If an expiration date isn't specified, the cookie
2901will remain active until the user quits the browser.
2902.IP "2. a domain" 4
2903.IX Item "2. a domain"
2904This is a partial or complete domain name for which the cookie is
2905valid. The browser will return the cookie to any host that matches
2906the partial domain name. For example, if you specify a domain name
2907of \*(L".capricorn.com\*(R", then the browser will return the cookie to
2908Web servers running on any of the machines \*(L"www.capricorn.com\*(R",
2909\&\*(L"www2.capricorn.com\*(R", \*(L"feckless.capricorn.com\*(R", etc. Domain names
2910must contain at least two periods to prevent attempts to match
2911on top level domains like \*(L".edu\*(R". If no domain is specified, then
2912the browser will only return the cookie to servers on the host the
2913cookie originated from.
2914.IP "3. a path" 4
2915.IX Item "3. a path"
2916If you provide a cookie path attribute, the browser will check it
2917against your script's \s-1URL\s0 before returning the cookie. For example,
2918if you specify the path \*(L"/cgi\-bin\*(R", then the cookie will be returned
2919to each of the scripts \*(L"/cgi\-bin/tally.pl\*(R", \*(L"/cgi\-bin/order.pl\*(R",
2920and \*(L"/cgi\-bin/customer_service/complain.pl\*(R", but not to the script
2921\&\*(L"/cgi\-private/site_admin.pl\*(R". By default, path is set to \*(L"/\*(R", which
2922causes the cookie to be sent to any \s-1CGI\s0 script on your site.
2923.ie n .IP "4. a ""secure"" flag" 4
2924.el .IP "4. a ``secure'' flag" 4
2925.IX Item "4. a secure flag"
2926If the \*(L"secure\*(R" attribute is set, the cookie will only be sent to your
2927script if the \s-1CGI\s0 request is occurring on a secure channel, such as \s-1SSL\s0.
2928.PP
2929The interface to \s-1HTTP\s0 cookies is the \fB\f(BIcookie()\fB\fR method:
2930.PP
2931.Vb 7
2932\& $cookie = cookie(-name=>'sessionID',
2933\& -value=>'xyzzy',
2934\& -expires=>'+1h',
2935\& -path=>'/cgi-bin/database',
2936\& -domain=>'.capricorn.org',
2937\& -secure=>1);
2938\& print header(-cookie=>$cookie);
2939.Ve
2940.PP
2941\&\fB\f(BIcookie()\fB\fR creates a new cookie. Its parameters include:
2942.IP "\fB\-name\fR" 4
2943.IX Item "-name"
2944The name of the cookie (required). This can be any string at all.
2945Although browsers limit their cookie names to non-whitespace
2946alphanumeric characters, \s-1CGI\s0.pm removes this restriction by escaping
2947and unescaping cookies behind the scenes.
2948.IP "\fB\-value\fR" 4
2949.IX Item "-value"
2950The value of the cookie. This can be any scalar value,
2951array reference, or even associative array reference. For example,
2952you can store an entire associative array into a cookie this way:
2953.Sp
2954.Vb 2
2955\& $cookie=cookie(-name=>'family information',
2956\& -value=>\e%childrens_ages);
2957.Ve
2958.IP "\fB\-path\fR" 4
2959.IX Item "-path"
2960The optional partial path for which this cookie will be valid, as described
2961above.
2962.IP "\fB\-domain\fR" 4
2963.IX Item "-domain"
2964The optional partial domain for which this cookie will be valid, as described
2965above.
2966.IP "\fB\-expires\fR" 4
2967.IX Item "-expires"
2968The optional expiration date for this cookie. The format is as described
2969in the section on the \fB\f(BIheader()\fB\fR method:
2970.Sp
2971.Vb 1
2972\& "+1h" one hour from now
2973.Ve
2974.IP "\fB\-secure\fR" 4
2975.IX Item "-secure"
2976If set to true, this cookie will only be used within a secure
2977\&\s-1SSL\s0 session.
2978.PP
2979The cookie created by \fIcookie()\fR must be incorporated into the \s-1HTTP\s0
2980header within the string returned by the \fIheader()\fR method:
2981.PP
2982.Vb 1
2983\& print header(-cookie=>$my_cookie);
2984.Ve
2985.PP
2986To create multiple cookies, give \fIheader()\fR an array reference:
2987.PP
2988.Vb 5
2989\& $cookie1 = cookie(-name=>'riddle_name',
2990\& -value=>"The Sphynx's Question");
2991\& $cookie2 = cookie(-name=>'answers',
2992\& -value=>\e%answers);
2993\& print header(-cookie=>[$cookie1,$cookie2]);
2994.Ve
2995.PP
2996To retrieve a cookie, request it by name by calling \fIcookie()\fR method
2997without the \fB\-value\fR parameter:
2998.PP
2999.Vb 4
3000\& use CGI;
3001\& $query = new CGI;
3002\& $riddle = cookie('riddle_name');
3003\& %answers = cookie('answers');
3004.Ve
3005.PP
3006Cookies created with a single scalar value, such as the \*(L"riddle_name\*(R"
3007cookie, will be returned in that form. Cookies with array and hash
3008values can also be retrieved.
3009.PP
3010The cookie and \s-1CGI\s0 namespaces are separate. If you have a parameter
3011named 'answers' and a cookie named 'answers', the values retrieved by
3012\&\fIparam()\fR and \fIcookie()\fR are independent of each other. However, it's
3013simple to turn a \s-1CGI\s0 parameter into a cookie, and vice\-versa:
3014.PP
3015.Vb 4
3016\& # turn a CGI parameter into a cookie
3017\& $c=cookie(-name=>'answers',-value=>[param('answers')]);
3018\& # vice-versa
3019\& param(-name=>'answers',-value=>[cookie('answers')]);
3020.Ve
3021.PP
3022See the \fBcookie.cgi\fR example script for some ideas on how to use
3023cookies effectively.
3024.SH "WORKING WITH FRAMES"
3025.IX Header "WORKING WITH FRAMES"
3026It's possible for \s-1CGI\s0.pm scripts to write into several browser panels
3027and windows using the \s-1HTML\s0 4 frame mechanism. There are three
3028techniques for defining new frames programmatically:
3029.IP "1. Create a <Frameset> document" 4
3030.IX Item "1. Create a <Frameset> document"
3031After writing out the \s-1HTTP\s0 header, instead of creating a standard
3032\&\s-1HTML\s0 document using the \fIstart_html()\fR call, create a <frameset>
3033document that defines the frames on the page. Specify your script(s)
3034(with appropriate parameters) as the \s-1SRC\s0 for each of the frames.
3035.Sp
3036There is no specific support for creating <frameset> sections
3037in \s-1CGI\s0.pm, but the \s-1HTML\s0 is very simple to write. See the frame
3038documentation in Netscape's home pages for details
3039.Sp
3040.Vb 1
3041\& http://home.netscape.com/assist/net_sites/frames.html
3042.Ve
3043.IP "2. Specify the destination for the document in the \s-1HTTP\s0 header" 4
3044.IX Item "2. Specify the destination for the document in the HTTP header"
3045You may provide a \fB\-target\fR parameter to the \fIheader()\fR method:
3046.Sp
3047.Vb 1
3048\& print header(-target=>'ResultsWindow');
3049.Ve
3050.Sp
3051This will tell the browser to load the output of your script into the
3052frame named \*(L"ResultsWindow\*(R". If a frame of that name doesn't already
3053exist, the browser will pop up a new window and load your script's
3054document into that. There are a number of magic names that you can
3055use for targets. See the frame documents on Netscape's home pages for
3056details.
3057.IP "3. Specify the destination for the document in the <form> tag" 4
3058.IX Item "3. Specify the destination for the document in the <form> tag"
3059You can specify the frame to load in the \s-1FORM\s0 tag itself. With
3060\&\s-1CGI\s0.pm it looks like this:
3061.Sp
3062.Vb 1
3063\& print start_form(-target=>'ResultsWindow');
3064.Ve
3065.Sp
3066When your script is reinvoked by the form, its output will be loaded
3067into the frame named \*(L"ResultsWindow\*(R". If one doesn't already exist
3068a new window will be created.
3069.PP
3070The script \*(L"frameset.cgi\*(R" in the examples directory shows one way to
3071create pages in which the fill-out form and the response live in
3072side-by-side frames.
3073.SH "SUPPORT FOR JAVASCRIPT"
3074.IX Header "SUPPORT FOR JAVASCRIPT"
3075Netscape versions 2.0 and higher incorporate an interpreted language
3076called JavaScript. Internet Explorer, 3.0 and higher, supports a
3077closely-related dialect called JScript. JavaScript isn't the same as
3078Java, and certainly isn't at all the same as Perl, which is a great
3079pity. JavaScript allows you to programatically change the contents of
3080fill-out forms, create new windows, and pop up dialog box from within
3081Netscape itself. From the point of view of \s-1CGI\s0 scripting, JavaScript
3082is quite useful for validating fill-out forms prior to submitting
3083them.
3084.PP
3085You'll need to know JavaScript in order to use it. There are many good
3086sources in bookstores and on the web.
3087.PP
3088The usual way to use JavaScript is to define a set of functions in a
3089<\s-1SCRIPT\s0> block inside the \s-1HTML\s0 header and then to register event
3090handlers in the various elements of the page. Events include such
3091things as the mouse passing over a form element, a button being
3092clicked, the contents of a text field changing, or a form being
3093submitted. When an event occurs that involves an element that has
3094registered an event handler, its associated JavaScript code gets
3095called.
3096.PP
3097The elements that can register event handlers include the <\s-1BODY\s0> of an
3098\&\s-1HTML\s0 document, hypertext links, all the various elements of a fill-out
3099form, and the form itself. There are a large number of events, and
3100each applies only to the elements for which it is relevant. Here is a
3101partial list:
3102.IP "\fBonLoad\fR" 4
3103.IX Item "onLoad"
3104The browser is loading the current document. Valid in:
3105.Sp
3106.Vb 1
3107\& + The HTML <BODY> section only.
3108.Ve
3109.IP "\fBonUnload\fR" 4
3110.IX Item "onUnload"
3111The browser is closing the current page or frame. Valid for:
3112.Sp
3113.Vb 1
3114\& + The HTML <BODY> section only.
3115.Ve
3116.IP "\fBonSubmit\fR" 4
3117.IX Item "onSubmit"
3118The user has pressed the submit button of a form. This event happens
3119just before the form is submitted, and your function can return a
3120value of false in order to abort the submission. Valid for:
3121.Sp
3122.Vb 1
3123\& + Forms only.
3124.Ve
3125.IP "\fBonClick\fR" 4
3126.IX Item "onClick"
3127The mouse has clicked on an item in a fill-out form. Valid for:
3128.Sp
3129.Vb 3
3130\& + Buttons (including submit, reset, and image buttons)
3131\& + Checkboxes
3132\& + Radio buttons
3133.Ve
3134.IP "\fBonChange\fR" 4
3135.IX Item "onChange"
3136The user has changed the contents of a field. Valid for:
3137.Sp
3138.Vb 6
3139\& + Text fields
3140\& + Text areas
3141\& + Password fields
3142\& + File fields
3143\& + Popup Menus
3144\& + Scrolling lists
3145.Ve
3146.IP "\fBonFocus\fR" 4
3147.IX Item "onFocus"
3148The user has selected a field to work with. Valid for:
3149.Sp
3150.Vb 6
3151\& + Text fields
3152\& + Text areas
3153\& + Password fields
3154\& + File fields
3155\& + Popup Menus
3156\& + Scrolling lists
3157.Ve
3158.IP "\fBonBlur\fR" 4
3159.IX Item "onBlur"
3160The user has deselected a field (gone to work somewhere else). Valid
3161for:
3162.Sp
3163.Vb 6
3164\& + Text fields
3165\& + Text areas
3166\& + Password fields
3167\& + File fields
3168\& + Popup Menus
3169\& + Scrolling lists
3170.Ve
3171.IP "\fBonSelect\fR" 4
3172.IX Item "onSelect"
3173The user has changed the part of a text field that is selected. Valid
3174for:
3175.Sp
3176.Vb 4
3177\& + Text fields
3178\& + Text areas
3179\& + Password fields
3180\& + File fields
3181.Ve
3182.IP "\fBonMouseOver\fR" 4
3183.IX Item "onMouseOver"
3184The mouse has moved over an element.
3185.Sp
3186.Vb 6
3187\& + Text fields
3188\& + Text areas
3189\& + Password fields
3190\& + File fields
3191\& + Popup Menus
3192\& + Scrolling lists
3193.Ve
3194.IP "\fBonMouseOut\fR" 4
3195.IX Item "onMouseOut"
3196The mouse has moved off an element.
3197.Sp
3198.Vb 6
3199\& + Text fields
3200\& + Text areas
3201\& + Password fields
3202\& + File fields
3203\& + Popup Menus
3204\& + Scrolling lists
3205.Ve
3206.PP
3207In order to register a JavaScript event handler with an \s-1HTML\s0 element,
3208just use the event name as a parameter when you call the corresponding
3209\&\s-1CGI\s0 method. For example, to have your \fIvalidateAge()\fR JavaScript code
3210executed every time the textfield named \*(L"age\*(R" changes, generate the
3211field like this:
3212.PP
3213.Vb 1
3214\& print textfield(-name=>'age',-onChange=>"validateAge(this)");
3215.Ve
3216.PP
3217This example assumes that you've already declared the \fIvalidateAge()\fR
3218function by incorporating it into a <\s-1SCRIPT\s0> block. The \s-1CGI\s0.pm
3219\&\fIstart_html()\fR method provides a convenient way to create this section.
3220.PP
3221Similarly, you can create a form that checks itself over for
3222consistency and alerts the user if some essential value is missing by
3223creating it this way:
3224 print startform(\-onSubmit=>\*(L"validateMe(this)\*(R");
3225.PP
3226See the javascript.cgi script for a demonstration of how this all
3227works.
3228.SH "LIMITED SUPPORT FOR CASCADING STYLE SHEETS"
3229.IX Header "LIMITED SUPPORT FOR CASCADING STYLE SHEETS"
3230\&\s-1CGI\s0.pm has limited support for \s-1HTML3\s0's cascading style sheets (css).
3231To incorporate a stylesheet into your document, pass the
3232\&\fIstart_html()\fR method a \fB\-style\fR parameter. The value of this
3233parameter may be a scalar, in which case it is treated as the source
3234\&\s-1URL\s0 for the stylesheet, or it may be a hash reference. In the latter
3235case you should provide the hash with one or more of \fB\-src\fR or
3236\&\fB\-code\fR. \fB\-src\fR points to a \s-1URL\s0 where an externally-defined
3237stylesheet can be found. \fB\-code\fR points to a scalar value to be
3238incorporated into a <style> section. Style definitions in \fB\-code\fR
3239override similarly-named ones in \fB\-src\fR, hence the name \*(L"cascading.\*(R"
3240.PP
3241You may also specify the type of the stylesheet by adding the optional
3242\&\fB\-type\fR parameter to the hash pointed to by \fB\-style\fR. If not
3243specified, the style defaults to 'text/css'.
3244.PP
3245To refer to a style within the body of your document, add the
3246\&\fB\-class\fR parameter to any \s-1HTML\s0 element:
3247.PP
3248.Vb 1
3249\& print h1({-class=>'Fancy'},'Welcome to the Party');
3250.Ve
3251.PP
3252Or define styles on the fly with the \fB\-style\fR parameter:
3253.PP
3254.Vb 1
3255\& print h1({-style=>'Color: red;'},'Welcome to Hell');
3256.Ve
3257.PP
3258You may also use the new \fB\f(BIspan()\fB\fR element to apply a style to a
3259section of text:
3260.PP
3261.Vb 4
3262\& print span({-style=>'Color: red;'},
3263\& h1('Welcome to Hell'),
3264\& "Where did that handbasket get to?"
3265\& );
3266.Ve
3267.PP
3268Note that you must import the \*(L":html3\*(R" definitions to have the
3269\&\fB\f(BIspan()\fB\fR method available. Here's a quick and dirty example of using
3270\&\s-1CSS\s0's. See the \s-1CSS\s0 specification at
3271http://www.w3.org/pub/WWW/TR/Wd\-css\-1.html for more information.
3272.PP
3273.Vb 1
3274\& use CGI qw/:standard :html3/;
3275.Ve
3276.PP
3277.Vb 29
3278\& #here's a stylesheet incorporated directly into the page
3279\& $newStyle=<<END;
3280\& <!--
3281\& P.Tip {
3282\& margin-right: 50pt;
3283\& margin-left: 50pt;
3284\& color: red;
3285\& }
3286\& P.Alert {
3287\& font-size: 30pt;
3288\& font-family: sans-serif;
3289\& color: red;
3290\& }
3291\& -->
3292\& END
3293\& print header();
3294\& print start_html( -title=>'CGI with Style',
3295\& -style=>{-src=>'http://www.capricorn.com/style/st1.css',
3296\& -code=>$newStyle}
3297\& );
3298\& print h1('CGI with Style'),
3299\& p({-class=>'Tip'},
3300\& "Better read the cascading style sheet spec before playing with this!"),
3301\& span({-style=>'color: magenta'},
3302\& "Look Mom, no hands!",
3303\& p(),
3304\& "Whooo wee!"
3305\& );
3306\& print end_html;
3307.Ve
3308.PP
3309Pass an array reference to \fB\-code\fR or \fB\-src\fR in order to incorporate
3310multiple stylesheets into your document.
3311.PP
3312Should you wish to incorporate a verbatim stylesheet that includes
3313arbitrary formatting in the header, you may pass a \-verbatim tag to
3314the \-style hash, as follows:
3315.PP
3316print start_html (\-STYLE => {\-verbatim => '@import
3317url(\*(L"/server\-common/css/'.$cssFile.'\*(R");',
3318 \-src => '/server\-common/css/core.css'});
3319</blockquote></pre>
3320.PP
3321This will generate an \s-1HTML\s0 header that contains this:
3322.PP
3323.Vb 4
3324\& <link rel="stylesheet" type="text/css" href="/server-common/css/core.css">
3325\& <style type="text/css">
3326\& @import url("/server-common/css/main.css");
3327\& </style>
3328.Ve
3329.PP
3330Any additional arguments passed in the \-style value will be
3331incorporated into the <link> tag. For example:
3332.PP
3333.Vb 2
3334\& start_html(-style=>{-src=>['/styles/print.css','/styles/layout.css'],
3335\& -media => 'all'});
3336.Ve
3337.PP
3338This will give:
3339.PP
3340.Vb 2
3341\& <link rel="stylesheet" type="text/css" href="/styles/print.css" media="all"/>
3342\& <link rel="stylesheet" type="text/css" href="/styles/layout.css" media="all"/>
3343.Ve
3344.PP
3345<p>
3346.PP
3347To make more complicated <link> tags, use the \fILink()\fR function
3348and pass it to \fIstart_html()\fR in the \-head argument, as in:
3349.PP
3350.Vb 3
3351\& @h = (Link({-rel=>'stylesheet',-type=>'text/css',-src=>'/ss/ss.css',-media=>'all'}),
3352\& Link({-rel=>'stylesheet',-type=>'text/css',-src=>'/ss/fred.css',-media=>'paper'}));
3353\& print start_html({-head=>\e@h})
3354.Ve
3355.SH "DEBUGGING"
3356.IX Header "DEBUGGING"
3357If you are running the script from the command line or in the perl
3358debugger, you can pass the script a list of keywords or
3359parameter=value pairs on the command line or from standard input (you
3360don't have to worry about tricking your script into reading from
3361environment variables). You can pass keywords like this:
3362.PP
3363.Vb 1
3364\& your_script.pl keyword1 keyword2 keyword3
3365.Ve
3366.PP
3367or this:
3368.PP
3369.Vb 1
3370\& your_script.pl keyword1+keyword2+keyword3
3371.Ve
3372.PP
3373or this:
3374.PP
3375.Vb 1
3376\& your_script.pl name1=value1 name2=value2
3377.Ve
3378.PP
3379or this:
3380.PP
3381.Vb 1
3382\& your_script.pl name1=value1&name2=value2
3383.Ve
3384.PP
3385To turn off this feature, use the \-no_debug pragma.
3386.PP
3387To test the \s-1POST\s0 method, you may enable full debugging with the \-debug
3388pragma. This will allow you to feed newline-delimited name=value
3389pairs to the script on standard input.
3390.PP
3391When debugging, you can use quotes and backslashes to escape
3392characters in the familiar shell manner, letting you place
3393spaces and other funny characters in your parameter=value
3394pairs:
3395.PP
3396.Vb 1
3397\& your_script.pl "name1='I am a long value'" "name2=two\e words"
3398.Ve
3399.PP
3400Finally, you can set the path info for the script by prefixing the first
3401name/value parameter with the path followed by a question mark (?):
3402.PP
3403.Vb 1
3404\& your_script.pl /your/path/here?name1=value1&name2=value2
3405.Ve
3406.Sh "\s-1DUMPING\s0 \s-1OUT\s0 \s-1ALL\s0 \s-1THE\s0 \s-1NAME/VALUE\s0 \s-1PAIRS\s0"
3407.IX Subsection "DUMPING OUT ALL THE NAME/VALUE PAIRS"
3408The \fIDump()\fR method produces a string consisting of all the query's
3409name/value pairs formatted nicely as a nested list. This is useful
3410for debugging purposes:
3411.PP
3412.Vb 1
3413\& print Dump
3414.Ve
3415.PP
3416Produces something that looks like:
3417.PP
3418.Vb 11
3419\& <ul>
3420\& <li>name1
3421\& <ul>
3422\& <li>value1
3423\& <li>value2
3424\& </ul>
3425\& <li>name2
3426\& <ul>
3427\& <li>value1
3428\& </ul>
3429\& </ul>
3430.Ve
3431.PP
3432As a shortcut, you can interpolate the entire \s-1CGI\s0 object into a string
3433and it will be replaced with the a nice \s-1HTML\s0 dump shown above:
3434.PP
3435.Vb 2
3436\& $query=new CGI;
3437\& print "<h2>Current Values</h2> $query\en";
3438.Ve
3439.SH "FETCHING ENVIRONMENT VARIABLES"
3440.IX Header "FETCHING ENVIRONMENT VARIABLES"
3441Some of the more useful environment variables can be fetched
3442through this interface. The methods are as follows:
3443.IP "\fB\f(BIAccept()\fB\fR" 4
3444.IX Item "Accept()"
3445Return a list of \s-1MIME\s0 types that the remote browser accepts. If you
3446give this method a single argument corresponding to a \s-1MIME\s0 type, as in
3447Accept('text/html'), it will return a floating point value
3448corresponding to the browser's preference for this type from 0.0
3449(don't want) to 1.0. Glob types (e.g. text/*) in the browser's accept
3450list are handled correctly.
3451.Sp
3452Note that the capitalization changed between version 2.43 and 2.44 in
3453order to avoid conflict with Perl's \fIaccept()\fR function.
3454.IP "\fB\f(BIraw_cookie()\fB\fR" 4
3455.IX Item "raw_cookie()"
3456Returns the \s-1HTTP_COOKIE\s0 variable, an \s-1HTTP\s0 extension implemented by
3457Netscape browsers version 1.1 and higher, and all versions of Internet
3458Explorer. Cookies have a special format, and this method call just
3459returns the raw form (?cookie dough). See \fIcookie()\fR for ways of
3460setting and retrieving cooked cookies.
3461.Sp
3462Called with no parameters, \fIraw_cookie()\fR returns the packed cookie
3463structure. You can separate it into individual cookies by splitting
3464on the character sequence \*(L"; \*(R". Called with the name of a cookie,
3465retrieves the \fBunescaped\fR form of the cookie. You can use the
3466regular \fIcookie()\fR method to get the names, or use the \fIraw_fetch()\fR
3467method from the CGI::Cookie module.
3468.IP "\fB\f(BIuser_agent()\fB\fR" 4
3469.IX Item "user_agent()"
3470Returns the \s-1HTTP_USER_AGENT\s0 variable. If you give
3471this method a single argument, it will attempt to
3472pattern match on it, allowing you to do something
3473like user_agent(netscape);
3474.IP "\fB\f(BIpath_info()\fB\fR" 4
3475.IX Item "path_info()"
3476Returns additional path information from the script \s-1URL\s0.
3477E.G. fetching /cgi\-bin/your_script/additional/stuff will result in
3478\&\fIpath_info()\fR returning \*(L"/additional/stuff\*(R".
3479.Sp
3480\&\s-1NOTE:\s0 The Microsoft Internet Information Server
3481is broken with respect to additional path information. If
3482you use the Perl \s-1DLL\s0 library, the \s-1IIS\s0 server will attempt to
3483execute the additional path information as a Perl script.
3484If you use the ordinary file associations mapping, the
3485path information will be present in the environment,
3486but incorrect. The best thing to do is to avoid using additional
3487path information in \s-1CGI\s0 scripts destined for use with \s-1IIS\s0.
3488.IP "\fB\f(BIpath_translated()\fB\fR" 4
3489.IX Item "path_translated()"
3490As per \fIpath_info()\fR but returns the additional
3491path information translated into a physical path, e.g.
3492\&\*(L"/usr/local/etc/httpd/htdocs/additional/stuff\*(R".
3493.Sp
3494The Microsoft \s-1IIS\s0 is broken with respect to the translated
3495path as well.
3496.IP "\fB\f(BIremote_host()\fB\fR" 4
3497.IX Item "remote_host()"
3498Returns either the remote host name or \s-1IP\s0 address.
3499if the former is unavailable.
3500.IP "\fB\f(BIscript_name()\fB\fR Return the script name as a partial \s-1URL\s0, for self-refering scripts." 4
3501.IX Item "script_name() Return the script name as a partial URL, for self-refering scripts."
3502.PD 0
3503.IP "\fB\f(BIreferer()\fB\fR" 4
3504.IX Item "referer()"
3505.PD
3506Return the \s-1URL\s0 of the page the browser was viewing
3507prior to fetching your script. Not available for all
3508browsers.
3509.IP "\fBauth_type ()\fR" 4
3510.IX Item "auth_type ()"
3511Return the authorization/verification method in use for this
3512script, if any.
3513.IP "\fBserver_name ()\fR" 4
3514.IX Item "server_name ()"
3515Returns the name of the server, usually the machine's host
3516name.
3517.IP "\fBvirtual_host ()\fR" 4
3518.IX Item "virtual_host ()"
3519When using virtual hosts, returns the name of the host that
3520the browser attempted to contact
3521.IP "\fBserver_port ()\fR" 4
3522.IX Item "server_port ()"
3523Return the port that the server is listening on.
3524.IP "\fBvirtual_port ()\fR" 4
3525.IX Item "virtual_port ()"
3526Like \fIserver_port()\fR except that it takes virtual hosts into account.
3527Use this when running with virtual hosts.
3528.IP "\fBserver_software ()\fR" 4
3529.IX Item "server_software ()"
3530Returns the server software and version number.
3531.IP "\fBremote_user ()\fR" 4
3532.IX Item "remote_user ()"
3533Return the authorization/verification name used for user
3534verification, if this script is protected.
3535.IP "\fBuser_name ()\fR" 4
3536.IX Item "user_name ()"
3537Attempt to obtain the remote user's name, using a variety of different
3538techniques. This only works with older browsers such as Mosaic.
3539Newer browsers do not report the user name for privacy reasons!
3540.IP "\fB\f(BIrequest_method()\fB\fR" 4
3541.IX Item "request_method()"
3542Returns the method used to access your script, usually
3543one of '\s-1POST\s0', '\s-1GET\s0' or '\s-1HEAD\s0'.
3544.IP "\fB\f(BIcontent_type()\fB\fR" 4
3545.IX Item "content_type()"
3546Returns the content_type of data submitted in a \s-1POST\s0, generally
3547multipart/form\-data or application/x\-www\-form\-urlencoded
3548.IP "\fB\f(BIhttp()\fB\fR" 4
3549.IX Item "http()"
3550Called with no arguments returns the list of \s-1HTTP\s0 environment
3551variables, including such things as \s-1HTTP_USER_AGENT\s0,
3552\&\s-1HTTP_ACCEPT_LANGUAGE\s0, and \s-1HTTP_ACCEPT_CHARSET\s0, corresponding to the
3553like-named \s-1HTTP\s0 header fields in the request. Called with the name of
3554an \s-1HTTP\s0 header field, returns its value. Capitalization and the use
3555of hyphens versus underscores are not significant.
3556.Sp
3557For example, all three of these examples are equivalent:
3558.Sp
3559.Vb 3
3560\& $requested_language = http('Accept-language');
3561\& $requested_language = http('Accept_language');
3562\& $requested_language = http('HTTP_ACCEPT_LANGUAGE');
3563.Ve
3564.IP "\fB\f(BIhttps()\fB\fR" 4
3565.IX Item "https()"
3566The same as \fI\fIhttp()\fI\fR, but operates on the \s-1HTTPS\s0 environment variables
3567present when the \s-1SSL\s0 protocol is in effect. Can be used to determine
3568whether \s-1SSL\s0 is turned on.
3569.SH "USING NPH SCRIPTS"
3570.IX Header "USING NPH SCRIPTS"
3571\&\s-1NPH\s0, or \*(L"no\-parsed\-header\*(R", scripts bypass the server completely by
3572sending the complete \s-1HTTP\s0 header directly to the browser. This has
3573slight performance benefits, but is of most use for taking advantage
3574of \s-1HTTP\s0 extensions that are not directly supported by your server,
3575such as server push and \s-1PICS\s0 headers.
3576.PP
3577Servers use a variety of conventions for designating \s-1CGI\s0 scripts as
3578\&\s-1NPH\s0. Many Unix servers look at the beginning of the script's name for
3579the prefix \*(L"nph\-\*(R". The Macintosh WebSTAR server and Microsoft's
3580Internet Information Server, in contrast, try to decide whether a
3581program is an \s-1NPH\s0 script by examining the first line of script output.
3582.PP
3583\&\s-1CGI\s0.pm supports \s-1NPH\s0 scripts with a special \s-1NPH\s0 mode. When in this
3584mode, \s-1CGI\s0.pm will output the necessary extra header information when
3585the \fIheader()\fR and \fIredirect()\fR methods are
3586called.
3587.PP
3588The Microsoft Internet Information Server requires \s-1NPH\s0 mode. As of
3589version 2.30, \s-1CGI\s0.pm will automatically detect when the script is
3590running under \s-1IIS\s0 and put itself into this mode. You do not need to
3591do this manually, although it won't hurt anything if you do. However,
3592note that if you have applied Service Pack 6, much of the
3593functionality of \s-1NPH\s0 scripts, including the ability to redirect while
3594setting a cookie, b<do not work at all> on \s-1IIS\s0 without a special patch
3595from Microsoft. See
3596http://support.microsoft.com/support/kb/articles/Q280/3/41.ASP:
3597Non-Parsed Headers Stripped From \s-1CGI\s0 Applications That Have nph\-
3598Prefix in Name.
3599.IP "In the \fBuse\fR statement" 4
3600.IX Item "In the use statement"
3601Simply add the \*(L"\-nph\*(R" pragmato the list of symbols to be imported into
3602your script:
3603.Sp
3604.Vb 1
3605\& use CGI qw(:standard -nph)
3606.Ve
3607.IP "By calling the \fB\f(BInph()\fB\fR method:" 4
3608.IX Item "By calling the nph() method:"
3609Call \fB\f(BInph()\fB\fR with a non-zero parameter at any point after using \s-1CGI\s0.pm in your program.
3610.Sp
3611.Vb 1
3612\& CGI->nph(1)
3613.Ve
3614.IP "By using \fB\-nph\fR parameters" 4
3615.IX Item "By using -nph parameters"
3616in the \fB\f(BIheader()\fB\fR and \fB\f(BIredirect()\fB\fR statements:
3617.Sp
3618.Vb 1
3619\& print header(-nph=>1);
3620.Ve
3621.SH "Server Push"
3622.IX Header "Server Push"
3623\&\s-1CGI\s0.pm provides four simple functions for producing multipart
3624documents of the type needed to implement server push. These
3625functions were graciously provided by Ed Jordan <ed@fidalgo.net>. To
3626import these into your namespace, you must import the \*(L":push\*(R" set.
3627You are also advised to put the script into \s-1NPH\s0 mode and to set $| to
36281 to avoid buffering problems.
3629.PP
3630Here is a simple script that demonstrates server push:
3631.PP
3632.Vb 14
3633\& #!/usr/local/bin/perl
3634\& use CGI qw/:push -nph/;
3635\& $| = 1;
3636\& print multipart_init(-boundary=>'----here we go!');
3637\& foreach (0 .. 4) {
3638\& print multipart_start(-type=>'text/plain'),
3639\& "The current time is ",scalar(localtime),"\en";
3640\& if ($_ < 4) {
3641\& print multipart_end;
3642\& } else {
3643\& print multipart_final;
3644\& }
3645\& sleep 1;
3646\& }
3647.Ve
3648.PP
3649This script initializes server push by calling \fB\f(BImultipart_init()\fB\fR.
3650It then enters a loop in which it begins a new multipart section by
3651calling \fB\f(BImultipart_start()\fB\fR, prints the current local time,
3652and ends a multipart section with \fB\f(BImultipart_end()\fB\fR. It then sleeps
3653a second, and begins again. On the final iteration, it ends the
3654multipart section with \fB\f(BImultipart_final()\fB\fR rather than with
3655\&\fB\f(BImultipart_end()\fB\fR.
3656.IP "\fImultipart_init()\fR" 4
3657.IX Item "multipart_init()"
3658.Vb 1
3659\& multipart_init(-boundary=>$boundary);
3660.Ve
3661.Sp
3662Initialize the multipart system. The \-boundary argument specifies
3663what \s-1MIME\s0 boundary string to use to separate parts of the document.
3664If not provided, \s-1CGI\s0.pm chooses a reasonable boundary for you.
3665.IP "\fImultipart_start()\fR" 4
3666.IX Item "multipart_start()"
3667.Vb 1
3668\& multipart_start(-type=>$type)
3669.Ve
3670.Sp
3671Start a new part of the multipart document using the specified \s-1MIME\s0
3672type. If not specified, text/html is assumed.
3673.IP "\fImultipart_end()\fR" 4
3674.IX Item "multipart_end()"
3675.Vb 1
3676\& multipart_end()
3677.Ve
3678.Sp
3679End a part. You must remember to call \fImultipart_end()\fR once for each
3680\&\fImultipart_start()\fR, except at the end of the last part of the multipart
3681document when \fImultipart_final()\fR should be called instead of \fImultipart_end()\fR.
3682.IP "\fImultipart_final()\fR" 4
3683.IX Item "multipart_final()"
3684.Vb 1
3685\& multipart_final()
3686.Ve
3687.Sp
3688End all parts. You should call \fImultipart_final()\fR rather than
3689\&\fImultipart_end()\fR at the end of the last part of the multipart document.
3690.PP
3691Users interested in server push applications should also have a look
3692at the CGI::Push module.
3693.PP
3694Only Netscape Navigator supports server push. Internet Explorer
3695browsers do not.
3696.SH "Avoiding Denial of Service Attacks"
3697.IX Header "Avoiding Denial of Service Attacks"
3698A potential problem with \s-1CGI\s0.pm is that, by default, it attempts to
3699process form POSTings no matter how large they are. A wily hacker
3700could attack your site by sending a \s-1CGI\s0 script a huge \s-1POST\s0 of many
3701megabytes. \s-1CGI\s0.pm will attempt to read the entire \s-1POST\s0 into a
3702variable, growing hugely in size until it runs out of memory. While
3703the script attempts to allocate the memory the system may slow down
3704dramatically. This is a form of denial of service attack.
3705.PP
3706Another possible attack is for the remote user to force \s-1CGI\s0.pm to
3707accept a huge file upload. \s-1CGI\s0.pm will accept the upload and store it
3708in a temporary directory even if your script doesn't expect to receive
3709an uploaded file. \s-1CGI\s0.pm will delete the file automatically when it
3710terminates, but in the meantime the remote user may have filled up the
3711server's disk space, causing problems for other programs.
3712.PP
3713The best way to avoid denial of service attacks is to limit the amount
3714of memory, \s-1CPU\s0 time and disk space that \s-1CGI\s0 scripts can use. Some Web
3715servers come with built-in facilities to accomplish this. In other
3716cases, you can use the shell \fIlimit\fR or \fIulimit\fR
3717commands to put ceilings on \s-1CGI\s0 resource usage.
3718.PP
3719\&\s-1CGI\s0.pm also has some simple built-in protections against denial of
3720service attacks, but you must activate them before you can use them.
3721These take the form of two global variables in the \s-1CGI\s0 name space:
3722.IP "\fB$CGI::POST_MAX\fR" 4
3723.IX Item "$CGI::POST_MAX"
3724If set to a non-negative integer, this variable puts a ceiling
3725on the size of POSTings, in bytes. If \s-1CGI\s0.pm detects a \s-1POST\s0
3726that is greater than the ceiling, it will immediately exit with an error
3727message. This value will affect both ordinary POSTs and
3728multipart POSTs, meaning that it limits the maximum size of file
3729uploads as well. You should set this to a reasonably high
3730value, such as 1 megabyte.
3731.IP "\fB$CGI::DISABLE_UPLOADS\fR" 4
3732.IX Item "$CGI::DISABLE_UPLOADS"
3733If set to a non-zero value, this will disable file uploads
3734completely. Other fill-out form values will work as usual.
3735.PP
3736You can use these variables in either of two ways.
3737.IP "\fB1. On a script-by-script basis\fR" 4
3738.IX Item "1. On a script-by-script basis"
3739Set the variable at the top of the script, right after the \*(L"use\*(R" statement:
3740.Sp
3741.Vb 4
3742\& use CGI qw/:standard/;
3743\& use CGI::Carp 'fatalsToBrowser';
3744\& $CGI::POST_MAX=1024 * 100; # max 100K posts
3745\& $CGI::DISABLE_UPLOADS = 1; # no uploads
3746.Ve
3747.IP "\fB2. Globally for all scripts\fR" 4
3748.IX Item "2. Globally for all scripts"
3749Open up \s-1CGI\s0.pm, find the definitions for \f(CW$POST_MAX\fR and
3750\&\f(CW$DISABLE_UPLOADS\fR, and set them to the desired values. You'll
3751find them towards the top of the file in a subroutine named
3752\&\fIinitialize_globals()\fR.
3753.PP
3754An attempt to send a \s-1POST\s0 larger than \f(CW$POST_MAX\fR bytes will cause
3755\&\fI\fIparam()\fI\fR to return an empty \s-1CGI\s0 parameter list. You can test for
3756this event by checking \fI\fIcgi_error()\fI\fR, either after you create the \s-1CGI\s0
3757object or, if you are using the function-oriented interface, call
3758<\fIparam()\fR> for the first time. If the \s-1POST\s0 was intercepted, then
3759\&\fIcgi_error()\fR will return the message \*(L"413 \s-1POST\s0 too large\*(R".
3760.PP
3761This error message is actually defined by the \s-1HTTP\s0 protocol, and is
3762designed to be returned to the browser as the \s-1CGI\s0 script's status
3763 code. For example:
3764.PP
3765.Vb 5
3766\& $uploaded_file = param('upload');
3767\& if (!$uploaded_file && cgi_error()) {
3768\& print header(-status=>cgi_error());
3769\& exit 0;
3770\& }
3771.Ve
3772.PP
3773However it isn't clear that any browser currently knows what to do
3774with this status code. It might be better just to create an
3775\&\s-1HTML\s0 page that warns the user of the problem.
3776.SH "COMPATIBILITY WITH CGI\-LIB.PL"
3777.IX Header "COMPATIBILITY WITH CGI-LIB.PL"
3778To make it easier to port existing programs that use cgi\-lib.pl the
3779compatibility routine \*(L"ReadParse\*(R" is provided. Porting is simple:
3780.PP
3781\&\s-1OLD\s0 \s-1VERSION\s0
3782 require \*(L"cgi\-lib.pl\*(R";
3783 &ReadParse;
3784 print \*(L"The value of the antique is \f(CW$in\fR{antique}.\en\*(R";
3785.PP
3786\&\s-1NEW\s0 \s-1VERSION\s0
3787 use \s-1CGI\s0;
3788 \fICGI::ReadParse()\fR;
3789 print \*(L"The value of the antique is \f(CW$in\fR{antique}.\en\*(R";
3790.PP
3791\&\s-1CGI\s0.pm's \fIReadParse()\fR routine creates a tied variable named \f(CW%in\fR,
3792which can be accessed to obtain the query variables. Like
3793ReadParse, you can also provide your own variable. Infrequently
3794used features of ReadParse, such as the creation of \f(CW@in\fR and \f(CW$in\fR
3795variables, are not supported.
3796.PP
3797Once you use ReadParse, you can retrieve the query object itself
3798this way:
3799.PP
3800.Vb 3
3801\& $q = $in{CGI};
3802\& print textfield(-name=>'wow',
3803\& -value=>'does this really work?');
3804.Ve
3805.PP
3806This allows you to start using the more interesting features
3807of \s-1CGI\s0.pm without rewriting your old scripts from scratch.
3808.SH "AUTHOR INFORMATION"
3809.IX Header "AUTHOR INFORMATION"
3810Copyright 1995\-1998, Lincoln D. Stein. All rights reserved.
3811.PP
3812This library is free software; you can redistribute it and/or modify
3813it under the same terms as Perl itself.
3814.PP
3815Address bug reports and comments to: lstein@cshl.org. When sending
3816bug reports, please provide the version of \s-1CGI\s0.pm, the version of
3817Perl, the name and version of your Web server, and the name and
3818version of the operating system you are using. If the problem is even
3819remotely browser dependent, please provide information about the
3820affected browers as well.
3821.SH "CREDITS"
3822.IX Header "CREDITS"
3823Thanks very much to:
3824.IP "Matt Heffron (heffron@falstaff.css.beckman.com)" 4
3825.IX Item "Matt Heffron (heffron@falstaff.css.beckman.com)"
3826.PD 0
3827.IP "James Taylor (james.taylor@srs.gov)" 4
3828.IX Item "James Taylor (james.taylor@srs.gov)"
3829.IP "Scott Anguish <sanguish@digifix.com>" 4
3830.IX Item "Scott Anguish <sanguish@digifix.com>"
3831.IP "Mike Jewell (mlj3u@virginia.edu)" 4
3832.IX Item "Mike Jewell (mlj3u@virginia.edu)"
3833.IP "Timothy Shimmin (tes@kbs.citri.edu.au)" 4
3834.IX Item "Timothy Shimmin (tes@kbs.citri.edu.au)"
3835.IP "Joergen Haegg (jh@axis.se)" 4
3836.IX Item "Joergen Haegg (jh@axis.se)"
3837.IP "Laurent Delfosse (delfosse@delfosse.com)" 4
3838.IX Item "Laurent Delfosse (delfosse@delfosse.com)"
3839.IP "Richard Resnick (applepi1@aol.com)" 4
3840.IX Item "Richard Resnick (applepi1@aol.com)"
3841.IP "Craig Bishop (csb@barwonwater.vic.gov.au)" 4
3842.IX Item "Craig Bishop (csb@barwonwater.vic.gov.au)"
3843.IP "Tony Curtis (tc@vcpc.univie.ac.at)" 4
3844.IX Item "Tony Curtis (tc@vcpc.univie.ac.at)"
3845.IP "Tim Bunce (Tim.Bunce@ig.co.uk)" 4
3846.IX Item "Tim Bunce (Tim.Bunce@ig.co.uk)"
3847.IP "Tom Christiansen (tchrist@convex.com)" 4
3848.IX Item "Tom Christiansen (tchrist@convex.com)"
3849.IP "Andreas Koenig (k@franz.ww.TU\-Berlin.DE)" 4
3850.IX Item "Andreas Koenig (k@franz.ww.TU-Berlin.DE)"
3851.IP "Tim MacKenzie (Tim.MacKenzie@fulcrum.com.au)" 4
3852.IX Item "Tim MacKenzie (Tim.MacKenzie@fulcrum.com.au)"
3853.IP "Kevin B. Hendricks (kbhend@dogwood.tyler.wm.edu)" 4
3854.IX Item "Kevin B. Hendricks (kbhend@dogwood.tyler.wm.edu)"
3855.IP "Stephen Dahmen (joyfire@inxpress.net)" 4
3856.IX Item "Stephen Dahmen (joyfire@inxpress.net)"
3857.IP "Ed Jordan (ed@fidalgo.net)" 4
3858.IX Item "Ed Jordan (ed@fidalgo.net)"
3859.IP "David Alan Pisoni (david@cnation.com)" 4
3860.IX Item "David Alan Pisoni (david@cnation.com)"
3861.IP "Doug MacEachern (dougm@opengroup.org)" 4
3862.IX Item "Doug MacEachern (dougm@opengroup.org)"
3863.IP "Robin Houston (robin@oneworld.org)" 4
3864.IX Item "Robin Houston (robin@oneworld.org)"
3865.IP "...and many many more..." 4
3866.IX Item "...and many many more..."
3867.PD
3868for suggestions and bug fixes.
3869.SH "A COMPLETE EXAMPLE OF A SIMPLE FORM-BASED SCRIPT"
3870.IX Header "A COMPLETE EXAMPLE OF A SIMPLE FORM-BASED SCRIPT"
3871.Vb 1
3872\& #!/usr/local/bin/perl
3873.Ve
3874.PP
3875.Vb 1
3876\& use CGI ':standard';
3877.Ve
3878.PP
3879.Vb 7
3880\& print header;
3881\& print start_html("Example CGI.pm Form");
3882\& print "<h1> Example CGI.pm Form</h1>\en";
3883\& print_prompt();
3884\& do_work();
3885\& print_tail();
3886\& print end_html;
3887.Ve
3888.PP
3889.Vb 5
3890\& sub print_prompt {
3891\& print start_form;
3892\& print "<em>What's your name?</em><br>";
3893\& print textfield('name');
3894\& print checkbox('Not my real name');
3895.Ve
3896.PP
3897.Vb 6
3898\& print "<p><em>Where can you find English Sparrows?</em><br>";
3899\& print checkbox_group(
3900\& -name=>'Sparrow locations',
3901\& -values=>[England,France,Spain,Asia,Hoboken],
3902\& -linebreak=>'yes',
3903\& -defaults=>[England,Asia]);
3904.Ve
3905.PP
3906.Vb 5
3907\& print "<p><em>How far can they fly?</em><br>",
3908\& radio_group(
3909\& -name=>'how far',
3910\& -values=>['10 ft','1 mile','10 miles','real far'],
3911\& -default=>'1 mile');
3912.Ve
3913.PP
3914.Vb 4
3915\& print "<p><em>What's your favorite color?</em> ";
3916\& print popup_menu(-name=>'Color',
3917\& -values=>['black','brown','red','yellow'],
3918\& -default=>'red');
3919.Ve
3920.PP
3921.Vb 1
3922\& print hidden('Reference','Monty Python and the Holy Grail');
3923.Ve
3924.PP
3925.Vb 7
3926\& print "<p><em>What have you got there?</em><br>";
3927\& print scrolling_list(
3928\& -name=>'possessions',
3929\& -values=>['A Coconut','A Grail','An Icon',
3930\& 'A Sword','A Ticket'],
3931\& -size=>5,
3932\& -multiple=>'true');
3933.Ve
3934.PP
3935.Vb 4
3936\& print "<p><em>Any parting comments?</em><br>";
3937\& print textarea(-name=>'Comments',
3938\& -rows=>10,
3939\& -columns=>50);
3940.Ve
3941.PP
3942.Vb 6
3943\& print "<p>",reset;
3944\& print submit('Action','Shout');
3945\& print submit('Action','Scream');
3946\& print endform;
3947\& print "<hr>\en";
3948\& }
3949.Ve
3950.PP
3951.Vb 2
3952\& sub do_work {
3953\& my(@values,$key);
3954.Ve
3955.PP
3956.Vb 1
3957\& print "<h2>Here are the current settings in this form</h2>";
3958.Ve
3959.PP
3960.Vb 6
3961\& foreach $key (param) {
3962\& print "<strong>$key</strong> -> ";
3963\& @values = param($key);
3964\& print join(", ",@values),"<br>\en";
3965\& }
3966\& }
3967.Ve
3968.PP
3969.Vb 7
3970\& sub print_tail {
3971\& print <<END;
3972\& <hr>
3973\& <address>Lincoln D. Stein</address><br>
3974\& <a href="/">Home Page</a>
3975\& END
3976\& }
3977.Ve
3978.SH "BUGS"
3979.IX Header "BUGS"
3980Please report them.
3981.SH "SEE ALSO"
3982.IX Header "SEE ALSO"
3983CGI::Carp, CGI::Fast, CGI::Pretty