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