package overload;
-our $VERSION = '1.17';
+our $VERSION = '1.20';
%ops = (
with_assign => "+ - * / % ** << >> x .",
$package = shift;
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.
+ *{$package . "::(("} = \&nil; # Make it findable via fetchmethod.
for (keys %arg) {
if ($_ eq 'fallback') {
- $fb = $arg{$_};
+ for my $sym (*{$package . "::()"}) {
+ *$sym = \&nil; # Make it findable via fetchmethod.
+ $$sym = $arg{$_};
+ }
} else {
warnings::warnif("overload arg '$_' is invalid")
unless $ops_seen{$_};
$sub = $arg{$_};
- if (not ref $sub and $sub !~ /::/) {
+ if (not ref $sub) {
$ {$package . "::(" . $_} = $sub;
$sub = \&nil;
}
*{$package . "::(" . $_} = \&{ $sub };
}
}
- ${$package . "::()"} = $fb; # Make it findable too (fallback only).
}
sub import {
sub unimport {
$package = (caller())[0];
- ${$package . "::OVERLOAD"}{dummy}++; # Upgrade the table
shift;
+ *{$package . "::(("} = \&nil;
for (@_) {
- if ($_ eq 'fallback') {
- undef $ {$package . "::()"};
- } else {
- delete $ {$package . "::"}{"(" . $_};
- }
+ warnings::warnif("overload arg '$_' is invalid")
+ unless $ops_seen{$_};
+ delete $ {$package . "::"}{$_ eq 'fallback' ? '()' : "(" .$_};
}
}
sub Overloaded {
my $package = shift;
$package = ref $package if ref $package;
- mycan ($package, '()');
+ mycan ($package, '()') || mycan ($package, '((');
}
sub ov_method {
}
sub AddrRef {
- my $package = ref $_[0];
- return "$_[0]" unless $package;
-
- local $@;
- local $!;
- require Scalar::Util;
- my $class = Scalar::Util::blessed($_[0]);
- my $class_prefix = defined($class) ? "$class=" : "";
- my $type = Scalar::Util::reftype($_[0]);
- my $addr = Scalar::Util::refaddr($_[0]);
- return sprintf("%s%s(0x%x)", $class_prefix, $type, $addr);
+ no overloading;
+ "$_[0]";
}
*StrVal = *AddrRef;
for both the I<read-filehandle> syntax C<E<lt>$varE<gt>> and
I<globbing> syntax C<E<lt>${var}E<gt>>.
-B<BUGS> Even in list context, the iterator is currently called only
-once and with scalar context.
-
=item * I<File tests>
The key C<'-X'> is used to specify a subroutine to handle all the
=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
and overload::remove_constant() from anywhere but import() and unimport() methods.
From these methods they may be called as
- sub import {
- shift;
- return unless @_;
- die "unknown import: @_" unless @_ == 1 and $_[0] eq ':constant';
- overload::constant integer => sub {Math::BigInt->new(shift)};
- }
+ sub import {
+ shift;
+ return unless @_;
+ die "unknown import: @_" unless @_ == 1 and $_[0] eq ':constant';
+ overload::constant integer => sub {Math::BigInt->new(shift)};
+ }
=head1 IMPLEMENTATION
The table of methods for all operations is cached in magic for the
symbol table hash for the package. The cache is invalidated during
processing of C<use overload>, C<no overload>, new function
-definitions, and changes in @ISA. However, this invalidation remains
-unprocessed until the next C<bless>ing into the package. Hence if you
-want to change overloading structure dynamically, you'll need an
-additional (fake) C<bless>ing to update the table.
+definitions, and changes in @ISA.
(Every SVish thing has a magic queue, and magic is an entry in that
queue. This is how a single variable may participate in multiple
the stashes, which are rarely used directly, thus should not slow down
Perl.)
-If an object belongs to a package using overload, it carries a special
-flag. Thus the only speed penalty during arithmetic operations without
-overloading is the checking of this flag.
-
-In fact, if C<use overload> is not present, there is almost no overhead
-for overloadable operations, so most programs should not suffer
-measurable performance penalties. A considerable effort was made to
-minimize the overhead when overload is used in some package, but the
-arguments in question do not belong to packages using overload. When
-in doubt, test your speed with C<use overload> and without it. So far
-there have been no reports of substantial speed degradation if Perl is
-compiled with optimization turned on.
-
-There is no size penalty for data if overload is not used. The only
-size penalty if overload is used in some package is that I<all> the
-packages acquire a magic during the next C<bless>ing into the
-package. This magic is three-words-long for packages without
-overloading, and carries the cache table if the package is overloaded.
+If a package uses overload, it carries a special flag. This flag is also
+set when new function are defined or @ISA is modified. There will be a
+slight speed penalty on the very first operation thereafter that supports
+overloading, while the overload tables are updated. If there is no
+overloading present, the flag is turned off. Thus the only speed penalty
+thereafter is the checking of this flag.
It is expected that arguments to methods that are not explicitly supposed
to be changed are constant (but this is not enforced).
=item *
-No warning is issued for invalid C<use overload> keys.
-Such errors are not always obvious:
-
- use overload "+0" => sub { ...; }, # should be "0+"
- "not" => sub { ...; }; # should be "!"
-
-(Bug #74098)
-
-=item *
-
A pitfall when fallback is TRUE and Perl resorts to a built-in
implementation of an operator is that some operators have more
than one semantic, for example C<|>:
=item *
-Because it is used for overloading, the per-package hash
-C<%OVERLOAD> now has a special meaning in Perl.
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