Importing Method Groups

The syntax for calling CGI methods can be unwieldy. However, you can import individual methods and then call the methods without explicit object calls. The “birthday” example shown earlier could be written even more simply as follows:

#!/usr/bin/perl

use CGI param,header,p;

$bday = param("birthday");
print header(  );
print p("Your birthday is $bday.");

By importing the param, header, and p methods into your namespace, you no longer have to use the new constructor (since it is called automatically now), and you don’t need to specify a CGI object with every method call.

CGI.pm also lets you import groups of methods, which can make your programs much simpler and more elegant. For example, to import all form-creation methods and all CGI-handling methods:

use CGI qw/:form :cgi/;

The method groups supported by CGI.pm are:

:cgi

All CGI-handling methods

:cgi-lib

All methods supplied for backwards compatibility with cgi-lib

:form

All form-generation methods

:html

All HTML methods

:html2

All HTML 2.0 methods

:html3

All HTML 3.0 methods

:html4

All HTML 4.0 methods

:netscape

All methods generating Netscape extensions

:ssl

All SSL methods

:standard

All HTML, form-generation, and CGI methods

:all

All available methods

You can also define new methods for HTML tag generation by simply listing them on the import line and letting CGI.pm make some educated guesses. For example:

use CGI shortcuts,smell; print smell {type=>'garlic', intensity=>'strong'}, "Scratch ...

Get Webmaster in a Nutshell, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.