10.2 Importing Method GroupsThe 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: By importing the#!/usr/bin/perl use CGI param,header,p; $bday = param("birthday"); print header(); print p("Your birthday is $bday.");
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: The method groups supported by CGI.pm are:use CGI qw/:form :cgi/;
You can also define new methods for HTML tag generation by simply listing them on the import line and then letting CGI.pm make some educated guesses. For example: This will cause the following tag to be generated:use CGI shortcuts,smell; print smell {type=>'garlic', intensity=>'strong'}, "Scratch here!"; <SMELL TYPE="garlic" INTENSITY="strong">Scratch here!</SMELL> |
|