source: trunk/essentials/dev-lang/perl/lib/overload.pm@ 3367

Last change on this file since 3367 was 3181, checked in by bird, 19 years ago

perl 5.8.8

File size: 44.9 KB
Line 
1package overload;
2
3our $VERSION = '1.04';
4
5$overload::hint_bits = 0x20000; # HINT_LOCALIZE_HH
6
7sub nil {}
8
9sub OVERLOAD {
10 $package = shift;
11 my %arg = @_;
12 my ($sub, $fb);
13 $ {$package . "::OVERLOAD"}{dummy}++; # Register with magic by touching.
14 *{$package . "::()"} = \&nil; # Make it findable via fetchmethod.
15 for (keys %arg) {
16 if ($_ eq 'fallback') {
17 $fb = $arg{$_};
18 } else {
19 $sub = $arg{$_};
20 if (not ref $sub and $sub !~ /::/) {
21 $ {$package . "::(" . $_} = $sub;
22 $sub = \&nil;
23 }
24 #print STDERR "Setting `$ {'package'}::\cO$_' to \\&`$sub'.\n";
25 *{$package . "::(" . $_} = \&{ $sub };
26 }
27 }
28 ${$package . "::()"} = $fb; # Make it findable too (fallback only).
29}
30
31sub import {
32 $package = (caller())[0];
33 # *{$package . "::OVERLOAD"} = \&OVERLOAD;
34 shift;
35 $package->overload::OVERLOAD(@_);
36}
37
38sub unimport {
39 $package = (caller())[0];
40 ${$package . "::OVERLOAD"}{dummy}++; # Upgrade the table
41 shift;
42 for (@_) {
43 if ($_ eq 'fallback') {
44 undef $ {$package . "::()"};
45 } else {
46 delete $ {$package . "::"}{"(" . $_};
47 }
48 }
49}
50
51sub Overloaded {
52 my $package = shift;
53 $package = ref $package if ref $package;
54 $package->can('()');
55}
56
57sub ov_method {
58 my $globref = shift;
59 return undef unless $globref;
60 my $sub = \&{*$globref};
61 return $sub if $sub ne \&nil;