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