Monks ~

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.

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); } }
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(); ... 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 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).

(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.


In reply to CGI OO 'param' vs. hash by legLess

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.