At the risk of presenting heresy, (and after reading all 3 of the "use soft references and die!" articles) I'd like to say that I like using soft references when I'm writing CGIs.

Here's the deal: When you're writing a CGI, you know (well, most of the time) all the form element names. Lots of times there's lots of elements, and for security's sake you only want to mess around with the elements that you care about. (i.e., if some bozo submits a form element named "foo" you want to ignore it and not import it accidentally into your namespace.) So, here's a favorite idiom I like to use:

@vars = qw(name rank serial_num shoe_size); foreach (@vars) { ${$_} = param ($_}; }
...and that's it. Now I have magically created a batch of globals that I want... that I know I want, while properly ignoring anything extraneous that the form might be handing my CGI. Because it's a CGI, it is very unlikely that I'll want to use a "$name" variable somewhere else for some purpose other than sucking in the "name" element as in my example. (After all, it's easy to keep track of "form element = variable name") Plus, as I add (or subtract) values from my HTML form all I have to do is add (or subtract) elements from my @vars array.

Now, according to the (excellent) articles I should not use soft references but instead do something like this:

@vars = qw(name rank serial_num shoe_size); foreach (@vars) { $form_input{$_} = param ($_}; }
...but I mean, really, isn't that almost the same thing? The only thing this buys you is a (fractionally) smaller percentage of accidentally blowing one of your variables away, something that's even less likely to happen when you localize all your subs anyway.

Don't mean to be crazy here, but remember, variables don't kill people... people kill people.

Gary Blackburn
Trained Killer


In reply to Re: Variable variable names by Trimbach
in thread Variable variable names by cei

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.