In the “Programming Perl” book there is a snippet (cutted):
By default, when(EXPR) is treated as an implicit smartmatch of
$_;that is,$_ ~~ EXPR. However, if the EXPR argument to when is one of the 10 exceptional forms listed below,
it is evaluated directly for a Boolean result, and no smartmatching occurs:
…
A regular expression match in the form of /REGEX/, $foo =~ /REGEX/, or $foo
=~ EXPR.
What does it mean evaluated directly for a Boolean result?
Example:
#!/usr/bin/perl
use v5.14;
my @a = ('aaa', 'bbb', 'ccc');
given(@a) {
when (/a/) { say '@a contains an a'; }
default { say '@a does not contain an a' }
}
when I run it the output varies from time to time:
@a does not contain an a
@a contains an a
@a does not contain an a
@a does not contain an a
I can’t understand what happens here, can anyone be so pleasant to help?
Appreciation in advance.
Read the documentation carefully:
Therefore,
given (@a)is turned intogiven(\@a). There is no smart matching, because you usewhen (/a/), so you are trying to matchThe reference is stringified. It sometimes contains “a”, as in
ARRAY(0x9a4e7f8), but usually does not 🙂