I was frightened today. A co-worker showed me some code from a client. We are co-developing a site with him. He apparently does this all the time:
foreach $key (CGI::param()) { ${$key} = CGI::param($key) }
(Assigning the HTML field name to the symbol table)

Now, he can use the HTML field names as variables (e.g. $Username, $Password, etc.) My co-worker defended this idea, saying it is entirely normal (he did it "all the time" in TCL), and that this feels much more readable to him.

So, my question: is this a "normal" thing to do (and I've just never stumbled across the idea) or am I right to have my skin crawl thinking of this?

This just can't be a "good thing," but it's not my code and not my project, so...

<cringing>Russ</cringing>

P.S. I explained the dangers of messing with the symbol table, the risks that someone will name a field (in HTML) to a used/reserved name in Perl, and the loss of maintainability (a variable which just "magically" appears without being referenced anywhere should confuse people). I told him it is considered good practice to run with 'use strict', and that this will not. In the end, I conceded that "yes, the code will work that way..." and our client wrote it, so...


In reply to Putting HTML fieldnames in symbol space by Russ

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.