Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: "Correct" program style questions

by Aristotle (Chancellor)
on Oct 26, 2002 at 00:00 UTC ( #208179=note: print w/replies, xml ) Need Help??

in reply to "Correct" program style questions

Here's a condensed form that can correctly differentiate all cases as per sauoq's requirements elsewhere in this thread. It certainly isn't something for the beginner's class though.
my ($foo, $bar, $baz, $quux); defined && /^([[:alpha:]]+)$/ ? $foo = $1 : taint_fail('foo') for scal +ar param('foo'); defined && /^([[:alpha:]]+)$/ ? $bar = $1 : taint_fail('bar') for scal +ar param('bar'); defined && /^([[:alpha:]]+)$/ ? $baz = $1 : taint_fail('baz') for scal +ar param('baz'); defined && /^([[:alpha:]]+)$/ ? $quux = $1 : taint_fail('quux') for sc +alar param('quux');
But any remotely hubristic programmer will of course write this:
my %check = ( foo => qr/^([[:alpha:]]+)$/, bar => qr/^([[:alpha:]]+)$/, baz => qr/^([[:alpha:]]+)$/, quux => qr/^([[:alpha:]]+)$/, ); my %f; for my $pname (keys %check) {; defined && /$check{$pname}/ ? $f{$pname} = $1 : untaint_fail($pnam +e) for scalar param($pname); }

This will call untaint_fail only when the regex failed, and preserve undef vs empty string where they're valid input.

Update: Oops. The code would previously leave things tainted since it was assigning $1 back to the tainted variable. Shifting things around minorly fixed that.

Makeshifts last the longest.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://208179]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2016-10-22 12:41 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (294 votes). Check out past polls.