I think haukex is right. I think a narrative of what's happening might be something like this:

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 (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 ''; } " ARGUMENT six five four STOP three two one BANG
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.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.