The when Statement and Modifier
Most of the power of a given
comes from the implicit smartmatching that various data types imply.
By default, when(EXPR) is treated as an implicit smartmatch of
$_; that is, $_ ~~ EXPR.
(See Chapter 3 for more details on smartmatching.)
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 user-defined subroutine call or a method invocation.
A regular expression match in the form of
/REGEX/,$foo =~ /REGEX/, or$foo =~EXPR.A smartmatch that uses an explicit
~~operator, such asEXPR~~EXPR. (You might, for instance, want to use an explicit smartmatch against$_when you need to reverse the default polymorphism ofwhen’s built-in smartmatching.)A relational operator such as
$_ < 10or$x eq "abc"that returns a Boolean result. This includes the six numeric comparisons (<,>,<=,>=,==, and!=), and the six string comparisons (lt,gt,le,ge,eq, andne).The three built-in functions
defined,exists, andeof.A negated expression, whether
!EXPRornot(EXPR), or a logical exclusive or,EXPR1xorEXPR2. (The bitwise versions [~and^] are not included.) Negated regular expressions also fall in this category, whichever way you write them:!/REGEX/,$foo !~ /REGEX/, or$foo !~EXPR.A file test operator (apart from
–s,–M,–A, and–C, as these return numbers, not Booleans).The
..and...flip-flop operators. (Note that the...infix operator is completely ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access