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