raygun has asked for the wisdom of the Perl Monks concerning the following question:
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?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: non-capture mode sometimes erases previous capture
by Corion (Patriarch) on May 30, 2018 at 09:16 UTC | |
by raygun (Scribe) on May 30, 2018 at 09:36 UTC | |
by BrowserUk (Patriarch) on May 30, 2018 at 09:51 UTC | |
by choroba (Cardinal) on May 30, 2018 at 11:01 UTC | |
by BillKSmith (Monsignor) on May 30, 2018 at 13:19 UTC | |
Re: non-capture mode sometimes erases previous capture
by haukex (Archbishop) on May 30, 2018 at 12:56 UTC | |
by raygun (Scribe) on Jun 01, 2018 at 18:00 UTC |