.--- On Sat, 11 Mar 2006, Randal L. Schwartz wrote:
| For "why not symbolic references", the classic reference is MJD's piece on
| that at <http://perl.plover.com/varvarname.html>.
`---
>From the article:
"When people come into comp.lang.perl.misc asking how to do
something stupid, I'm never quite sure what to do. I can just
answer the question as asked, figuring that it's not my problem to
tell people that they're being stupid."
Well, it turns out to be a fine and informative article once you get
past the initial mortification ritual. To those of you reading
Dominus's stuff for the first time, I almost feel like I should
apologize for him. He's usually much better behaved, and he's well
worth reading. He seems fairly frustrated with reliving his own
developmental process through others.
Meanwhile, I refuse to join Schwartz's bout of badge-flashing
proscriptivism -- though I suppose if anyone's authoritative, he
certainly is (and when he cares to actually make arguments, they're
inevitably strong ones).
That being said: over time, I have gone back and undone all the places
I've ever used symrefs. I did this because I had gradually changed how
I thought about solving the problem, and not merely because it was
Wrong and Bad, or because I had come to believe that I was somehow
"stupid" when I wrote it. Data structures began to feel more right and
more simple than language tricks, and I came to really enjoy solving
problems as data rather then a tangle of strange flow. It's just a
style thing, and not a character weakness.
Anyway, onward. An alternative and my favorite way to deal with
params is to first collect only the ones you need into a nice, neat
hash:
my %param;
foreach my $param_name (qw(email firstname lastname)) {
$param{$param_name} = $r->param($param_name);
# Clean the values up here:
$param{$param_name} = s/^\s+//;
$param{$param_name} = s/\s+$//;
}
Once you've got them in the hash, you can do whatever you like with
them. To be extra-safe, each one should be matched against a subset of
valid characters to prevent mischief and later misery. This should be
done as soon after the copy as possible.
$param{name} = $r->param(name);
$param{name} =~ /([a-z])/i;
$param{name} = $1;
If you choose to carefully filter them this way (and I urge you to do
so because it will allow you to have total control of the data that
gets into your program), you'll need to either not use a loop as I did
above, or you'll need to do all the preening after the loop has
finished. I tend to do the preening after, but there's no compelling
advantage to ordering it either way. In the end, each parameter should
have checks that are specific and carefully crafted. Once filtered,
you can conveniently (and safely) refer to each parameter as:
$param{email}
It's easy on your eyes, and no symrefs are needed. A wonderful
advantage is: with symrefs, you'd have to separately store a list of
the params to know which variables to access. With a hash, it's just
"keys %param". Instant happiness.
Enjoy!
Best regards,
-- Patrick
--
.------ Patrick M. Jordan ------. Random unused band name:
| Systems/Network Administrator | Speling Counts
`----- Antistatic Matrix -------'
-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Mason-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mason-users