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