summaryrefslogtreecommitdiff
path: root/lib/overload.pm
diff options
context:
space:
mode:
authorPerl 5 Porters <perl5-porters@africa.nicoh.com>1997-01-01 08:59:00 +1200
committerChip Salzenberg <chip@atlantic.net>1997-01-01 08:59:00 +1200
commita60067777be62ee91d1318f9ae26d9ed713245de (patch)
tree9e312a824c6ef40aa10dd0e60451fd737098a965 /lib/overload.pm
parenta034a98d8bfd0fd904012bd5227ce209aaaa0b26 (diff)
downloadperl-a60067777be62ee91d1318f9ae26d9ed713245de.tar.gz
[inseparable changes from patch from perl5.003_17 to perl5.003_18]
CORE LANGUAGE CHANGES Subject: Inherited overloading Date: Sun, 29 Dec 1996 08:12:54 -0500 (EST) From: Ilya Zakharevich <ilya@math.ohio-state.edu> Files: gv.c lib/overload.pm perl.h sv.c sv.h t/op/overload.t Chip Salzenberg writes: > > Patch now, tarchive later: Below is the fixed overloading patch. Note that in between AMG_names got const on it (a good thing!), but as a corollary I needed to cast away const-ness to actually use it (since, say, newSVpv does not have const args). Enjoy, p5p-msgid: <199612291312.IAA02134@monk.mps.ohio-state.edu> Subject: Closures at file scope must be anonymous From: Chip Salzenberg <chip@atlantic.net> Files: op.c Subject: Warn on '{if,while} ($x = X)' where X is glob, readdir, or <FH> From: Chip Salzenberg <chip@atlantic.net> Files: op.c pod/perldiag.pod DOCUMENTATION Subject: Re: perldiag.pod entry for "Scalar value @%s{%s} ..." Date: Tue, 31 Dec 1996 11:50:19 -0500 From: Roderick Schertler <roderick@gate.net> Files: pod/perldiag.pod Msg-ID: <2043.852051019@eeyore.ibcinc.com> (applied based on p5p patch as commit c885792efecf3f527b3b5099727cc16b03eee1dc) OTHER CORE CHANGES Subject: Get rid of 'Leaked scalars' From: Chip Salzenberg <chip@atlantic.net> Files: cop.h gv.c op.c TESTS Subject: Expanded locale.t and misc.t From: Jarkko Hietaniemi <jhi@cc.hut.fi> Files: t/lib/locale.t t/lib/misc.t Subject: Expanded my.t From: Chip Salzenberg <chip@atlantic.net> Files: t/lib/my.t
Diffstat (limited to 'lib/overload.pm')
-rw-r--r--lib/overload.pm62
1 files changed, 43 insertions, 19 deletions
diff --git a/lib/overload.pm b/lib/overload.pm
index 20411ea576..ec874ec8d7 100644
--- a/lib/overload.pm
+++ b/lib/overload.pm
@@ -1,12 +1,26 @@
package overload;
+sub nil {}
+
sub OVERLOAD {
$package = shift;
my %arg = @_;
- my $hash = \%{$package . "::OVERLOAD"};
+ my ($sub, $fb);
+ $ {$package . "::OVERLOAD"}{dummy}++; # Register with magic by touching.
+ *{$package . "::()"} = \&nil; # Make it findable via fetchmethod.
for (keys %arg) {
- $hash->{$_} = $arg{$_};
+ if ($_ eq 'fallback') {
+ $fb = $arg{$_};
+ } else {
+ $sub = $arg{$_};
+ if (not ref $sub and $sub !~ /::/) {
+ $sub = "${'package'}::$sub";
+ }
+ #print STDERR "Setting `$ {'package'}::\cO$_' to \\&`$sub'.\n";
+ *{$package . "::(" . $_} = \&{ $sub };
+ }
}
+ ${$package . "::()"} = $fb; # Make it findable too (fallback only).
}
sub import {
@@ -18,41 +32,47 @@ sub import {
sub unimport {
$package = (caller())[0];
- my $hash = \%{$package . "::OVERLOAD"};
+ ${$package . "::OVERLOAD"}{dummy}++; # Upgrade the table
shift;
for (@_) {
- delete $hash->{$_};
+ if ($_ eq 'fallback') {
+ undef $ {$package . "::()"};
+ } else {
+ delete $ {$package . "::"}{"(" . $_};
+ }
}
}
sub Overloaded {
- ($package = ref $_[0]) and defined %{$package . "::OVERLOAD"};
+ my $package = shift;
+ $package = ref $package if ref $package;
+ $package->can('()');
}
sub OverloadedStringify {
- ($package = ref $_[0]) and
- defined %{$package . "::OVERLOAD"} and
- exists $ {$package . "::OVERLOAD"}{'""'} and
- defined &{$ {$package . "::OVERLOAD"}{'""'}};
+ my $package = shift;
+ $package = ref $package if ref $package;
+ $package->can('(""')
}
sub Method {
- ($package = ref $_[0]) and
- defined %{$package . "::OVERLOAD"} and
- $ {$package . "::OVERLOAD"}{$_[1]};
+ my $package = shift;
+ $package = ref $package if ref $package;
+ $package->can('(' . shift)
}
sub AddrRef {
- $package = ref $_[0];
- bless $_[0], Overload::Fake; # Non-overloaded package
+ my $package = ref $_[0];
+ return "$_[0]" unless $package;
+ bless $_[0], overload::Fake; # Non-overloaded package
my $str = "$_[0]";
bless $_[0], $package; # Back
- $str;
+ $package . substr $str, index $str, '=';
}
sub StrVal {
- (OverloadedStringify) ?
- (AddrRef) :
+ (OverloadedStringify($_[0])) ?
+ (AddrRef(shift)) :
"$_[0]";
}
@@ -486,9 +506,13 @@ induces diagnostic messages.
=head1 BUGS
Because it is used for overloading, the per-package associative array
-%OVERLOAD now has a special meaning in Perl.
+%OVERLOAD now has a special meaning in Perl. The symbol table is
+filled with names looking like line-noise.
-As shipped, mathemagical properties are not inherited via the @ISA tree.
+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
+from two overloaded packages.
This document is confusing.