There's more than one way to do things | |
PerlMonks |
Re^5: RegExp substitutionby AnomalousMonk (Archbishop) |
on Apr 11, 2014 at 00:01 UTC ( [id://1081892]=note: print w/replies, xml ) | Need Help?? |
... The critical thing to realize about this code is that the capture variables $2 $3 $4 have never been set to any meaningful value. I.e., they have the undefined value undef. When the undefined value is interpolated into a string or a regex, it interpolates as '' (the empty string), or, in the case of a regex, // (the empty regex). ...
This pair of statements and corresponding succeeding statement pairs is very interesting. I strongly recommend you insert the statement
Here's a narrative. As you can see from the newly-added debug print statement, the first
The second
The reason for this odd behavior is that when $2 with an undefined value interpolates into s/$2/$second/g; it produces the // empty regex match pattern. This pattern is special: it uses the last successful regex match pattern for matching. The last successful match pattern was in the /([A-Z][a-z]+)/g; statement immediately before the s/// substitution statement. Therefore, And similarly for each subsequent //; s///; statement pair. That ought to give you something to think about while you're reviewing the regex documentation. (BTW: The /g modifier in the /([A-Z][a-z]+)/g; statement is at best useless and at worst confusing and corrupting. You cannot use the /g modifier in this way to "keep track" of match positions in successive matches. (The /c modifier in conjunction with the /g modifier does something like this in certain cases, but I don't really see how it could be adapted to serve here.) You will have to think of some other way to query the user about successive numbers in the original string so that they may be 'corrected' one by one.)
In Section
Seekers of Perl Wisdom
|
|