CGI

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN

        use CGI;
        use Encode qw/ decode /;

        my $cgi   = CGI->new;
        my $param = $cgi->param('foo');
        $param    = decode( 'UTF-8',$param );

- -putdata\_upload / -postdata\_upload / -patchdata\_upload

    Makes `$cgi->param('PUTDATA');`, `$cgi->param('PATCHDATA');`,
    and `$cgi->param('POSTDATA');` act like file uploads named PUTDATA,
    PATCHDATA, and POSTDATA. See ["Handling non-urlencoded arguments"](#handling-non-urlencoded-arguments) and
    ["Processing a file upload field"](#processing-a-file-upload-field) PUTDATA/POSTDATA/PATCHDATA are also available
    via [upload\_hook](#progress-bars-for-file-uploads-and-avoiding-temp-files).

- -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.

- -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.

- -oldstyle\_urls

    Separate the name=value pairs in CGI parameter query strings with ampersands
    rather than semicolons. This is no longer the default.

- -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 /;

- -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.

# 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.

## 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 **-type**, **-status**,
**-expires**, and **-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 **-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.421 second using v1.01-cache-2.11-cpan-b16cb0d3907 )