|Perl Monk, Perl Meditation|
Re: why such an error happened?by krusty (Hermit)
|on Oct 20, 2008 at 01:44 UTC||Need Help??|
Your global substitution matches the string you gave it three times.
The first match only matches "this" and stores it in $1. $2 either contains an empty string or is undefined (I don't know which as it's never mattered to my code).
The second match doesn't match "this" or "that" so it moves on to one or more word characters and matches "and" and stores it in $2. This time $1 is undef.
The last match matches "that" and stores it once again in $1. $2 is undef.
It seems as if you are possibly confused by either the number of times the substitution actually matches or by which portion of the string gets assigned to $1 and $2.
The reason why you are getting the warning, is that each time the substitution matches, the replacement pattern contains an uninitialized value in $2, $1, and $2 respectively.
If you are having trouble figuring out which portion of the string would be assigned to $1 and $2, then the following code may help. If you can count left parentheses, you can figure out what part of the string will be assigned to which variable from left to right. For instance the following string will match as follows.