1 package Exporter::Heavy;
6 # On one line so MakeMaker will see it.
7 require Exporter; our $VERSION = $Exporter::VERSION;
9 # Carp 1.05+ does this now for us, but we may be running with an old Carp
10 $Carp::Internal{'Exporter::Heavy'}++;
14 Exporter::Heavy - Exporter guts
22 No user-serviceable parts inside.
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.
32 my ($pkg, $exports, $cache) = @_;
33 s/^&// foreach @$exports;
34 @{$cache}{@$exports} = (1) x @$exports;
35 my $ok = \@{"${pkg}::EXPORT_OK"};
38 @{$cache}{@$ok} = (1) x @$ok;
44 # First make import warnings look like they're coming from the "use".
45 local $SIG{__WARN__} = sub {
47 if ($text =~ s/ at \S*Exporter\S*.pm line \d+.*\n//) {
49 local $Carp::CarpLevel = 1; # ignore package calling us too.
56 local $SIG{__DIE__} = sub {
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 "(.*?)::"/;
63 my($pkg, $callpkg, @imports) = @_;
64 my($type, $sym, $cache_is_current, $oops);
65 my($exports, $export_cache) = (\@{"${pkg}::EXPORT"},
66 $Exporter::Cache{$pkg} ||= {});
69 if (!%$export_cache) {
70 _rebuild_cache ($pkg, $exports, $export_cache);
71 $cache_is_current = 1;
74 if (grep m{^[/!:]}, @imports) {
75 my $tagsref = \%{"${pkg}::EXPORT_TAGS"};
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/^!//;
85 if ($spec eq 'DEFAULT'){
88 elsif ($tagdata = $tagsref->{$spec}) {
92 warn qq["$spec" is not defined in %${pkg}::EXPORT_TAGS];
97 elsif ($spec =~ m:^/(.*)/$:){
99 @allexports = keys %$export_cache unless @allexports; # only do keys once
100 @names = grep(/$patn/, @allexports); # not anchored by default
103 @names = ($spec); # is a normal symbol name
106 warn "Import ".($remove ? "del":"add").": @names "
107 if $Exporter::Verbose;
110 foreach $sym (@names) { delete $imports{$sym} }
113 @imports{@names} = (1) x @names;
116 @imports = keys %imports;
120 foreach $sym (@imports) {
121 if (!$export_cache->{$sym}) {
122 if ($sym =~ m/^\d/) {
123 $pkg->VERSION($sym); # inherit from UNIVERSAL
124 # If the version number was the only thing specified
125 # then we should act as if nothing was specified:
127 @imports = @$exports;
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]) {
136 } elsif ($sym !~ s/^&// || !$export_cache->{$sym}) {
137 # Last chance - see if they've updated EXPORT_OK since we
140 unless ($cache_is_current) {
142 _rebuild_cache ($pkg, $exports, $export_cache);
143 $cache_is_current = 1;
146 if (!$export_cache->{$sym}) {
147 # accumulate the non-exports
149 qq["$sym" is not exported by the $pkg module\n];
157 Carp::croak("@{carp}Can't continue after import errors");
161 @imports = @$exports;
164 my($fail, $fail_cache) = (\@{"${pkg}::EXPORT_FAIL"},
165 $Exporter::FailCache{$pkg} ||= {});
169 # Build cache of symbols. Optimise the lookup by adding
170 # barewords twice... both with and without a leading &.
171 # (Technique could be applied to $export_cache at cost of memory)
172 my @expanded = map { /^\w/ ? ($_, '&'.$_) : $_ } @$fail;
173 warn "${pkg}::EXPORT_FAIL cached: @expanded" if $Exporter::Verbose;
174 @{$fail_cache}{@expanded} = (1) x @expanded;
177 foreach $sym (@imports) { push(@failed, $sym) if $fail_cache->{$sym} }
179 @failed = $pkg->export_fail(@failed);
180 foreach $sym (@failed) {
182 Carp::carp(qq["$sym" is not implemented by the $pkg module ],
183 "on this architecture");
187 Carp::croak("Can't continue after import errors");
192 warn "Importing into $callpkg from $pkg: ",
193 join(", ",sort @imports) if $Exporter::Verbose;
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)//;
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") };
211 sub heavy_export_to_level
215 (undef) = shift; # XXX redundant arg
216 my $callpkg = caller($level);
217 $pkg->export($callpkg, @_);
223 my($pkg, $var, $syms) = @_;
225 my $export_tags = \%{"${pkg}::EXPORT_TAGS"};
226 push(@{"${pkg}::$var"},
227 map { $export_tags->{$_} ? @{$export_tags->{$_}}
228 : scalar(push(@nontag,$_),$_) }
229 (@$syms) ? @$syms : keys %$export_tags);
230 if (@nontag and $^W) {
231 # This may change to a die one day
233 Carp::carp(join(", ", @nontag)." are not tags of $pkg");
237 sub heavy_require_version {
238 my($self, $wanted) = @_;
239 my $pkg = ref $self || $self;
240 return ${pkg}->VERSION($wanted);
243 sub heavy_export_tags {
244 _push_tags((caller)[0], "EXPORT", \@_);
247 sub heavy_export_ok_tags {
248 _push_tags((caller)[0], "EXPORT_OK", \@_);