From: David Mitchell Date: Sat, 22 Aug 2009 19:43:00 +0000 (+0100) Subject: better document smart match overloading X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0de1c906c34397b53c088e443cd0325d9c209649;p=p5sagit%2Fp5-mst-13.2.git better document smart match overloading --- diff --git a/lib/overload.pm b/lib/overload.pm index 7790832..d5ac5ad 100644 --- a/lib/overload.pm +++ b/lib/overload.pm @@ -455,6 +455,31 @@ The key C<"~~"> allows you to override the smart matching logic used by the C<~~> operator and the switch construct (C/C). See L and L. +Unusually, overloading of the smart match operator does not automatically +take precedence over normal smart match behaviour. In particular, in the +following code: + + package Foo; + use overload '~~' => 'match'; + + my $obj = Foo->new(); + $obj ~~ [ 1,2,3 ]; + +the smart match does I invoke the method call like this: + + $obj->match([1,2,3],0); + +rather, the smart match distributive rule takes precedence, so $obj is +smart matched against each array element in turn until a match is found, +so you may see between one and three of these calls instead: + + $obj->match(1,0); + $obj->match(2,0); + $obj->match(3,0); + +Consult the match table in L for +details of when overloading is invoked. + =item * I '${}', '@{}', '%{}', '&{}', '*{}'. diff --git a/pod/perlsyn.pod b/pod/perlsyn.pod index c7db812..59f268e 100644 --- a/pod/perlsyn.pod +++ b/pod/perlsyn.pod @@ -735,13 +735,23 @@ C does not. =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. +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 The Perl 5 smart match and C/C constructs are not