Ok, I've looked at it a little further now. I omitted a bit in my quote from perlop:

If the PATTERN evaluates to the empty string, the last successfully matched regular expression is used instead. ... If no match has previously succeeded, this will (silently) act instead as a genuine empty pattern (which will always match).

I think there is something omitted from the documentation - AnomalousMonk describes this as well:

$ perl -le'print"x"=~//?"yes":"no"; "y"=~/y/; print"x"=~//?"yes":"no"' yes no $ perl -le'print"x"=~//?"yes":"no";{"y"=~/y/} print"x"=~//?"yes":"no"' yes yes

So in other words, the "last successfully matched regular expression" means the "last successfully matched regular expression in this scope", which explains why the successful match when $last is "Config" doesn't affect the other matches. I stumbled on this at first; I think it might be worth a documentation patch...

Anyway, that means in the OP's example, we can remove startStandardServices('Config') since that's working as expected, and since the other two calls of startStandardServices and startGeneralServices are seeing the same issue each, the script can be reduced to:

use warnings; use 5.014; my @stoppedGeneralServices = ('OP Mover', 'OP Monitor'); my $supported = '4.0.0,4.0.1,4.1.0,4.1.1,4.1.2'; $supported =~ /\Q4.1.2/ if @ARGV; startGeneralServices(); sub startGeneralServices { my $last = shift; my $name; while (@stoppedGeneralServices) { $name = pop @stoppedGeneralServices; say ' Starting $name=\'', $name, '\' $last=', defined $last ? "'$last'" : 'undef', ' ($name=~/$last/i)=', ($name =~ /$last/i) ? 1 : 0; last if $name =~ /$last/i; } }

And taking that a few steps further:

use warnings; use 5.014; '4.1.1,4.1.2' =~ /\Q4.1.2/ if @ARGV; my $last = ''; my $name = 'OP Mover'; say '$name=\'', $name, '\' $last=', defined $last ? "'$last'" : 'undef', ', $name=~/$last/i = ', ($name =~ /$last/i) ? 1 : 0;
$ perl 11102215.pl $name='OP Mover' $last='', $name=~/$last/i = 1 $ perl 11102215.pl all $name='OP Mover' $last='', $name=~/$last/i = 0

Which shows that:


In reply to Re^3: Side effect of using undefined variable in regex by haukex
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.