legLess has asked for the wisdom of the Perl Monks concerning the following question:
I'm writing a multi-form CGI script. Thanks to some input I've received here it's going quite well.
I'm currently using this code, where I slurp all CGI params into a hash, then pass that hash to each sub. Some subs add key/value pairs (parameters, basically), so I return the whole hash or some part of it. Some other parts of the code add parameters, too.
And I'm thinking about something like this, where I dispense with the hash entirely and use just the $q CGI query object.my $q = CGI->new(); my %prm = $q->Vars(); ... if ($prm{'action'} eq 'Login') { if ($prm{'mid'}) { $prm{'warning'} = 'Already logged in, bonehead.'; show_member($q,%prm); } else { do_login($q,%prm); } }
I started this a couple weeks ago, and I know an order of magnitude more Perl now, so it's good to re-examine my previous assumptions. I'd guess that the first example would be a little quicker, without the OO overhead. The second example looks a little cleaner, and is probably easier to maintain (this alone argues in its favor).my $q = CGI->new(); ... if ($q->param('action') eq 'Login') { if ($q->param('mid')) { $q->param('warning') = 'Already logged in, bonehead.'; show_member($q); } else { do_login($q); } }
(I did a quick search/replace on the file and it nailed 115 instances (in ~700 lines) of $prm{'x'}, replacing them with $q->param('x').)
It seems to me that the second example is superior in many ways, but I'd enjoy comments from any passing Monks.
Thanks.
--
man with no legs, inc.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: CGI OO 'param' vs. hash
by tadman (Prior) on Jul 09, 2001 at 22:29 UTC | |
by Ovid (Cardinal) on Jul 09, 2001 at 23:16 UTC | |
by legLess (Hermit) on Jul 10, 2001 at 00:06 UTC | |
by Ovid (Cardinal) on Jul 10, 2001 at 00:20 UTC | |
by legLess (Hermit) on Jul 10, 2001 at 00:34 UTC | |
| |
by legLess (Hermit) on Jul 10, 2001 at 00:11 UTC | |
|
(Ovid) Re: CGI OO 'param' vs. hash
by Ovid (Cardinal) on Jul 09, 2001 at 23:27 UTC | |
by legLess (Hermit) on Jul 10, 2001 at 00:02 UTC | |
|
Re: CGI OO 'param' vs. hash
by TGI (Parson) on Jul 10, 2001 at 05:11 UTC |