Skip to Main Content
Perl in a Nutshell, 2nd Edition
book

Perl in a Nutshell, 2nd Edition

by Nathan Patwardhan, Ellen Siever, Stephen Spainhour
June 2002
Beginner content levelBeginner
759 pages
80h 42m
English
O'Reilly Media, Inc.
Content preview from Perl in a Nutshell, 2nd Edition

Importing Method Groups

The syntax for calling CGI methods can be unwieldy. However, if you choose not to use the object form of CGI.pm, you can import individual methods instead. The “birthday” example shown earlier could be written even more simply as:

#!/usr/local/bin/perl -w

use CGI qw(param header p);

my $bday = param("birthday");

print header('text/plain');
print "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 backward 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

:netscape

All methods generating Netscape extensions

:ssl

All SSL methods

:standard

All HTML 2.0, 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 here!";

This will cause the following tag to ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Perl by Example, Fourth Edition

Perl by Example, Fourth Edition

Ellie Quigley
Perl Cookbook, 2nd Edition

Perl Cookbook, 2nd Edition

Tom Christiansen, Nathan Torkington
Perl in a Nutshell

Perl in a Nutshell

Nathan Patwardhan, Ellen Siever, Stephen Spainhour
Learning Perl, 7th Edition

Learning Perl, 7th Edition

Randal L. Schwartz, brian d foy, Tom Phoenix

Publisher Resources

ISBN: 0596002416Errata Page