in reply to Re^2: why such an error happened?
in thread why such an error happened?

Because of the | in the middle of the regex there will never be a time where both parenthesis are used. That is, there will only be one set of parenthesis total. Ergo, $2 will never be defined.

I'm so adjective, I verb nouns!

chomp; # nom nom nom

Replies are listed 'Best First'.
Re^4: why such an error happened?
by lightoverhead (Pilgrim) on Oct 20, 2008 at 01:39 UTC
    Thank you for your posts. Now I begin to understand the warning messages. But I thought $1 or $2 will not be defined at the same time,i.e. for any moment, only either of them is defined, the other is undefined. We can say sometime $1 is undefined, sometime $2 is undefined; but I still don't think that $2 will never be defined, since in this example, "\U$2" indeed change "and" to "AND". Is my understanding right? Thanks.
      $1 and $2 are statically assigned when the regex is being compiled. At that time the regex-engine does not yet know which --if any-- of the parentheses will match. So $1 will refer to what is between the first set of parentheses (counting from the left); $2 refers to the second set (counting from the left); etc. ...

      In an (nested) alternation pattern (|) only one of these will ever match and that causes the warning.

      In your case you can safely ignore it.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Uhh, what?

      I think you are confusing what $1 and $2 are. $1 represents what was matched in the first set of matched parenthesis. $2 represents what was matched in the second set of matched parenthesis. Seeing as there will never be two matched sets of parenthesis in your regex, $2 will never be defined.

      I'm so adjective, I verb nouns!

      chomp; # nom nom nom

        lightoverhead is correct. Perhaps you missed the "g" modifier? There will be three matches.

        1. $1 = substr($chr, 0, 4); $2 = undef;
        2. $1 = undef;              $2 = substr($chr, 5, 3);
        3. $1 = substr($chr, 9, 4); $2 = undef;

        Update: I understand your confusion better now. I'll correct what you said:

        $1 represents what was matched in the first set of matched parenthesis. $2 represents what was matched in the second set of matched parenthesis.

        There's no such thing as "matched parentheses" Either the whole pattern matches or it doesn't.

        ________ $1 unconditionally refers to this capture / if the match was successful. | | __ $2 unconditionally refers to this capture | / if the match was successful. | | v v /(...)|(...)/

        It can easily be demonstrated:

        for (qw(a b)) { /(a)|(b)/; print( defined($1) ? $1 : '~', defined($2) ? $2 : '~', "\n" ); }
        a~ ~b