Re: Copious warnings from Sys::Syslog
[p5sagit/p5-mst-13.2.git] / lib / Exporter / Heavy.pm
CommitLineData
b75c8c73 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;
bb2cbcd1 8$Carp::Internal{"Exporter::Heavy"} = 1;
b75c8c73 9our $Verbose;
4af1b167 10
ca24dfc6 11=head1 NAME
12
13Exporter::Heavy - Exporter guts
14
fe854a6f 15=head1 SYNOPSIS
ca24dfc6 16
17(internal use only)
18
19=head1 DESCRIPTION
20
21No user-serviceable parts inside.
3cb6de81 22
ca24dfc6 23=cut
4ac9195f 24
4af1b167 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
30sub 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);
b75c8c73 53 my($exports, $export_cache) = (\@{"${pkg}::EXPORT"},
54 \%{"${pkg}::EXPORT"});
4af1b167 55
56 if (@imports) {
b75c8c73 57 if (!%$export_cache) {
58 s/^&// foreach @$exports;
59 @{$export_cache}{@$exports} = (1) x @$exports;
4af1b167 60 my $ok = \@{"${pkg}::EXPORT_OK"};
61 if (@$ok) {
b75c8c73 62 s/^&// foreach @$ok;
63 @{$export_cache}{@$ok} = (1) x @$ok;
4af1b167 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'){
b75c8c73 79 @names = @$exports;
4af1b167 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;
b75c8c73 92 @allexports = keys %$export_cache unless @allexports; # only do keys once
4af1b167 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
ddc53c8b 112 my @carp;
4af1b167 113 foreach $sym (@imports) {
b75c8c73 114 if (!$export_cache->{$sym}) {
4af1b167 115 if ($sym =~ m/^\d/) {
22b4675c 116 $pkg->VERSION($sym); # inherit from UNIVERSAL
4af1b167 117 # If the version number was the only thing specified
118 # then we should act as if nothing was specified:
119 if (@imports == 1) {
b75c8c73 120 @imports = @$exports;
4af1b167 121 last;
122 }
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]) {
126 @imports = ();
127 last;
128 }
b75c8c73 129 } elsif ($sym !~ s/^&// || !$export_cache->{$sym}) {
ddc53c8b 130 # accumulate the non-exports
131 push @carp,
132 qq["$sym" is not exported by the $pkg module\n];
4af1b167 133 $oops++;
134 }
135 }
136 }
137 if ($oops) {
138 require Carp;
ddc53c8b 139 Carp::croak("@{carp}Can't continue after import errors");
4af1b167 140 }
141 }
142 else {
b75c8c73 143 @imports = @$exports;
4af1b167 144 }
145
b75c8c73 146 my($fail, $fail_cache) = (\@{"${pkg}::EXPORT_FAIL"},
147 \%{"${pkg}::EXPORT_FAIL"});
148
149 if (@$fail) {
150 if (!%$fail_cache) {
4af1b167 151 # Build cache of symbols. Optimise the lookup by adding
152 # barewords twice... both with and without a leading &.
b75c8c73 153 # (Technique could be applied to $export_cache at cost of memory)
154 my @expanded = map { /^\w/ ? ($_, '&'.$_) : $_ } @$fail;
4af1b167 155 warn "${pkg}::EXPORT_FAIL cached: @expanded" if $Verbose;
b75c8c73 156 @{$fail_cache}{@expanded} = (1) x @expanded;
4af1b167 157 }
158 my @failed;
b75c8c73 159 foreach $sym (@imports) { push(@failed, $sym) if $fail_cache->{$sym} }
4af1b167 160 if (@failed) {
161 @failed = $pkg->export_fail(@failed);
162 foreach $sym (@failed) {
163 require Carp;
164 Carp::carp(qq["$sym" is not implemented by the $pkg module ],
165 "on this architecture");
166 }
167 if (@failed) {
168 require Carp;
169 Carp::croak("Can't continue after import errors");
170 }
171 }
172 }
173
174 warn "Importing into $callpkg from $pkg: ",
175 join(", ",sort @imports) if $Verbose;
176
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)//;
181 $type = $1;
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") };
189 }
190}
191
192sub heavy_export_to_level
193{
194 my $pkg = shift;
195 my $level = shift;
31a63159 196 (undef) = shift; # XXX redundant arg
4af1b167 197 my $callpkg = caller($level);
198 $pkg->export($callpkg, @_);
199}
200
201# Utility functions
202
203sub _push_tags {
204 my($pkg, $var, $syms) = @_;
b75c8c73 205 my @nontag = ();
206 my $export_tags = \%{"${pkg}::EXPORT_TAGS"};
4af1b167 207 push(@{"${pkg}::$var"},
b75c8c73 208 map { $export_tags->{$_} ? @{$export_tags->{$_}}
209 : scalar(push(@nontag,$_),$_) }
210 (@$syms) ? @$syms : keys %$export_tags);
211 if (@nontag and $^W) {
4af1b167 212 # This may change to a die one day
213 require Carp;
b75c8c73 214 Carp::carp(join(", ", @nontag)." are not tags of $pkg");
4af1b167 215 }
216}
217
0e57b4e8 218sub heavy_require_version {
4af1b167 219 my($self, $wanted) = @_;
220 my $pkg = ref $self || $self;
22b4675c 221 return ${pkg}->VERSION($wanted);
4af1b167 222}
223
0e57b4e8 224sub heavy_export_tags {
225 _push_tags((caller)[0], "EXPORT", \@_);
226}
227
228sub heavy_export_ok_tags {
229 _push_tags((caller)[0], "EXPORT_OK", \@_);
230}
231
4af1b167 2321;