in reply to Using multiple regex's
More problematic is the use of my() with the if modifier. The behavior of my in such a construct is actually little-known bug/feature of Perl.
my() is partly executed at compile time, and partly executed at runtime. At compile time, some space is allocated for the lexical variable. At run time, the variable is reset, which happens each time execution leaves the scope of the my declaration.
Using my() with the if modifier means that the space is allocated at compile time as usual, but the variable is reset only if the conditional is true. If the conditional is false, the value of the variable is preserved for the next execution. Here's an example of this odd behavior:
This behavior, which I believe was originally unintentional, has been left in because people are using it to get static variables, which are private variables that keep their values between executions of a subroutine.#!perl -l sub blah { my $x if $foo; print $x++; } for (0..3) { blah() } $foo = 1; for (0..3) { blah() } __END__ 0 1 2 3 4 0 0 0
You can avoid this behavior in your script by separating the my and the assignment:
my ($var1,$var2); ($var1,$var2) = ($1,$2) if ($value1=~/something/ && $value2=~/(what)(e +ver)/);
|
|---|