if (EXPR) BLOCK
if (EXPR) BLOCK else BLOCK
if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK
+ unless (EXPR) BLOCK
+ unless (EXPR) BLOCK else BLOCK
+ unless (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK
LABEL while (EXPR) BLOCK
LABEL while (EXPR) BLOCK continue BLOCK
LABEL until (EXPR) BLOCK
The C<if> statement is straightforward. Because BLOCKs are always
bounded by curly brackets, there is never any ambiguity about which
C<if> an C<else> goes with. If you use C<unless> in place of C<if>,
-the sense of the test is reversed.
+the sense of the test is reversed. Like C<if>, C<unless> can be followed
+by C<else>. C<unless> can even be followed by one or more C<elsif>
+statements, though you may want to think twice before using that particular
+language construct, as everyone reading your code will have to think at least
+twice before they can understand what's going on.
The C<while> statement executes the block as long as the expression is
L<true|/"Truth and Falsehood">.
increment a loop variable, even when the loop has been continued via
the C<next> statement.
+Extension modules can also hook into the Perl parser to define new
+kinds of compound statement. These are introduced by a keyword which
+the extension recognises, and the syntax following the keyword is
+defined entirely by the extension. If you are an implementor, see
+L<perlapi/PL_keyword_plugin> for the mechanism. If you are using such
+a module, see the module's documentation for details of the syntax that
+it defines.
+
=head2 Loop Control
X<loop control> X<loop, control> X<next> X<last> X<redo> X<continue>
when($_ ~~ $foo)
-In fact C<when(EXPR)> is treated as an implicit smart match most of the
-time. The exceptions are that when EXPR is:
+Most of the time, C<when(EXPR)> is treated as an implicit smart match of
+C<$_>, i.e. C<$_ ~~ EXPR>. (See L</"Smart matching in detail"> for more
+information on smart matching.) But when EXPR is one of the below
+exceptional cases, it is used directly as a boolean:
=over 4
=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.
C<default> behaves exactly like C<when(1 == 1)>, which is
to say that it always matches.
-See L</"Smart matching in detail"> for more information
-on smart matching.
-
=head3 Breaking out
You can use the C<break> keyword to break out of the enclosing
default { say '$foo does not contain a y' }
}
+=head3 Return value
+
+When a C<given> statement is also a valid expression (e.g.
+when it's the last statement of a block), it evaluates to :
+
+=over 4
+
+=item *
+
+an empty list as soon as an explicit C<break> is encountered.
+
+=item *
+
+the value of the last evaluated expression of the successful
+C<when>/C<default> clause, if there's one.
+
+=item *
+
+the value of the last evaluated expression of the C<given> block if no
+condition is true.
+
+=back
+
+In both last cases, the last expression is evaluated in the context that
+was applied to the C<given> block.
+
+Note that, unlike C<if> and C<unless>, failed C<when> statements always
+evaluate to an empty list.
+
+ my $price = do { given ($item) {
+ when ([ 'pear', 'apple' ]) { 1 }
+ break when 'vote'; # My vote cannot be bought
+ 1e10 when /Mona Lisa/;
+ 'unknown';
+ } };
+
+Currently, C<given> blocks can't always be used as proper expressions. This
+may be addressed in a future version of perl.
+
=head3 Switching in a loop
Instead of using C<given()>, you can use a C<foreach()> loop.
}
print "\@array contains $count copies of 'foo'\n";
-On exit from the C<when> block, there is an implicit C<next>.
+At the end of all C<when> blocks, there is an implicit C<next>.
You can override that with an explicit C<last> if you're only
interested in the first match.
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.)
+
+Note that the "Matching Code" column is not always an exact rendition. For
+example, the smart match operator short-circuits whenever possible, but
+C<grep> does not.
$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]
- Array Hash hash slice existence grep { exists $b->{$_} } @$a
+ Hash Hash hash keys identical (every key is found in both hashes)
+ Array Hash hash keys intersection grep { exists $b->{$_} } @$a
Regex Hash hash key grep grep /$a/, keys %$b
undef Hash always false (undef can't be a key)
Any Hash hash entry existence exists $b->{$a}
- Hash Array hash slice existence grep { exists $a->{$_} } @$b
+ Hash Array hash keys intersection grep { exists $a->{$_} } @$b
Array Array arrays are comparable[2]
Regex Array array grep grep /$a/, @$b
undef Array array contains undef grep !defined, @$b
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 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
-
-The "matching code" doesn't represent the I<real> matching code,
-of course: it's just there to explain the intended meaning. Unlike
-C<grep>, the smart match operator will short-circuit whenever it can.
+ 4 - either a real number, or a string that looks like a number
=head3 Custom matching via overloading
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<~~>.
+the C<~~> operator. This may alter the usual smart match semantics.
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
underlying structure).
+Note also that smart match's matching rules take precedence over
+overloading, so if C<$obj> has smart match overloading, then
+
+ $obj ~~ X
+
+will not automatically invoke the overload method with X as an argument;
+instead the table above is consulted as normal, and based in the type of X,
+overloading may or may not be invoked.
+
+See L<overload>.
+
=head3 Differences from Perl 6
The Perl 5 smart match and C<given>/C<when> constructs are not
# example: '# line 42 "new_filename.plx"'
/^\# \s*
line \s+ (\d+) \s*
- (?:\s("?)([^"]+)\2)? \s*
+ (?:\s("?)([^"]+)\g2)? \s*
$/x
with C<$1> being the line number for the next line, and C<$3> being