5 Exporter::Heavy - Exporter guts
13 No user-serviceable parts inside.
17 # We go to a lot of trouble not to 'require Carp' at file scope,
18 # because Carp requires Exporter, and something has to give.
23 # First make import warnings look like they're coming from the "use".
24 local $SIG{__WARN__
} = sub {
26 if ($text =~ s/ at \S*Exporter\S*.pm line \d+.*\n//) {
28 local $Carp::CarpLevel
= 1; # ignore package calling us too.
35 local $SIG{__DIE__
} = sub {
37 local $Carp::CarpLevel
= 1; # ignore package calling us too.
38 Carp
::croak
("$_[0]Illegal null symbol in \@${1}::EXPORT")
39 if $_[0] =~ /^Unable to create sub named "(.*?)::"/;
42 my($pkg, $callpkg, @imports) = @_;
43 my($type, $sym, $oops);
44 *exports
= *{"${pkg}::EXPORT"};
48 grep(s/^&//, @exports);
49 @exports{@exports} = (1) x
@exports;
50 my $ok = \@
{"${pkg}::EXPORT_OK"};
53 @exports{@
$ok} = (1) x @
$ok;
57 if ($imports[0] =~ m
#^[/!:]#){
58 my $tagsref = \
%{"${pkg}::EXPORT_TAGS"};
61 my($remove, $spec, @names, @allexports);
62 # negated first item implies starting with default set:
63 unshift @imports, ':DEFAULT' if $imports[0] =~ m/^!/;
64 foreach $spec (@imports){
65 $remove = $spec =~ s/^!//;
68 if ($spec eq 'DEFAULT'){
71 elsif ($tagdata = $tagsref->{$spec}) {
75 warn qq["$spec" is
not defined in %${pkg
}::EXPORT_TAGS
];
80 elsif ($spec =~ m
:^/(.*)/$:){
82 @allexports = keys %exports unless @allexports; # only do keys once
83 @names = grep(/$patn/, @allexports); # not anchored by default
86 @names = ($spec); # is a normal symbol name
89 warn "Import ".($remove ?
"del":"add").": @names "
93 foreach $sym (@names) { delete $imports{$sym} }
96 @imports{@names} = (1) x
@names;
99 @imports = keys %imports;
102 foreach $sym (@imports) {
103 if (!$exports{$sym}) {
104 if ($sym =~ m/^\d/) {
105 $pkg->require_version($sym);
106 # If the version number was the only thing specified
107 # then we should act as if nothing was specified:
112 # We need a way to emulate 'use Foo ()' but still
113 # allow an easy version check: "use Foo 1.23, ''";
114 if (@imports == 2 and !$imports[1]) {
118 } elsif ($sym !~ s/^&// || !$exports{$sym}) {
120 Carp
::carp
(qq["$sym" is
not exported by the
$pkg module
]);
127 Carp
::croak
("Can't continue after import errors");
134 *fail
= *{"${pkg}::EXPORT_FAIL"};
137 # Build cache of symbols. Optimise the lookup by adding
138 # barewords twice... both with and without a leading &.
139 # (Technique could be applied to %exports cache at cost of memory)
140 my @expanded = map { /^\w/ ?
($_, '&'.$_) : $_ } @fail;
141 warn "${pkg}::EXPORT_FAIL cached: @expanded" if $Verbose;
142 @fail{@expanded} = (1) x
@expanded;
145 foreach $sym (@imports) { push(@failed, $sym) if $fail{$sym} }
147 @failed = $pkg->export_fail(@failed);
148 foreach $sym (@failed) {
150 Carp
::carp
(qq["$sym" is
not implemented by the
$pkg module
],
151 "on this architecture");
155 Carp
::croak
("Can't continue after import errors");
160 warn "Importing into $callpkg from $pkg: ",
161 join(", ",sort @imports) if $Verbose;
163 foreach $sym (@imports) {
164 # shortcut for the common case of no type character
165 (*{"${callpkg}::$sym"} = \
&{"${pkg}::$sym"}, next)
166 unless $sym =~ s/^(\W)//;
168 *{"${callpkg}::$sym"} =
169 $type eq '&' ? \
&{"${pkg}::$sym"} :
170 $type eq '$' ? \
${"${pkg}::$sym"} :
171 $type eq '@' ? \@
{"${pkg}::$sym"} :
172 $type eq '%' ? \
%{"${pkg}::$sym"} :
173 $type eq '*' ?
*{"${pkg}::$sym"} :
174 do { require Carp
; Carp
::croak
("Can't export symbol: $type$sym") };
178 sub heavy_export_to_level
182 (undef) = shift; # XXX redundant arg
183 my $callpkg = caller($level);
184 $pkg->export($callpkg, @_);
190 my($pkg, $var, $syms) = @_;
192 *export_tags
= \
%{"${pkg}::EXPORT_TAGS"};
193 push(@
{"${pkg}::$var"},
194 map { $export_tags{$_} ? @
{$export_tags{$_}} : scalar(++$nontag,$_) }
195 (@
$syms) ? @
$syms : keys %export_tags);
196 if ($nontag and $^W
) {
197 # This may change to a die one day
199 Carp
::carp
("Some names are not tags");
210 sub require_version
{
211 my($self, $wanted) = @_;
212 my $pkg = ref $self || $self;
213 my $version = ${"${pkg}::VERSION"};
214 if (!$version or $version < $wanted) {
215 $version ||= "(undef)";
216 # %INC contains slashes, but $pkg contains double-colons.
217 my $file = (map {s
,::,/,g
; $INC{$_}} "$pkg.pm")[0];
218 $file &&= " ($file)";
220 Carp
::croak
("$pkg $wanted required--this is only version $version$file")