Because the latter two regular expressions use the /n option, they should have no effect on the existing values of $1, $2, and $3. And in fact the first one does not. But the second one erases all three.#!/usr/bin/perl $_ = 'abcdefg'; # define a string /(..)(..)(..)/; # break the string into 3 two-character chunks print "$1 $2 $3\n"; # print the chunks /(j)/n; # search for a letter that's not in the string print "$1 $2 $3\n"; # print the chunks /(g)/n; # search for a letter that's in the string print "$1 $2 $3\n"; # print the chunks
I grant you, the documentation for /n is a little sketchy, saying only "Non-capture mode. Don't let () fill in $1, $2, etc..." It doesn't actually specify what happens to existing values of these variables. But I would expect consistent behavior: it should either always preserve the values (and this seems the ideal — what is ever gained from overwriting them when the user has asked that they not be populated?), or always erase them. Erasing them in the case of a successful match but not a failed one is at least unexpected, if it doesn't rise to the level of outright bug.
Or, I'm fundamentally misunderstanding something about /n. What says the wisdom of the monastery?In reply to non-capture mode sometimes erases previous capture by raygun
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |