1 package Exporter::Heavy;
6 # On one line so MakeMaker will see it.
7 require Exporter; our $VERSION = $Exporter::VERSION;
8 # Carp does this now for us, so we can finally live w/o Carp
9 #$Carp::Internal{"Exporter::Heavy"} = 1;
13 Exporter::Heavy - Exporter guts
21 No user-serviceable parts inside.
26 # We go to a lot of trouble not to 'require Carp' at file scope,
27 # because Carp requires Exporter, and something has to give.
31 my ($pkg, $exports, $cache) = @_;
32 s/^&// foreach @$exports;
33 @{$cache}{@$exports} = (1) x @$exports;
34 my $ok = \@{"${pkg}::EXPORT_OK"};
37 @{$cache}{@$ok} = (1) x @$ok;
43 # First make import warnings look like they're coming from the "use".
44 local $SIG{__WARN__} = sub {
46 if ($text =~ s/ at \S*Exporter\S*.pm line \d+.*\n//) {
48 local $Carp::CarpLevel = 1; # ignore package calling us too.
55 local $SIG{__DIE__} = sub {
57 local $Carp::CarpLevel = 1; # ignore package calling us too.
58 Carp::croak("$_[0]Illegal null symbol in \@${1}::EXPORT")
59 if $_[0] =~ /^Unable to create sub named "(.*?)::"/;
62 my($pkg, $callpkg, @imports) = @_;
63 my($type, $sym, $cache_is_current, $oops);
64 my($exports, $export_cache) = (\@{"${pkg}::EXPORT"},
65 $Exporter::Cache{$pkg} ||= {});
68 if (!%$export_cache) {
69 _rebuild_cache ($pkg, $exports, $export_cache);
70 $cache_is_current = 1;
73 if (grep m{^[/!:]}, @imports) {
74 my $tagsref = \%{"${pkg}::EXPORT_TAGS"};
77 my($remove, $spec, @names, @allexports);
78 # negated first item implies starting with default set:
79 unshift @imports, ':DEFAULT' if $imports[0] =~ m/^!/;
80 foreach $spec (@imports){
81 $remove = $spec =~ s/^!//;
84 if ($spec eq 'DEFAULT'){
87 elsif ($tagdata = $tagsref->{$spec}) {
91 warn qq["$spec" is not defined in %${pkg}::EXPORT_TAGS];
96 elsif ($spec =~ m:^/(.*)/$:){
98 @allexports = keys %$export_cache unless @allexports; # only do keys once
99 @names = grep(/$patn/, @allexports); # not anchored by default
102 @names = ($spec); # is a normal symbol name
105 warn "Import ".($remove ? "del":"add").": @names "
106 if $Exporter::Verbose;
109 foreach $sym (@names) { delete $imports{$sym} }
112 @imports{@names} = (1) x @names;
115 @imports = keys %imports;
119 foreach $sym (@imports) {
120 if (!$export_cache->{$sym}) {
121 if ($sym =~ m/^\d/) {
122 $pkg->VERSION($sym); # inherit from UNIVERSAL
123 # If the version number was the only thing specified
124 # then we should act as if nothing was specified:
126 @imports = @$exports;
129 # We need a way to emulate 'use Foo ()' but still
130 # allow an easy version check: "use Foo 1.23, ''";
131 if (@imports == 2 and !$imports[1]) {
135 } elsif ($sym !~ s/^&// || !$export_cache->{$sym}) {
136 # Last chance - see if they've updated EXPORT_OK since we
139 unless ($cache_is_current) {
141 _rebuild_cache ($pkg, $exports, $export_cache);
142 $cache_is_current = 1;
145 if (!$export_cache->{$sym}) {
146 # accumulate the non-exports
148 qq["$sym" is not exported by the $pkg module\n];
156 Carp::croak("@{carp}Can't continue after import errors");
160 @imports = @$exports;
163 my($fail, $fail_cache) = (\@{"${pkg}::EXPORT_FAIL"},
164 $Exporter::FailCache{$pkg} ||= {});
168 # Build cache of symbols. Optimise the lookup by adding
169 # barewords twice... both with and without a leading &.
170 # (Technique could be applied to $export_cache at cost of memory)
171 my @expanded = map { /^\w/ ? ($_, '&'.$_) : $_ } @$fail;
172 warn "${pkg}::EXPORT_FAIL cached: @expanded" if $Exporter::Verbose;
173 @{$fail_cache}{@expanded} = (1) x @expanded;
176 foreach $sym (@imports) { push(@failed, $sym) if $fail_cache->{$sym} }
178 @failed = $pkg->export_fail(@failed);
179 foreach $sym (@failed) {
181 Carp::carp(qq["$sym" is not implemented by the $pkg module ],
182 "on this architecture");
186 Carp::croak("Can't continue after import errors");
191 warn "Importing into $callpkg from $pkg: ",
192 join(", ",sort @imports) if $Exporter::Verbose;
194 foreach $sym (@imports) {
195 # shortcut for the common case of no type character
196 (*{"${callpkg}::$sym"} = \&{"${pkg}::$sym"}, next)
197 unless $sym =~ s/^(\W)//;
200 *{"${callpkg}::$sym"} =
201 $type eq '&' ? \&{"${pkg}::$sym"} :
202 $type eq '$' ? \${"${pkg}::$sym"} :
203 $type eq '@' ? \@{"${pkg}::$sym"} :
204 $type eq '%' ? \%{"${pkg}::$sym"} :
205 $type eq '*' ? *{"${pkg}::$sym"} :
206 do { require Carp; Carp::croak("Can't export symbol: $type$sym") };
210 sub heavy_export_to_level
214 (undef) = shift; # XXX redundant arg
215 my $callpkg = caller($level);
216 $pkg->export($callpkg, @_);
222 my($pkg, $var, $syms) = @_;
224 my $export_tags = \%{"${pkg}::EXPORT_TAGS"};
225 push(@{"${pkg}::$var"},
226 map { $export_tags->{$_} ? @{$export_tags->{$_}}
227 : scalar(push(@nontag,$_),$_) }
228 (@$syms) ? @$syms : keys %$export_tags);
229 if (@nontag and $^W) {
230 # This may change to a die one day
232 Carp::carp(join(", ", @nontag)." are not tags of $pkg");
236 sub heavy_require_version {
237 my($self, $wanted) = @_;
238 my $pkg = ref $self || $self;
239 return ${pkg}->VERSION($wanted);
242 sub heavy_export_tags {
243 _push_tags((caller)[0], "EXPORT", \@_);
246 sub heavy_export_ok_tags {
247 _push_tags((caller)[0], "EXPORT_OK", \@_);