summaryrefslogtreecommitdiff
path: root/lib/overload.pm
diff options
context:
space:
mode:
authorFather Chrysostomos <sprout@cpan.org>2012-05-19 23:46:04 -0700
committerFather Chrysostomos <sprout@cpan.org>2012-05-21 18:09:29 -0700
commit50853fa94fa95a4fad70b61c9360709826bb8093 (patch)
tree08332f7abef8085b0f9fdb914f0850a60d92b3ab /lib/overload.pm
parent27b36e73a18363f49fb9cd132ee660376466b5ed (diff)
downloadperl-50853fa94fa95a4fad70b61c9360709826bb8093.tar.gz
Make overloaded classes inherit fallback
Before this commit, only classes that had no overloading defined could inherit the fallback from other classes. If a subclass wanted to override a single overload setting, it would have to specify the fall- back value explicitly, to avoid implying fallback=>undef. We do this by separating the fallback value from overloadedness itself, so it is possible to have a class that is overloaded, but with no fallback value. Previously, the ‘()’ stash entry was used for two purposes. It had a sub in it so it could be found using usual method lookup mechanims. The failure to find any such method would be taken for efficiency’s sake to mean that there was no overloaded, and the search for methods could end early. The scalar slot contained the fallback entry itself. To preserve the effiency, we still use &{"()"} to indicate that there is overloadedness. Fallback now uses its own entry, named ‘(fallback’. Since it has to be special-cased anyway, there is no need to add it to regen/overload.pl.
Diffstat (limited to 'lib/overload.pm')
-rw-r--r--lib/overload.pm22
1 files changed, 11 insertions, 11 deletions
diff --git a/lib/overload.pm b/lib/overload.pm
index fbb04e5c2e..f270310baa 100644
--- a/lib/overload.pm
+++ b/lib/overload.pm
@@ -32,11 +32,13 @@ sub OVERLOAD {
my %arg = @_;
my ($sub, $fb);
$ {$package . "::OVERLOAD"}{dummy}++; # Register with magic by touching.
- $fb = ${$package . "::()"}; # preserve old fallback value RT#68196
*{$package . "::()"} = \&nil; # Make it findable via fetchmethod.
for (keys %arg) {
if ($_ eq 'fallback') {
- $fb = $arg{$_};
+ for my $sym (*{$package . "::(fallback"}) {
+ *$sym = \&nil; # Make it findable via fetchmethod.
+ $$sym = $arg{$_};
+ }
} else {
warnings::warnif("overload arg '$_' is invalid")
unless $ops_seen{$_};
@@ -49,7 +51,6 @@ sub OVERLOAD {
*{$package . "::(" . $_} = \&{ $sub };
}
}
- ${$package . "::()"} = $fb; # Make it findable too (fallback only).
}
sub import {
@@ -64,11 +65,7 @@ sub unimport {
${$package . "::OVERLOAD"}{dummy}++; # Upgrade the table
shift;
for (@_) {
- if ($_ eq 'fallback') {
- undef $ {$package . "::()"};
- } else {
delete $ {$package . "::"}{"(" . $_};
- }
}
}
@@ -936,10 +933,10 @@ be called to implement operation C<+> for an object in package C<A>.
=back
-Note that since the value of the C<fallback> key is not a subroutine,
-its inheritance is not governed by the above rules. In the current
-implementation, the value of C<fallback> in the first overloaded
-ancestor is used, but this is accidental and subject to change.
+Note that in Perl version prior to 5.18 inheritance of the C<fallback> key
+was not governed by the above rules. The value of C<fallback> in the first
+overloaded ancestor was used. This was fixed in 5.18 to follow the usual
+rules of inheritance.
=head2 Run-time Overloading
@@ -1681,6 +1678,9 @@ The symbol table is filled with names looking like line-noise.
=item *
+This bug was fixed in Perl 5.18, but may still trip you up if you are using
+older versions:
+
For the purpose of inheritance every overloaded package behaves as if
C<fallback> is present (possibly undefined). This may create
interesting effects if some package is not overloaded, but inherits