That's still a lot of typing. It turns out that CGI.pm includes a whole slew of convenience functions for simplifying this. Each of these routines returns a string for you to output. For example,
returns a string containing the
line with a following blank line,
as an HTML title,
as a first-level HTML heading, and
as a new HTML paragraph.
We could list all these functions in the import list given with
, but that will eventually grow too unwieldy. However, CGI.pm, like many modules, provides you with
- labels that stand for groups of functions to import. You simply place the desired tags (each of which begins with a colon) at the beginning of your import list. The tags available with CGI.pm include these:
Import all argument-handling methods, such as
Import all fill-out form generating methods, such as
Import all methods that generate HTML 2.0 standard elements.
Import all methods that generate HTML 3.0 elements (such as
Import all methods that generate Netscape-specific HTML extensions.
Import all HTML-generating shortcuts (that is, "html2" + "html3" + "netscape").
Import "standard" features: "html2", "form", and "cgi".
Import all the available methods. For the full list, see the CGI.pm module, where the variable
We'll just use
. (For more about importing functions and variables from modules, see
the Exporter module
, or the
Here's our program using all the shortcuts CGI.pm provides:
# cgi-bin/ice_cream: program to answer ice cream
# favorite flavor form (version 2)
use CGI qw(:standard);
print header(), start_html("Hello World"), h1("Greetings, Terrans!");
my $favorite = param("flavor");
print p("Your favorite flavor is $favorite.");
See how much easier that is? You don't have to worry about form decoding, headers, or HTML if you don't want to.