CGI
view release on metacpan or search on metacpan
lib/CGI.pod view on Meta::CPAN
my $cgi = CGI->new;
my $param = $cgi->param('foo');
$param = decode( 'UTF-8',$param );
=item -putdata_upload / -postdata_upload / -patchdata_upload
Makes C<<< $cgi->param('PUTDATA'); >>>, C<<< $cgi->param('PATCHDATA'); >>>,
and C<<< $cgi->param('POSTDATA'); >>> act like file uploads named PUTDATA,
PATCHDATA, and POSTDATA. See L</Handling non-urlencoded arguments> and
L</Processing a file upload field> PUTDATA/POSTDATA/PATCHDATA are also available
via L<upload_hook|/Progress bars for file uploads and avoiding temp files>.
=item -nph
This makes CGI.pm produce a header appropriate for an NPH (no parsed header)
script. You may need to do other things as well to tell the server that the
script is NPH. See the discussion of NPH scripts below.
=item -newstyle_urls
Separate the name=value pairs in CGI parameter query strings with semicolons
rather than ampersands. For example:
?name=fred;age=24;favorite_color=3
Semicolon-delimited query strings are always accepted, and will be emitted by
self_url() and query_string(). newstyle_urls became the default in version
2.64.
=item -oldstyle_urls
Separate the name=value pairs in CGI parameter query strings with ampersands
rather than semicolons. This is no longer the default.
=item -no_debug
This turns off the command-line processing features. If you want to run a CGI.pm
script from the command line, and you don't want it to read CGI parameters from
the command line or STDIN, then use this pragma:
use CGI qw/ -no_debug :standard /;
=item -debug
This turns on full debugging. In addition to reading CGI arguments from the
command-line processing, CGI.pm will pause and try to read arguments from STDIN,
producing the message "(offline mode: enter name=value pairs on standard input)"
features.
See the section on debugging for more details.
=back
=head1 GENERATING DYNAMIC DOCUMENTS
Most of CGI.pm's functions deal with creating documents on the fly. Generally
you will produce the HTTP header first, followed by the document itself. CGI.pm
provides functions for generating HTTP headers of various types.
Each of these functions produces a fragment of HTTP which you can print out
directly so that it is processed by the browser, appended to a string, or saved
to a file for later use.
=head2 Creating a standard http header
Normally the first thing you will do in any CGI script is print out an HTTP
header. This tells the browser what type of document to expect, and gives other
optional information, such as the language, expiration date, and whether to
cache the document. The header can also be manipulated for special purposes,
such as server push and pay per view pages.
use strict;
use warnings;
use CGI;
my $cgi = CGI->new;
print $cgi->header;
-or-
print $cgi->header('image/gif');
-or-
print $cgi->header('text/html','204 No response');
-or-
print $cgi->header(
-type => 'image/gif',
-nph => 1,
-status => '402 Payment required',
-expires => '+3d',
-cookie => $cookie,
-charset => 'utf-8',
-attachment => 'foo.gif',
-Cost => '$2.00'
);
header() returns the Content-type: header. You can provide your own MIME type
if you choose, otherwise it defaults to text/html. An optional second parameter
specifies the status code and a human-readable message. For example, you can
specify 204, "No response" to create a script that tells the browser to do
nothing at all. Note that RFC 2616 expects the human-readable phase to be there
as well as the numeric status code.
The last example shows the named argument style for passing arguments to the CGI
methods using named parameters. Recognized parameters are B<-type>, B<-status>,
B<-expires>, and B<-cookie>. Any other named parameters will be stripped of
their initial hyphens and turned into header fields, allowing you to specify
any HTTP header you desire. Internal underscores will be turned into hyphens:
print $cgi->header( -Content_length => 3002 );
Most browsers will not cache the output from CGI scripts. Every time the browser
reloads the page, the script is invoked anew. You can change this behavior with
the B<-expires> parameter. When you specify an absolute or relative expiration
interval with this parameter, some browsers and proxy servers will cache the
( run in 1.370 second using v1.01-cache-2.11-cpan-364913b4093 )