Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

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]
[marto]: what a day so far...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2017-12-18 11:10 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (482 votes). Check out past polls.