the sense of the test is reversed.
The C<while> statement executes the block as long as the expression is
-true (does not evaluate to the null string C<""> or C<0> or C<"0">).
+L<true|/"Truth and Falsehood">.
The C<until> statement executes the block as long as the expression is
false.
The LABEL is optional, and if present, consists of an identifier followed
when($_ ~~ $foo)
-(though you need to enable the "~~" feature before you
-can use the C<~~> operator directly). In fact C<when(EXPR)>
-is treated as an implicit smart match most of the time. The
-exceptions are that when EXPR is:
+In fact C<when(EXPR)> is treated as an implicit smart match most of the
+time. The exceptions are that when EXPR is:
=over 4
-=item o
+=item *
a subroutine or method call
-=item o
+=item *
a regular expression match, i.e. C</REGEX/> or C<$foo =~ /REGEX/>,
or a negated regular expression match C<$foo !~ /REGEX/>.
-=item o
+=item *
a comparison such as C<$_ E<lt> 10> or C<$x eq "abc">
(or of course C<$_ ~~ $c>)
-=item o
+=item *
C<defined(...)>, C<exists(...)>, or C<eof(...)>
-=item o
+=item *
A negated expression C<!(...)> or C<not (...)>, or a logical
exclusive-or C<(...) xor (...)>.
These rules look complicated, but usually they will do what
you want. For example you could write:
- when (/^\d$/ && $_ < 75) { ... }
+ 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
given($foo) {
when (/x/) { say '$foo contains an x'; continue }
when (/y/) { say '$foo contains a y' }
- default { say '$foo contains neither an x nor a y' }
+ default { say '$foo does not contain a y' }
}
=head3 Switching in a loop
Any Code[+] scalar sub truth $b->($a)
Hash Hash hash keys identical [sort keys %$a]~~[sort keys %$b]
- Hash Array hash value slice truth grep $_, @$a{@$b}
+ Hash Array hash slice existence grep {exists $a->{$_}} @$b
Hash Regex hash key grep grep /$b/, keys %$a
Hash Any hash entry existence exists $a->{$b}
+ - this must be a code reference whose prototype (if present) is not ""
(subs with a "" prototype are dealt with by the 'Code()' entry lower down)
- * - if a circular reference is found, we fall back to referential equality
+ * - that is, each element matches the element of same index in the other
+ array. If a circular reference is found, we fall back to referential
+ equality.
! - either a real number, or a string that looks like a number
The "matching code" doesn't represent the I<real> matching code,
as though the argument to C<given> were an element of the hash
C<%foo>, interpreting the braces as hash-element syntax.
-The table of smart matches is not identical to that proposed
-by the Perl 6 specification Synopsis 4. Some of the differences
-are simply a consequence of Perl 5's different data model, while
-other changes have been made to address problems with the Perl 6
-proposal. For example, the Perl 6 specification implies that
-C<$string ~~ qr/regex/> would test string equality, rather than
-doing a regular expression match. On the other hand, informal
-examples elsewhere make it clear that a regular expression
-match is the intended behaviour. Thus the Synopsis 4 smart
-match specification cannot yet be regarded as definitive.
+The table of smart matches is not identical to that proposed by the
+Perl 6 specification, mainly due to the differences between Perl 6's
+and Perl 5's data models.
In Perl 6, C<when()> will always do an implicit smart match
with its argument, whilst it is convenient in Perl 5 to