Trying to untaint some variables in Perl, and the following code works great:
if ($year =~ /^(\d{4})$/) {
$year = $1;
} else {
&invalid("year");
}
In the above instance, $1 contains $year if valid. However, when using the ?:
operator, $1 contains "1" when valid:
($year =~ /^(\d{4})$/) ? $year = $1 : &invalid("year");
Anyone see where I might be at fault? I'm confused why this is happening. It's only happening on this machine. Or rather, I have successfully used the ? operator for returning proper match variables for years. I haven't tried this piece of code on any other machine yet.
This is Perl, v5.8.8 built for x86_64-linux-thread-multi
Be careful about precedence here.
Contrast http://codepad.org/vEPnhWfH which works as expected, with http://codepad.org/nXVU5CA7 which does not. Of course, I tweaked the code a little bit to avoid calling
invalid
, but I suspect this may be at the root of the problem.In both cases, though
$1
contains "2011", so perhaps you should show additional code, as requested in the first comment by mu.UPDATE
I changed the codepad examples to use the call to
&invalid
and the error does not show up.Output is
http://codepad.org/z22GMEcn
EDIT 2 Also works on 5.12.3 on the Mac.
I agree with Jonathan that you may have found a bug in 5.8.8 that has been fixed since. If you are incredibly curious you can work your way through the Perl changes, such as: