Fri 10 Nov 2006
CGI.pm is a module included in the standard perl distribution. It is the most commonly used module for writing perl scripts that interact with the web.
I run across these mistakes a lot when reviewing beginner perl code. The first mistake has to do with the dual nature of perl modules. A perl module can be accessed either as a function-oriented module or as a object-oriented module or by both methods.
CGI.pm allows both. Under the hood, that makes for some messy looking code. In practice, we find a lot of bloated code. Normally, we would use either the function oriented or the OO version, not both.Here are a few of the most common errors I see.
-
#!/usr/bin/perl
-
-
use strict;
-
use warnings;
-
-
-
my $q = CGI->new();
use CGI qw( :standard );
loads the standard subroutines which the author of CGI.pm deems necessary for interacting with and creating xhtml pages and forms. This means we are going to use the function-oriented interface to CGI.pm. my $q = CGI->new();
establishes we are going to use the object-oriented interface to CGI.pm.
There's nothing wrong with that, but I find most people making this mistake go on to ignore one of the interfaces. I think these beginners just don't understand what they are doing. How should this look? It depends on the interface we want to use.
Object-Oriented
-
use CGI;
-
-
my $q = CGI->new();
Function-Oriented
Actually, the last example is also a common mistake. When using the function-oriented interface to CGI.pm we are importing subroutines from a module into our main name space. Ideally we would like to import only those subroutines which we plan to use. Any other subroutines would "pollute" our namespace.
What's wrong with :standard
? Chances are you won't use more than ten or twelve functions. :standard
represents about a hundred functions. Some of those are very obscure functions. Some of them are not used in xhtml documents.
Here's a list from my version of CGI.pm. :standard
is actually four other exported tags.
:html2: h1 h2 h3 h4 h5 h6 p br hr ol ul li dl dt dd menu code var strong em tt u i b blockquote pre img a address cite samp dfn html head base body Link nextid title meta kbd start_html end_html input Select option comment charset escapeHTML :html3: div table caption th td TR Tr sup Sub strike applet Param embed basefont style span layer ilayer font frameset frame script small big :form: textfield textarea filefield password_field hidden checkbox checkbox_group submit reset defaults radio_group popup_menu button autoEscape scrolling_list image_button start_form end_form startform endform start_multipart_form end_multipart_form isindex tmpFileName uploadInfo URL_ENCODED MULTIPART :cgi: param upload path_info path_translated url self_url script_name cookie Dump raw_cookie request_method query_string Accept user_agent remote_host content_type remote_addr referer server_name server_software server_port server_protocol virtual_host remote_ident auth_type http save_parameters restore_parameters param_fetch remote_user user_name header redirect import_names put Delete Delete_all url_param cgi_error
I doubt anyone needs all of those. It is better to just import the ones you are using. I find this is all that is needed in most cases.
Leave a Reply
You must be logged in to post a comment.