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:
- If @ARGV is false, there is no previously executed regex, so /$last/ aka // acts like the empty pattern, which always matches.
- If @ARGV is true, then // means to use the previous successful regex, /\Q4.1.2/, which does not match the string 'OP Mover'.
|