Your use of $1 and $2 should be fine, because it only occurs if both regexes match, and the second one (with the capturing parens) will be matched last.

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:

#!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
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.

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)/);

In reply to Re: Using multiple regex's by chipmunk
in thread Using multiple regex's by leons

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.