1 package Exporter::Heavy;
6 # On one line so MakeMaker will see it.
7 require Exporter; our $VERSION = $Exporter::VERSION;
11 Exporter::Heavy - Exporter guts
19 No user-serviceable parts inside.
24 # We go to a lot of trouble not to 'require Carp' at file scope,
25 # because Carp requires Exporter, and something has to give.
29 my ($pkg, $exports, $cache) = @_;
30 s/^&// foreach @$exports;
31 @{$cache}{@$exports} = (1) x @$exports;
32 my $ok = \@{"${pkg}::EXPORT_OK"};
35 @{$cache}{@$ok} = (1) x @$ok;
41 # First make import warnings look like they're coming from the "use".
42 local $SIG{__WARN__} = sub {
44 if ($text =~ s/ at \S*Exporter\S*.pm line \d+.*\n//) {
46 local $Carp::CarpLevel = 1; # ignore package calling us too.
53 local $SIG{__DIE__} = sub {
55 local $Carp::CarpLevel = 1; # ignore package calling us too.
56 Carp::croak("$_[0]Illegal null symbol in \@${1}::EXPORT")
57 if $_[0] =~ /^Unable to create sub named "(.*?)::"/;
60 my($pkg, $callpkg, @imports) = @_;
61 my($type, $sym, $cache_is_current, $oops);
62 my($exports, $export_cache) = (\@{"${pkg}::EXPORT"},
63 $Exporter::Cache{$pkg} ||= {});
66 if (!%$export_cache) {
67 _rebuild_cache ($pkg, $exports, $export_cache);
68 $cache_is_current = 1;
71 if (grep m{^[/!:]}, @imports) {
72 my $tagsref = \%{"${pkg}::EXPORT_TAGS"};
75 my($remove, $spec, @names, @allexports);
76 # negated first item implies starting with default set:
77 unshift @imports, ':DEFAULT' if $imports[0] =~ m/^!/;
78 foreach $spec (@imports){
79 $remove = $spec =~ s/^!//;
82 if ($spec eq 'DEFAULT'){
85 elsif ($tagdata = $tagsref->{$spec}) {
89 warn qq["$spec" is not defined in %${pkg}::EXPORT_TAGS];
94 elsif ($spec =~ m:^/(.*)/$:){
96 @allexports = keys %$export_cache unless @allexports; # only do keys once
97 @names = grep(/$patn/, @allexports); # not anchored by default
100 @names = ($spec); # is a normal symbol name
103 warn "Import ".($remove ? "del":"add").": @names "
104 if $Exporter::Verbose;
107 foreach $sym (@names) { delete $imports{$sym} }
110 @imports{@names} = (1) x @names;
113 @imports = keys %imports;
117 foreach $sym (@imports) {
118 if (!$export_cache->{$sym}) {
119 if ($sym =~ m/^\d/) {
120 $pkg->VERSION($sym); # inherit from UNIVERSAL
121 # If the version number was the only thing specified
122 # then we should act as if nothing was specified:
124 @imports = @$exports;
127 # We need a way to emulate 'use Foo ()' but still
128 # allow an easy version check: "use Foo 1.23, ''";
129 if (@imports == 2 and !$imports[1]) {
133 } elsif ($sym !~ s/^&// || !$export_cache->{$sym}) {
134 # Last chance - see if they've updated EXPORT_OK since we
137 unless ($cache_is_current) {
139 _rebuild_cache ($pkg, $exports, $export_cache);
140 $cache_is_current = 1;
143 if (!$export_cache->{$sym}) {
144 # accumulate the non-exports
146 qq["$sym" is not exported by the $pkg module\n];
154 Carp::croak("@{carp}Can't continue after import errors");
158 @imports = @$exports;
161 my($fail, $fail_cache) = (\@{"${pkg}::EXPORT_FAIL"},
162 $Exporter::FailCache{$pkg} ||= {});
166 # Build cache of symbols. Optimise the lookup by adding
167 # barewords twice... both with and without a leading &.
168 # (Technique could be applied to $export_cache at cost of memory)
169 my @expanded = map { /^\w/ ? ($_, '&'.$_) : $_ } @$fail;
170 warn "${pkg}::EXPORT_FAIL cached: @expanded" if $Exporter::Verbose;
171 @{$fail_cache}{@expanded} = (1) x @expanded;
174 foreach $sym (@imports) { push(@failed, $sym) if $fail_cache->{$sym} }
176 @failed = $pkg->export_fail(@failed);
177 foreach $sym (@failed) {
179 Carp::carp(qq["$sym" is not implemented by the $pkg module ],
180 "on this architecture");
184 Carp::croak("Can't continue after import errors");
189 warn "Importing into $callpkg from $pkg: ",
190 join(", ",sort @imports) if $Exporter::Verbose;
192 foreach $sym (@imports) {
193 # shortcut for the common case of no type character
194 (*{"${callpkg}::$sym"} = \&{"${pkg}::$sym"}, next)
195 unless $sym =~ s/^(\W)//;
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") };
208 sub heavy_export_to_level
212 (undef) = shift; # XXX redundant arg
213 my $callpkg = caller($level);
214 $pkg->export($callpkg, @_);
220 my($pkg, $var, $syms) = @_;
222 my $export_tags = \%{"${pkg}::EXPORT_TAGS"};
223 push(@{"${pkg}::$var"},
224 map { $export_tags->{$_} ? @{$export_tags->{$_}}
225 : scalar(push(@nontag,$_),$_) }
226 (@$syms) ? @$syms : keys %$export_tags);
227 if (@nontag and $^W) {
228 # This may change to a die one day
230 Carp::carp(join(", ", @nontag)." are not tags of $pkg");
234 sub heavy_require_version {
235 my($self, $wanted) = @_;
236 my $pkg = ref $self || $self;
237 return ${pkg}->VERSION($wanted);
240 sub heavy_export_tags {
241 _push_tags((caller)[0], "EXPORT", \@_);
244 sub heavy_export_ok_tags {
245 _push_tags((caller)[0], "EXPORT_OK", \@_);