blob: e84708d65663f0a377fd89a4919a0ead507cdbf8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
package overloading;
use warnings;
use Carp ();
our $VERSION = '0.01';
my $HINT_NO_AMAGIC = 0x01000000; # see perl.h
require 5.011000;
sub _ops_to_nums {
require overload::numbers;
map { exists $overload::numbers::names{"($_"}
? $overload::numbers::names{"($_"}
: Carp::croak("'$_' is not a valid overload")
} @_;
}
sub import {
my ( $class, @ops ) = @_;
if ( @ops ) {
if ( $^H{overloading} ) {
vec($^H{overloading} , $_, 1) = 0 for _ops_to_nums(@ops);
}
if ( $^H{overloading} !~ /[^\0]/ ) {
delete $^H{overloading};
$^H &= ~$HINT_NO_AMAGIC;
}
} else {
delete $^H{overloading};
$^H &= ~$HINT_NO_AMAGIC;
}
}
sub unimport {
my ( $class, @ops ) = @_;
if ( exists $^H{overloading} or not $^H & $HINT_NO_AMAGIC ) {
if ( @ops ) {
vec($^H{overloading} ||= '', $_, 1) = 1 for _ops_to_nums(@ops);
} else {
delete $^H{overloading};
}
}
$^H |= $HINT_NO_AMAGIC;
}
1;
__END__
=head1 NAME
overloading - perl pragma to lexically control overloading
=head1 SYNOPSIS
{
no overloading;
my $str = "$object"; # doesn't call stringification overload
}
# it's lexical, so this stringifies:
warn "$object";
# it can be enabled per op
no overloading qw("");
warn "$object";
# and also reenabled
use overloading;
=head1 DESCRIPTION
This pragma allows you to lexically disable or enable overloading.
=over 6
=item C<no overloading>
Disables overloading entirely in the current lexical scope.
=item C<no overloading @ops>
Disables only specific overloads in the current lexical scope.
=item C<use overloading>
Reenables overloading in the current lexical scope.
=item C<use overloading @ops>
Reenables overloading only for specific ops in the current lexical scope.
=back
=cut
|