One of my goals since coming to the Monastery is to write more efficient code, albeit, not necessarily more readible. My primary use of Perl is forms processing and dynamic web pages. Traditionally I have been using something like the following to read in my variables from an HTML form:
use strict;
use CGI;
my $q = new CGI;
my $name = $q -> param('name');
my $address = $q -> param('address');
my $city = $q -> param('city');
my $state = $q -> param('state');
my $zip = $q -> param('zip');
my $phone = $q -> param('phone');
and so on....
However, in reading my newly acquired
Perl Cookbook about referencing and dereferencing, I found that I could do the same with:
use strict;
no strict 'refs';
use CGI qw(:standard);
my @params = param(); #get field names with empty params()
foreach my $field ( @params ) {
my $variable = $field;
$$variable = param( $field );
}
This apparently saves me the trouble of writing numberous lines of
$variable = $q -> param('fieldname'); (some forms will have 20-30 fields) and also make edits easier.
So, fellow monks, any pitfalls or better ways of doing this?
Note: I switched from the OO syntax to the standard, even after reading
ovid's great
CGI tutorial and
CGI Programming in Perl--it seems a toss-up, but I'm open.
Thanks!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.