I think haukex is right. I think a narrative of what's happening might be something like this:
In this example (no side-effect match at all), the first call to func('STOP') loops until it sees a match on STOP, as expected. The second call to func() (warnings not enabled!) matches on an empty pattern ( /undef/ -> //) because there has been no previous successful match in the dynamic scope of the matching operator. Matching on // always matches, so the while-loop terminates after three.c:\@Work\Perl\monks>perl -Mstrict -le "my @services = qw(BANG one two three STOP four five six); ;; my $side_effect = 'xxx'; $side_effect =~ /xxx/ if @ARGV; ;; func('STOP'); ;; func(); ;; sub func { my $done = shift; while (my $serv = pop @services) { printf qq{$serv }; last if $serv =~ /$done/i } print ''; } " six five four STOP three
In this example, identical except that an argument is passed to the script and a successful side-effect match occurs, the first call to func('STOP') runs as before. The second call to func() matches on the pattern /xxx/ because there has been a previous successful match in the dynamic scope of the matching operator, the side-effect match /xxx/. Matching on /xxx/ never succeeds, so the while-loop runs to completion: BANG.c:\@Work\Perl\monks>perl -Mstrict -le "my @services = qw(BANG one two three STOP four five six); ;; my $side_effect = 'xxx'; $side_effect =~ /xxx/ if @ARGV; ;; func('STOP'); ;; func(); ;; sub func { my $done = shift; while (my $serv = pop @services) { printf qq{$serv }; last if $serv =~ /$done/i } print ''; } " ARGUMENT six five four STOP three two one BANG
The critical point is that the match in func() during the second call is not in the dynamic scope of the match during the first call, but only in the dynamic scope of the side-effect match, should it have occurred.
If that makes any sense...
Run under Perl version 5.8.9 without warnings.
Update: A few trivial wording changes (update: and a spelling correction).
Give a man a fish: <%-{-{-{-<
In reply to Re: Side effect of using undefined variable in regex
by AnomalousMonk
in thread Side effect of using undefined variable in regex
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |