File Coverage

blib/lib/Exporter/Heavy.pm
Criterion Covered Total %
statement 0 134 0.0
branch 0 80 0.0
condition 0 16 0.0
subroutine 0 9 0.0
pod 0 5 0.0
total 0 244 0.0


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