| 1 | package Exporter; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | no strict 'refs'; | ||
| 5 | |||
| 6 | our $Debug = 0; | ||
| 7 | our $ExportLevel = 0; | ||
| 8 | our $Verbose ||= 0; | ||
| 9 | our $VERSION = '5.78'; | ||
| 10 | our %Cache; | ||
| 11 | |||
| 12 | sub as_heavy { | ||
| 13 | require Exporter::Heavy; | ||
| 14 | # Unfortunately, this does not work if the caller is aliased as *name = \&foo | ||
| 15 | # Thus the need to create a lot of identical subroutines | ||
| 16 | my $c = (caller(1))[3]; | ||
| 17 | $c =~ s/.*:://; | ||
| 18 | \&{"Exporter::Heavy::heavy_$c"}; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub export { | ||
| 22 | goto &{as_heavy()}; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub import { | ||
| 26 | my $pkg = shift; | ||
| 27 | my $callpkg = caller($ExportLevel); | ||
| 28 | |||
| 29 | if ($pkg eq "Exporter" and @_ and $_[0] eq "import") { | ||
| 30 | *{$callpkg."::import"} = \&import; | ||
| 31 | return; | ||
| 32 | } | ||
| 33 | |||
| 34 | # We *need* to treat @{"$pkg\::EXPORT_FAIL"} since Carp uses it :-( | ||
| 35 | my $exports = \@{"$pkg\::EXPORT"}; | ||
| 36 | # But, avoid creating things if they don't exist, which saves a couple of | ||
| 37 | # hundred bytes per package processed. | ||
| 38 | my $fail = ${$pkg . '::'}{EXPORT_FAIL} && \@{"$pkg\::EXPORT_FAIL"}; | ||
| 39 | return export $pkg, $callpkg, @_ | ||
| 40 | if $Verbose or $Debug or $fail && @$fail > 1; | ||
| 41 | my $export_cache = ($Cache{$pkg} ||= {}); | ||
| 42 | my $args = @_ or @_ = @$exports; | ||
| 43 | |||
| 44 | if ($args and not %$export_cache) { | ||
| 45 | s/^&//, $export_cache->{$_} = 1 | ||
| 46 | foreach (@$exports, @{"$pkg\::EXPORT_OK"}); | ||
| 47 | } | ||
| 48 | my $heavy; | ||
| 49 | # Try very hard not to use {} and hence have to enter scope on the foreach | ||
| 50 | # We bomb out of the loop with last as soon as heavy is set. | ||
| 51 | if ($args or $fail) { | ||
| 52 | ($heavy = (/\W/ or $args and not exists $export_cache->{$_} | ||
| 53 | or $fail and @$fail and $_ eq $fail->[0])) and last | ||
| 54 | foreach (@_); | ||
| 55 | } else { | ||
| 56 | ($heavy = /\W/) and last | ||
| 57 | foreach (@_); | ||
| 58 | } | ||
| 59 | return export $pkg, $callpkg, ($args ? @_ : ()) if $heavy; | ||
| 60 | local $SIG{__WARN__} = | ||
| 61 | sub {require Carp; &Carp::carp} if not $SIG{__WARN__}; | ||
| 62 | # shortcut for the common case of no type character | ||
| 63 | *{"$callpkg\::$_"} = \&{"$pkg\::$_"} foreach @_; | ||
| 64 | } | ||
| 65 | |||
| 66 | # Default methods | ||
| 67 | |||
| 68 | sub export_fail { | ||
| 69 | my $self = shift; | ||
| 70 | @_; | ||
| 71 | } | ||
| 72 | |||
| 73 | # Unfortunately, caller(1)[3] "does not work" if the caller is aliased as | ||
| 74 | # *name = \&foo. Thus the need to create a lot of identical subroutines | ||
| 75 | # Otherwise we could have aliased them to export(). | ||
| 76 | |||
| 77 | sub export_to_level { | ||
| 78 | goto &{as_heavy()}; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub export_tags { | ||
| 82 | goto &{as_heavy()}; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub export_ok_tags { | ||
| 86 | goto &{as_heavy()}; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub require_version { | ||
| 90 | goto &{as_heavy()}; | ||
| 91 | } | ||
| 92 | |||
| 93 | 1; | ||
| 94 | __END__ |