| 1 | package Module::Load; | ||
| 2 | |||
| 3 | $VERSION = '0.32'; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use File::Spec (); | ||
| 8 | |||
| 9 | sub import { | ||
| 10 | my $who = _who(); | ||
| 11 | my $h; shift; | ||
| 12 | |||
| 13 | { no strict 'refs'; | ||
| 14 | |||
| 15 | @_ or ( | ||
| 16 | *{"${who}::load"} = \&load, # compat to prev version | ||
| 17 | *{"${who}::autoload"} = \&autoload, | ||
| 18 | return | ||
| 19 | ); | ||
| 20 | |||
| 21 | map { $h->{$_} = () if defined $_ } @_; | ||
| 22 | |||
| 23 | (exists $h->{none} or exists $h->{''}) | ||
| 24 | and shift, last; | ||
| 25 | |||
| 26 | ((exists $h->{autoload} and shift,1) or (exists $h->{all} and shift)) | ||
| 27 | and *{"${who}::autoload"} = \&autoload; | ||
| 28 | |||
| 29 | ((exists $h->{load} and shift,1) or exists $h->{all}) | ||
| 30 | and *{"${who}::load"} = \&load; | ||
| 31 | |||
| 32 | ((exists $h->{load_remote} and shift,1) or exists $h->{all}) | ||
| 33 | and *{"${who}::load_remote"} = \&load_remote; | ||
| 34 | |||
| 35 | ((exists $h->{autoload_remote} and shift,1) or exists $h->{all}) | ||
| 36 | and *{"${who}::autoload_remote"} = \&autoload_remote; | ||
| 37 | |||
| 38 | } | ||
| 39 | |||
| 40 | } | ||
| 41 | |||
| 42 | sub load(*;@){ | ||
| 43 | goto &_load; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub autoload(*;@){ | ||
| 47 | unshift @_, 'autoimport'; | ||
| 48 | goto &_load; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub load_remote($$;@){ | ||
| 52 | my ($dst, $src, @exp) = @_; | ||
| 53 | |||
| 54 | eval "package $dst;Module::Load::load('$src', qw/@exp/);"; | ||
| 55 | $@ && die "$@"; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub autoload_remote($$;@){ | ||
| 59 | my ($dst, $src, @exp) = @_; | ||
| 60 | |||
| 61 | eval "package $dst;Module::Load::autoload('$src', qw/@exp/);"; | ||
| 62 | $@ && die "$@"; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub _load{ | ||
| 66 | my $autoimport = $_[0] eq 'autoimport' and shift; | ||
| 67 | my $mod = shift or return; | ||
| 68 | my $who = _who(); | ||
| 69 | |||
| 70 | if( _is_file( $mod ) ) { | ||
| 71 | require $mod; | ||
| 72 | } else { | ||
| 73 | LOAD: { | ||
| 74 | my $err; | ||
| 75 | for my $flag ( qw[1 0] ) { | ||
| 76 | my $file = _to_file( $mod, $flag); | ||
| 77 | eval { require $file }; | ||
| 78 | $@ ? $err .= $@ : last LOAD; | ||
| 79 | } | ||
| 80 | die $err if $err; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | ### This addresses #41883: Module::Load cannot import | ||
| 85 | ### non-Exporter module. ->import() routines weren't | ||
| 86 | ### properly called when load() was used. | ||
| 87 | |||
| 88 | { no strict 'refs'; | ||
| 89 | my $import; | ||
| 90 | |||
| 91 | ((@_ or $autoimport) and ( | ||
| 92 | $import = $mod->can('import') | ||
| 93 | ) and ( | ||
| 94 | unshift(@_, $mod), | ||
| 95 | goto &$import, | ||
| 96 | return | ||
| 97 | ) | ||
| 98 | ); | ||
| 99 | } | ||
| 100 | |||
| 101 | } | ||
| 102 | |||
| 103 | sub _to_file{ | ||
| 104 | local $_ = shift; | ||
| 105 | my $pm = shift || ''; | ||
| 106 | |||
| 107 | ## trailing blanks ignored by default. [rt #69886] | ||
| 108 | my @parts = split /::|'/, $_, -1; | ||
| 109 | ## make sure that we can't hop out of @INC | ||
| 110 | shift @parts if @parts && !$parts[0]; | ||
| 111 | |||
| 112 | ### because of [perl #19213], see caveats ### | ||
| 113 | my $file = $^O eq 'MSWin32' | ||
| 114 | ? join "/", @parts | ||
| 115 | : File::Spec->catfile( @parts ); | ||
| 116 | |||
| 117 | $file .= '.pm' if $pm; | ||
| 118 | |||
| 119 | ### on perl's before 5.10 (5.9.5@31746) if you require | ||
| 120 | ### a file in VMS format, it's stored in %INC in VMS | ||
| 121 | ### format. Therefor, better unixify it first | ||
| 122 | ### Patch in reply to John Malmbergs patch (as mentioned | ||
| 123 | ### above) on p5p Tue 21 Aug 2007 04:55:07 | ||
| 124 | $file = VMS::Filespec::unixify($file) if $^O eq 'VMS'; | ||
| 125 | |||
| 126 | return $file; | ||
| 127 | } | ||
| 128 | |||
| 129 | sub _who { (caller(1))[0] } | ||
| 130 | |||
| 131 | sub _is_file { | ||
| 132 | local $_ = shift; | ||
| 133 | return /^\./ ? 1 : | ||
| 134 | /[^\w:']/ ? 1 : | ||
| 135 | undef | ||
| 136 | #' silly bbedit.. | ||
| 137 | } | ||
| 138 | |||
| 139 | |||
| 140 | 1; | ||
| 141 | |||
| 142 | __END__ |