in reply to why such an error happened?

It's not an error, it's a warning.

And you're getting this warning because $2 is undefined.

Take a closer look at your regular expression. You only have one set of capturing parentheses on each side of the alternation, so you're only ever going to get one match. ie. $2 will always be undefined.

Cheers,
Darren

Replies are listed 'Best First'.
Re^2: why such an error happened?
by lightoverhead (Pilgrim) on Oct 20, 2008 at 01:16 UTC
    Thank you Darren. But I am still confused. $1 is matched to "this" or "that", so they will not be upper cased. $2 matched to other words other than "this" or "that" and change them to upper cases, in this example, the "and" was changed to "AND". It seems to me that the "and" was assigned to $2, why you thought that $2 will always be undefined? Thanks.

      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

        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.
Re^2: why such an error happened?
by JavaFan (Canon) on Oct 20, 2008 at 09:36 UTC
    You only have one set of capturing parentheses on each side of the alternation, so you're only ever going to get one match. ie. $2 will always be undefined.
    Not true.

    What is true that you get only one defined capture (if there's a match), but that one can very well be $2.