This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Mention parallel testing in INSTALL and perl5110delta.pod
[perl5.git] / pod / perlsyn.pod
index 28bb824..2ba30d8 100644 (file)
@@ -534,24 +534,19 @@ This construct is very flexible and powerful. For example:
        when (undef) {
            say '$foo is undefined';
        }
-       
        when ("foo") {
            say '$foo is the string "foo"';
        }
-       
        when ([1,3,5,7,9]) {
            say '$foo is an odd digit';
            continue; # Fall through
        }
-       
        when ($_ < 100) {
            say '$foo is numerically less than 100';
        }
-       
        when (\&complicated_check) {
-           say 'complicated_check($foo) is true';
+           say 'a complicated check for $foo is true';
        }
-       
        default {
            die q(I don't know what to do with $foo);
        }
@@ -585,7 +580,7 @@ a subroutine or method call
 =item *
 
 a regular expression match, i.e. C</REGEX/> or C<$foo =~ /REGEX/>,
-or a negated regular expression match C<$foo !~ /REGEX/>.
+or a negated regular expression match (C<!/REGEX/> or C<$foo !~ /REGEX/>).
 
 =item *
 
@@ -598,26 +593,36 @@ C<defined(...)>, C<exists(...)>, or C<eof(...)>
 
 =item *
 
-A negated expression C<!(...)> or C<not (...)>, or a logical
+a negated expression C<!(...)> or C<not (...)>, or a logical
 exclusive-or C<(...) xor (...)>.
 
+=item *
+
+a filetest operator, with the exception of C<-s>, C<-M>, C<-A>, and C<-C>,
+that return numerical values, not boolean ones.
+
+=item *
+
+the C<..> and C<...> flip-flop operators.
+
 =back
 
-then the value of EXPR is used directly as a boolean.
+In those cases the value of EXPR is used directly as a boolean.
+
 Furthermore:
 
 =over 4
 
-=item o
+=item *
 
 If EXPR is C<... && ...> or C<... and ...>, the test
 is applied recursively to both arguments. If I<both>
 arguments pass the test, then the argument is treated
 as boolean.
 
-=item o
+=item *
 
-If EXPR is C<... || ...> or C<... or ...>, the test
+If EXPR is C<... || ...>, C<... // ...> or C<... or ...>, the test
 is applied recursively to the first argument.
 
 =back
@@ -677,47 +682,51 @@ variable C<$_>. (You can use C<for my $_ (@array)>.)
 
 =head3 Smart matching in detail
 
-The behaviour of a smart match depends on what type of thing
-its arguments are. It is always commutative, i.e. C<$a ~~ $b>
-behaves the same as C<$b ~~ $a>. The behaviour is determined
-by the following table: the first row that applies, in either
-order, determines the match behaviour.
-
+The behaviour of a smart match depends on what type of thing its arguments
+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 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
     ======  =====     =====================    =============
-    (overloading trumps everything)
+    Any     undef     undefined                !defined $a
 
-    Code[+] Code[+]   referential equality     $a == $b
-    Any     Code[+]   scalar sub truth         $b->($a)
+    Any     Object    invokes ~~ overloading on $object, or dies
 
-    Hash    Hash      hash keys identical      [sort keys %$a]~~[sort keys %$b]
-    Hash    Array     hash slice existence     @$b == grep {exists $a->{$_}} @$b
-    Hash    Regex     hash key grep            grep /$b/, keys %$a
-    Hash    Any       hash entry existence     exists $a->{$b}
+    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)
 
-    Array   Array     arrays are identical[*]
-    Array   Regex     array grep               grep /$b/, @$a
-    Array   Num       array contains number    grep $_ == $b, @$a
-    Array   Any       array contains string    grep $_ eq $b, @$a
+    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)
+    Any     Hash      hash entry existence     exists $b->{$a}
 
-    Any     undef     undefined                !defined $a
+    Hash    Array     hash slice existence     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
+    Any     Array     match against an array element[3]
+                                               grep $a ~~ $_, @$b
+
+    Hash    Regex     hash key grep            grep /$b/, keys %$a
+    Array   Regex     array grep               grep /$b/, @$a
     Any     Regex     pattern match            $a =~ /$b/
-    Code()  Code()    results are equal        $a->() eq $b->()
-    Any     Code()    simple closure truth     $b->() # ignoring $a
-    Num     numish[!] numeric equality         $a == $b
-    Any     Str       string equality          $a eq $b
-    Any     Num       numeric equality         $a == $b
 
+    Object  Any       invokes ~~ overloading on $object, or falls back:
+    Any     Num       numeric equality         $a == $b
+    Num     numish[4] numeric equality         $a == $b
     Any     Any       string equality          $a eq $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)
- * - 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
+ 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 - 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
@@ -729,6 +738,10 @@ 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>.
 
+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).
+
 =head3 Differences from Perl 6
 
 The Perl 5 smart match and C<given>/C<when> constructs are not