in reply to Variable variable names
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:
...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.@vars = qw(name rank serial_num shoe_size); foreach (@vars) { ${$_} = param ($_}; }
Now, according to the (excellent) articles I should not use soft references but instead do something like this:
...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.@vars = qw(name rank serial_num shoe_size); foreach (@vars) { $form_input{$_} = param ($_}; }
Don't mean to be crazy here, but remember, variables don't kill people... people kill people.
Gary Blackburn
Trained Killer
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: Re: Variable variable names
by btrott (Parson) on Oct 24, 2000 at 03:16 UTC | |
by Trimbach (Curate) on Oct 24, 2000 at 05:07 UTC | |
by merlyn (Sage) on Oct 24, 2000 at 09:05 UTC | |
|
RE: Re: Variable variable names
by chromatic (Archbishop) on Oct 24, 2000 at 08:15 UTC |