X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlsyn.pod;h=4e1bc0a8a7b9ca62b24bb1d3dcda1cff90f8313f;hb=93592fd5aeec89ac25994a493ef54e1d7a572d65;hp=e8ef30f09de229bf396f4366469cd9e52d0b57dc;hpb=54a85b95f465893ba23bf8c7ad5b933bde134505;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perlsyn.pod b/pod/perlsyn.pod index e8ef30f..4e1bc0a 100644 --- a/pod/perlsyn.pod +++ b/pod/perlsyn.pod @@ -117,7 +117,7 @@ is treated as 0. =head2 Statement Modifiers X X X X X -X X X +X X X X Any simple statement may optionally be followed by a I modifier, just before the terminating semicolon (or block ending). The possible @@ -127,6 +127,8 @@ modifiers are: unless EXPR while EXPR until EXPR + when EXPR + for LIST foreach LIST The C following the modifier is referred to as the "condition". @@ -139,6 +141,22 @@ the condition is true (i.e., if the condition is false). print "Basset hounds got long ears" if length $ear >= 10; go_outside() and play() unless $is_raining; +C executes the statement I C<$_> smart matches C, and +then either Cs out if it's enclosed in a C scope or skips +to the C element when it lies directly inside a C loop. +See also L. + + 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 modifier is an iterator: it executes the statement once for each item in the LIST (with C<$_> aliased to each item in turn). @@ -237,7 +255,7 @@ C an C goes with. If you use C in place of C, the sense of the test is reversed. The C 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. The C statement executes the block as long as the expression is false. The LABEL is optional, and if present, consists of an identifier followed @@ -254,6 +272,14 @@ conditional is about to be evaluated again. Thus it can be used to increment a loop variable, even when the loop has been continued via the C 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 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 X X X X X @@ -516,24 +542,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); } @@ -555,53 +576,63 @@ is exactly equivalent to when($_ ~~ $foo) -(though you need to enable the "~~" feature before you -can use the C<~~> operator directly). In fact C -is treated as an implicit smart match most of the time. The -exceptions are that when EXPR is: +Most of the time, C is treated as an implicit smart match of +C<$_>, i.e. C<$_ ~~ EXPR>. (See L 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 o +=item * a subroutine or method call -=item o +=item * a regular expression match, i.e. C or C<$foo =~ /REGEX/>, -or a negated regular expression match C<$foo !~ /REGEX/>. +or a negated regular expression match (C or C<$foo !~ /REGEX/>). -=item o +=item * a comparison such as C<$_ E 10> or C<$x eq "abc"> (or of course C<$_ ~~ $c>) -=item o +=item * C, C, or C -=item o +=item * -A negated expression C or C, or a logical +a negated expression C or C, 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 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 @@ -609,19 +640,16 @@ is applied recursively to the first argument. 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, it is turned into a +or hash as the argument to C, it is turned into a reference. So C is the same as C, for example. C behaves exactly like C, which is to say that it always matches. -See L for more information -on smart matching. - =head3 Breaking out You can use the C keyword to break out of the enclosing @@ -636,7 +664,7 @@ case to the next: 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 @@ -651,7 +679,7 @@ string occurs in an array: } print "\@array contains $count copies of 'foo'\n"; -On exit from the C block, there is an implicit C. +At the end of all C blocks, there is an implicit C. You can override that with an explicit C if you're only interested in the first match. @@ -661,54 +689,75 @@ variable C<$_>. (You can use C.) =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.) +Note that the "Matching Code" column is not always an exact rendition. For +example, the smart match operator short-circuits whenever possible, but +C does not. $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 value slice truth grep $_, @$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 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} - Any undef undefined !defined $a + 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 + 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 + undef Any undefined !defined($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) - * - 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 matching code, -of course: it's just there to explain the intended meaning. Unlike -C, the smart match operator will short-circuit whenever it can. + 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 =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. +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. =head3 Differences from Perl 6 @@ -716,7 +765,8 @@ See L. The Perl 5 smart match and C/C 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 and C. Parentheses in Perl 6 +the argument to C and C (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, C, or C; they can't be made optional in Perl 5 without a great deal of potential confusion, because Perl 5 @@ -729,16 +779,9 @@ would parse the expression as though the argument to C 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 will always do an implicit smart match with its argument, whilst it is convenient in Perl 5 to