=head2 Statement Modifiers
X<statement modifier> X<modifier> X<if> X<unless> X<while>
-X<until> X<foreach> X<for>
+X<until> X<when> X<foreach> X<for>
Any simple statement may optionally be followed by a I<SINGLE> modifier,
just before the terminating semicolon (or block ending). The possible
unless EXPR
while EXPR
until EXPR
+ when EXPR
+ for LIST
foreach LIST
The C<EXPR> following the modifier is referred to as the "condition".
print "Basset hounds got long ears" if length $ear >= 10;
go_outside() and play() unless $is_raining;
+C<when> executes the statement I<when> C<$_> smart matches C<EXPR>, and
+then either C<break>s out if it's enclosed in a C<given> scope or skips
+to the C<next> element when it lies directly inside a C<for> loop.
+See also L</"Switch statements">.
+
+ given ($something) {
+ $abc = 1 when /^abc/;
+ $just_a = 1 when /^a/;
+ $other = 1;
+ }
+
+ for (@names) {
+ admin($_) when [ qw/Alice Bob/ ];
+ regular($_) when [ qw/Chris David Ellen/ ];
+ }
+
The C<foreach> modifier is an iterator: it executes the statement once
for each item in the LIST (with C<$_> aliased to each item in turn).
=item *
-the C<...> flip-flop operator (but B<not> the two-dot version C<..>, which
-is used to construct and test against numerical or string ranges).
+the C<..> and C<...> flip-flop operators.
=back
when (/^\d+$/ && $_ < 75) { ... }
Another useful shortcut is that, if you use a literal array
-or hash as the argument to C<when>, it is turned into a
+or hash as the argument to C<given>, it is turned into a
reference. So C<given(@foo)> is the same as C<given(\@foo)>,
for example.
are. The behaviour is determined by the following table: the first row
that applies determines the match behaviour (which is thus mostly
determined by the type of the right operand). Note that the smart match
-implicitly dereferences any hash or array ref, so the "Hash" and "Array"
-entries apply in those cases.
+implicitly dereferences any non-blessed hash or array ref, so the "Hash"
+and "Array" entries apply in those cases. (For blessed references, the
+"Object" entries apply.)
$a $b Type of Match Implied Matching Code
====== ===== ===================== =============
Any undef undefined !defined $a
- Any Object invokes ~~ overloading on $object, or dies
+ Any Object invokes ~~ overloading on $object, or dies
Hash CodeRef sub truth for each key[1] !grep { !$b->($_) } keys %$a
Array CodeRef sub truth for each elt[1] !grep { !$b->($_) } @$a
Any CodeRef scalar sub truth $b->($a)
- Hash Hash hash keys identical [sort keys %$a]~~[sort keys %$b]
+ Hash Hash hash keys identical (every key is found in both hashes)
Array Hash hash slice existence grep { exists $b->{$_} } @$a
Regex Hash hash key grep grep /$a/, keys %$b
undef Hash always false (undef can't be a key)
Array Regex array grep grep /$b/, @$a
Any Regex pattern match $a =~ /$b/
- undef Range[4] always false
- Any Range[4] in range
-
+ Object Any invokes ~~ overloading on $object, or falls back:
Any Num numeric equality $a == $b
- Num numish[5] numeric equality $a == $b
+ Num numish[4] numeric equality $a == $b
+ undef Any undefined !defined($b)
Any Any string equality $a eq $b
-
1 - empty hashes or arrays will match.
- 2 - that is, each element matches the element of same index in the other
- array. [3]
+ 2 - that is, each element smart-matches the element of same index in the
+ other array. [3]
3 - If a circular reference is found, we fall back to referential equality.
- 4 - a range is written EXPR..EXPR (using the C<..> range operator, but
- NOT the three-dot version C<...>, which will be treated as a boolean
- operator). Numeric ranges will use numeric comparison: that is,
- "4.5 ~~ 3..5" will be true.
- 5 - either a real number, or a string that looks like a number
+ 4 - either a real number, or a string that looks like a number
The "matching code" doesn't represent the I<real> matching code,
of course: it's just there to explain the intended meaning. Unlike
You can change the way that an object is matched by overloading
the C<~~> operator. This trumps the usual smart match semantics.
-See L<overload>. Since smart matching dispatch is driven by the
-right hand side argument, overloading applies only when the object
-is on the right of C<~~>.
+See L<overload>.
It should be noted that C<~~> will refuse to work on objects that
don't overload it (in order to avoid relying on the object's
The Perl 5 smart match and C<given>/C<when> constructs are not
absolutely identical to their Perl 6 analogues. The most visible
difference is that, in Perl 5, parentheses are required around
-the argument to C<given()> and C<when()>. Parentheses in Perl 6
+the argument to C<given()> and C<when()> (except when this last
+one is used as a statement modifier). Parentheses in Perl 6
are always optional in a control construct such as C<if()>,
C<while()>, or C<when()>; they can't be made optional in Perl
5 without a great deal of potential confusion, because Perl 5