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, header() returns a string containing the Content-type line with a following blank line, start_html(string) returns string as an HTML title, h1(string) returns string as a first-level HTML heading, and p(string) returns string as a new HTML paragraph.
We could list all these functions in the import list given with use, but that will eventually grow too unwieldy. However, CGI.pm, like many modules, provides you with import tags —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 param().
Import all fill-out form generating methods, such as textfield().
Import all methods that generate HTML 2.0 standard elements.
:html3
Import all methods that generate HTML 3.0 elements (such as <table>, <super>, and <sub>).
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 %TAGS is defined.
We'll just use :standard. (For more about importing functions and variables from modules, see the Exporter module in Chapter 7 of Programming Perl, or the Exporter (3) manpage.)
Here's our program using all the shortcuts CGI.pm provides:
#!/usr/bin/perl -w # 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."); print end_html();
See how much easier that is? You don't have to worry about form decoding, headers, or HTML if you don't want to.