1 package Exporter::Heavy;
6 # On one line so MakeMaker will see it.
7 require Exporter; our $VERSION = $Exporter::VERSION;
8 $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.
32 # First make import warnings look like they're coming from the "use".
33 local $SIG{__WARN__} = sub {
35 if ($text =~ s/ at \S*Exporter\S*.pm line \d+.*\n//) {
37 local $Carp::CarpLevel = 1; # ignore package calling us too.
44 local $SIG{__DIE__} = sub {
46 local $Carp::CarpLevel = 1; # ignore package calling us too.
47 Carp::croak("$_[0]Illegal null symbol in \@${1}::EXPORT")
48 if $_[0] =~ /^Unable to create sub named "(.*?)::"/;
51 my($pkg, $callpkg, @imports) = @_;
52 my($type, $sym, $oops);
53 my($exports, $export_cache) = (\@{"${pkg}::EXPORT"},
54 \%{"${pkg}::EXPORT"});
57 if (!%$export_cache) {
58 s/^&// foreach @$exports;
59 @{$export_cache}{@$exports} = (1) x @$exports;
60 my $ok = \@{"${pkg}::EXPORT_OK"};
63 @{$export_cache}{@$ok} = (1) x @$ok;
67 if ($imports[0] =~ m#^[/!:]#){
68 my $tagsref = \%{"${pkg}::EXPORT_TAGS"};
71 my($remove, $spec, @names, @allexports);
72 # negated first item implies starting with default set:
73 unshift @imports, ':DEFAULT' if $imports[0] =~ m/^!/;
74 foreach $spec (@imports){
75 $remove = $spec =~ s/^!//;
78 if ($spec eq 'DEFAULT'){
81 elsif ($tagdata = $tagsref->{$spec}) {
85 warn qq["$spec" is not defined in %${pkg}::EXPORT_TAGS];
90 elsif ($spec =~ m:^/(.*)/$:){
92 @allexports = keys %$export_cache unless @allexports; # only do keys once
93 @names = grep(/$patn/, @allexports); # not anchored by default
96 @names = ($spec); # is a normal symbol name
99 warn "Import ".($remove ? "del":"add").": @names "
103 foreach $sym (@names) { delete $imports{$sym} }
106 @imports{@names} = (1) x @names;
109 @imports = keys %imports;
113 foreach $sym (@imports) {
114 if (!$export_cache->{$sym}) {
115 if ($sym =~ m/^\d/) {
116 $pkg->VERSION($sym); # inherit from UNIVERSAL
117 # If the version number was the only thing specified
118 # then we should act as if nothing was specified:
120 @imports = @$exports;
123 # We need a way to emulate 'use Foo ()' but still
124 # allow an easy version check: "use Foo 1.23, ''";
125 if (@imports == 2 and !$imports[1]) {
129 } elsif ($sym !~ s/^&// || !$export_cache->{$sym}) {
130 # accumulate the non-exports
132 qq["$sym" is not exported by the $pkg module\n];
139 Carp::croak("@{carp}Can't continue after import errors");
143 @imports = @$exports;
146 my($fail, $fail_cache) = (\@{"${pkg}::EXPORT_FAIL"},
147 \%{"${pkg}::EXPORT_FAIL"});
151 # Build cache of symbols. Optimise the lookup by adding
152 # barewords twice... both with and without a leading &.
153 # (Technique could be applied to $export_cache at cost of memory)
154 my @expanded = map { /^\w/ ? ($_, '&'.$_) : $_ } @$fail;
155 warn "${pkg}::EXPORT_FAIL cached: @expanded" if $Verbose;
156 @{$fail_cache}{@expanded} = (1) x @expanded;
159 foreach $sym (@imports) { push(@failed, $sym) if $fail_cache->{$sym} }
161 @failed = $pkg->export_fail(@failed);
162 foreach $sym (@failed) {
164 Carp::carp(qq["$sym" is not implemented by the $pkg module ],
165 "on this architecture");
169 Carp::croak("Can't continue after import errors");
174 warn "Importing into $callpkg from $pkg: ",
175 join(", ",sort @imports) if $Verbose;
177 foreach $sym (@imports) {
178 # shortcut for the common case of no type character
179 (*{"${callpkg}::$sym"} = \&{"${pkg}::$sym"}, next)
180 unless $sym =~ s/^(\W)//;
182 *{"${callpkg}::$sym"} =
183 $type eq '&' ? \&{"${pkg}::$sym"} :
184 $type eq '$' ? \${"${pkg}::$sym"} :
185 $type eq '@' ? \@{"${pkg}::$sym"} :
186 $type eq '%' ? \%{"${pkg}::$sym"} :
187 $type eq '*' ? *{"${pkg}::$sym"} :
188 do { require Carp; Carp::croak("Can't export symbol: $type$sym") };
192 sub heavy_export_to_level
196 (undef) = shift; # XXX redundant arg
197 my $callpkg = caller($level);
198 $pkg->export($callpkg, @_);
204 my($pkg, $var, $syms) = @_;
206 my $export_tags = \%{"${pkg}::EXPORT_TAGS"};
207 push(@{"${pkg}::$var"},
208 map { $export_tags->{$_} ? @{$export_tags->{$_}}
209 : scalar(push(@nontag,$_),$_) }
210 (@$syms) ? @$syms : keys %$export_tags);
211 if (@nontag and $^W) {
212 # This may change to a die one day
214 Carp::carp(join(", ", @nontag)." are not tags of $pkg");
218 sub heavy_require_version {
219 my($self, $wanted) = @_;
220 my $pkg = ref $self || $self;
221 return ${pkg}->VERSION($wanted);
224 sub heavy_export_tags {
225 _push_tags((caller)[0], "EXPORT", \@_);
228 sub heavy_export_ok_tags {
229 _push_tags((caller)[0], "EXPORT_OK", \@_);