Add a section on how to submit a patch
[p5sagit/p5-mst-13.2.git] / lib / Exporter / Heavy.pm
CommitLineData
b75c8c73 1package Exporter::Heavy;
2
3use strict;
4no strict 'refs';
5
6# On one line so MakeMaker will see it.
7require Exporter; our $VERSION = $Exporter::VERSION;
3e927c50 8
9# Carp 1.05+ does this now for us, but we may be running with an old Carp
10$Carp::Internal{'Exporter::Heavy'}++;
4af1b167 11
ca24dfc6 12=head1 NAME
13
14Exporter::Heavy - Exporter guts
15
fe854a6f 16=head1 SYNOPSIS
ca24dfc6 17
18(internal use only)
19
20=head1 DESCRIPTION
21
22No user-serviceable parts inside.
3cb6de81 23
ca24dfc6 24=cut
4ac9195f 25
4af1b167 26#
27# We go to a lot of trouble not to 'require Carp' at file scope,
28# because Carp requires Exporter, and something has to give.
29#
30
364e1267 31sub _rebuild_cache {
32 my ($pkg, $exports, $cache) = @_;
33 s/^&// foreach @$exports;
34 @{$cache}{@$exports} = (1) x @$exports;
35 my $ok = \@{"${pkg}::EXPORT_OK"};
36 if (@$ok) {
37 s/^&// foreach @$ok;
38 @{$cache}{@$ok} = (1) x @$ok;
39 }
40}
41
4af1b167 42sub heavy_export {
43
44 # First make import warnings look like they're coming from the "use".
45 local $SIG{__WARN__} = sub {
46 my $text = shift;
47 if ($text =~ s/ at \S*Exporter\S*.pm line \d+.*\n//) {
48 require Carp;
49 local $Carp::CarpLevel = 1; # ignore package calling us too.
50 Carp::carp($text);
51 }
52 else {
53 warn $text;
54 }
55 };
56 local $SIG{__DIE__} = sub {
57 require Carp;
58 local $Carp::CarpLevel = 1; # ignore package calling us too.
59 Carp::croak("$_[0]Illegal null symbol in \@${1}::EXPORT")
60 if $_[0] =~ /^Unable to create sub named "(.*?)::"/;
61 };
62
63 my($pkg, $callpkg, @imports) = @_;
364e1267 64 my($type, $sym, $cache_is_current, $oops);
b75c8c73 65 my($exports, $export_cache) = (\@{"${pkg}::EXPORT"},
a6faae8d 66 $Exporter::Cache{$pkg} ||= {});
4af1b167 67
68 if (@imports) {
b75c8c73 69 if (!%$export_cache) {
364e1267 70 _rebuild_cache ($pkg, $exports, $export_cache);
71 $cache_is_current = 1;
4af1b167 72 }
73
a2bca5a3 74 if (grep m{^[/!:]}, @imports) {
4af1b167 75 my $tagsref = \%{"${pkg}::EXPORT_TAGS"};
76 my $tagdata;
77 my %imports;
78 my($remove, $spec, @names, @allexports);
79 # negated first item implies starting with default set:
80 unshift @imports, ':DEFAULT' if $imports[0] =~ m/^!/;
81 foreach $spec (@imports){
82 $remove = $spec =~ s/^!//;
83
84 if ($spec =~ s/^://){
85 if ($spec eq 'DEFAULT'){
b75c8c73 86 @names = @$exports;
4af1b167 87 }
88 elsif ($tagdata = $tagsref->{$spec}) {
89 @names = @$tagdata;
90 }
91 else {
92 warn qq["$spec" is not defined in %${pkg}::EXPORT_TAGS];
93 ++$oops;
94 next;
95 }
96 }
97 elsif ($spec =~ m:^/(.*)/$:){
98 my $patn = $1;
b75c8c73 99 @allexports = keys %$export_cache unless @allexports; # only do keys once
4af1b167 100 @names = grep(/$patn/, @allexports); # not anchored by default
101 }
102 else {
103 @names = ($spec); # is a normal symbol name
104 }
105
106 warn "Import ".($remove ? "del":"add").": @names "
e336476d 107 if $Exporter::Verbose;
4af1b167 108
109 if ($remove) {
110 foreach $sym (@names) { delete $imports{$sym} }
111 }
112 else {
113 @imports{@names} = (1) x @names;
114 }
115 }
116 @imports = keys %imports;
117 }
118
ddc53c8b 119 my @carp;
4af1b167 120 foreach $sym (@imports) {
b75c8c73 121 if (!$export_cache->{$sym}) {
4af1b167 122 if ($sym =~ m/^\d/) {
22b4675c 123 $pkg->VERSION($sym); # inherit from UNIVERSAL
4af1b167 124 # If the version number was the only thing specified
125 # then we should act as if nothing was specified:
126 if (@imports == 1) {
b75c8c73 127 @imports = @$exports;
4af1b167 128 last;
129 }
130 # We need a way to emulate 'use Foo ()' but still
131 # allow an easy version check: "use Foo 1.23, ''";
132 if (@imports == 2 and !$imports[1]) {
133 @imports = ();
134 last;
135 }
b75c8c73 136 } elsif ($sym !~ s/^&// || !$export_cache->{$sym}) {
364e1267 137 # Last chance - see if they've updated EXPORT_OK since we
138 # cached it.
139
140 unless ($cache_is_current) {
141 %$export_cache = ();
142 _rebuild_cache ($pkg, $exports, $export_cache);
143 $cache_is_current = 1;
144 }
145
146 if (!$export_cache->{$sym}) {
147 # accumulate the non-exports
148 push @carp,
149 qq["$sym" is not exported by the $pkg module\n];
150 $oops++;
151 }
4af1b167 152 }
153 }
154 }
155 if ($oops) {
156 require Carp;
ddc53c8b 157 Carp::croak("@{carp}Can't continue after import errors");
4af1b167 158 }
159 }
160 else {
b75c8c73 161 @imports = @$exports;
4af1b167 162 }
163
b75c8c73 164 my($fail, $fail_cache) = (\@{"${pkg}::EXPORT_FAIL"},
a6faae8d 165 $Exporter::FailCache{$pkg} ||= {});
b75c8c73 166
167 if (@$fail) {
168 if (!%$fail_cache) {
4af1b167 169 # Build cache of symbols. Optimise the lookup by adding
170 # barewords twice... both with and without a leading &.
b75c8c73 171 # (Technique could be applied to $export_cache at cost of memory)
172 my @expanded = map { /^\w/ ? ($_, '&'.$_) : $_ } @$fail;
e336476d 173 warn "${pkg}::EXPORT_FAIL cached: @expanded" if $Exporter::Verbose;
b75c8c73 174 @{$fail_cache}{@expanded} = (1) x @expanded;
4af1b167 175 }
176 my @failed;
b75c8c73 177 foreach $sym (@imports) { push(@failed, $sym) if $fail_cache->{$sym} }
4af1b167 178 if (@failed) {
179 @failed = $pkg->export_fail(@failed);
180 foreach $sym (@failed) {
181 require Carp;
182 Carp::carp(qq["$sym" is not implemented by the $pkg module ],
183 "on this architecture");
184 }
185 if (@failed) {
186 require Carp;
187 Carp::croak("Can't continue after import errors");
188 }
189 }
190 }
191
192 warn "Importing into $callpkg from $pkg: ",
e336476d 193 join(", ",sort @imports) if $Exporter::Verbose;
4af1b167 194
195 foreach $sym (@imports) {
196 # shortcut for the common case of no type character
197 (*{"${callpkg}::$sym"} = \&{"${pkg}::$sym"}, next)
198 unless $sym =~ s/^(\W)//;
199 $type = $1;
452617c3 200 no warnings 'once';
4af1b167 201 *{"${callpkg}::$sym"} =
202 $type eq '&' ? \&{"${pkg}::$sym"} :
203 $type eq '$' ? \${"${pkg}::$sym"} :
204 $type eq '@' ? \@{"${pkg}::$sym"} :
205 $type eq '%' ? \%{"${pkg}::$sym"} :
206 $type eq '*' ? *{"${pkg}::$sym"} :
207 do { require Carp; Carp::croak("Can't export symbol: $type$sym") };
208 }
209}
210
211sub heavy_export_to_level
212{
213 my $pkg = shift;
214 my $level = shift;
31a63159 215 (undef) = shift; # XXX redundant arg
4af1b167 216 my $callpkg = caller($level);
217 $pkg->export($callpkg, @_);
218}
219
220# Utility functions
221
222sub _push_tags {
223 my($pkg, $var, $syms) = @_;
b75c8c73 224 my @nontag = ();
225 my $export_tags = \%{"${pkg}::EXPORT_TAGS"};
4af1b167 226 push(@{"${pkg}::$var"},
b75c8c73 227 map { $export_tags->{$_} ? @{$export_tags->{$_}}
228 : scalar(push(@nontag,$_),$_) }
229 (@$syms) ? @$syms : keys %$export_tags);
230 if (@nontag and $^W) {
4af1b167 231 # This may change to a die one day
232 require Carp;
b75c8c73 233 Carp::carp(join(", ", @nontag)." are not tags of $pkg");
4af1b167 234 }
235}
236
0e57b4e8 237sub heavy_require_version {
4af1b167 238 my($self, $wanted) = @_;
239 my $pkg = ref $self || $self;
22b4675c 240 return ${pkg}->VERSION($wanted);
4af1b167 241}
242
0e57b4e8 243sub heavy_export_tags {
244 _push_tags((caller)[0], "EXPORT", \@_);
245}
246
247sub heavy_export_ok_tags {
248 _push_tags((caller)[0], "EXPORT_OK", \@_);
249}
250
4af1b167 2511;