File Coverage

inc/Module/Install.pm
Criterion Covered Total %
statement 128 157 81.5
branch 29 64 45.3
condition 10 29 34.4
subroutine 20 20 100.0
pod 0 7 0.0
total 187 277 67.5


line stmt bran cond sub pod time code
1             #line 1
2             package Module::Install;
3              
4             # For any maintainers:
5             # The load order for Module::Install is a bit magic.
6             # It goes something like this...
7             #
8             # IF ( host has Module::Install installed, creating author mode ) {
9             # 1. Makefile.PL calls "use inc::Module::Install"
10             # 2. $INC{inc/Module/Install.pm} set to installed version of inc::Module::Install
11             # 3. The installed version of inc::Module::Install loads
12             # 4. inc::Module::Install calls "require Module::Install"
13             # 5. The ./inc/ version of Module::Install loads
14             # } ELSE {
15             # 1. Makefile.PL calls "use inc::Module::Install"
16             # 2. $INC{inc/Module/Install.pm} set to ./inc/ version of Module::Install
17             # 3. The ./inc/ version of Module::Install loads
18             # }
19 1     1   29  
  1         3  
  1         43  
20 1     1   4 use 5.004;
  1         3  
  1         33  
21             use strict 'vars';
22 1     1   5  
  1         1  
  1         77  
23             use vars qw{$VERSION};
24             BEGIN {
25             # All Module::Install core packages now require synchronised versions.
26             # This will be used to ensure we don't accidentally load old or
27             # different versions of modules.
28             # This is not enforced yet, but will be some time in the next few
29             # releases once we can make sure it won't clash with custom
30 1     1   374 # Module::Install extensions.
31             $VERSION = '0.64';
32             }
33              
34             # Whether or not inc::Module::Install is actually loaded, the
35             # $INC{inc/Module/Install.pm} is what will still get set as long as
36             # the caller loaded module this in the documented manner.
37             # If not set, the caller may NOT have loaded the bundled version, and thus
38             # they may not have a MI version that works with the Makefile.PL. This would
39             # result in false errors or unexpected behaviour. And we don't want that.
40             my $file = join( '/', 'inc', split /::/, __PACKAGE__ ) . '.pm';
41             unless ( $INC{$file} ) {
42             die <<"END_DIE";
43             Please invoke ${\__PACKAGE__} with:
44              
45             use inc::${\__PACKAGE__};
46              
47             not:
48              
49             use ${\__PACKAGE__};
50              
51             END_DIE
52             }
53              
54             # If the script that is loading Module::Install is from the future,
55             # then make will detect this and cause it to re-run over and over
56             # again. This is bad. Rather than taking action to touch it (which
57             # is unreliable on some platforms and requires write permissions)
58             # for now we should catch this and refuse to run.
59             if ( -f $0 and (stat($0))[9] > time ) {
60             die << "END_DIE";
61             Your installer $0 has a modification time in the future.
62              
63             This is known to create infinite loops in make.
64              
65             Please correct this, then run $0 again.
66              
67             END_DIE
68             }
69 1     1   6  
  1         1  
  1         16  
70 1     1   5 use Cwd ();
  1         2  
  1         14  
71 1     1   5 use File::Find ();
  1         1  
  1         25  
72 1     1   1116 use File::Path ();
  1         1535  
  1         2934  
73             use FindBin;
74              
75             *inc::Module::Install::VERSION = *VERSION;
76             @inc::Module::Install::ISA = __PACKAGE__;
77              
78 5     5 0 12 sub autoload {
79 5         48 my $self = shift;
80 5         35736 my $who = $self->_caller;
81 5         106 my $cwd = Cwd::cwd();
82             my $sym = "${who}::AUTOLOAD";
83 13     13   86366 $sym->{$cwd} = sub {
84 13 50       725 my $pwd = Cwd::cwd();
85             if ( my $code = $sym->{$pwd} ) {
86 13 50       137 # delegate back to parent dirs
87             goto &$code unless $cwd eq $pwd;
88 13 50       586 }
89 13         10974 $$sym =~ /([^:]+)$/ or die "Cannot autoload $who - $sym";
90 13 50       120 unshift @_, ($self, $1);
  13         461  
91 5         374 goto &{$self->can('call')} unless uc($1) eq $1;
92             };
93             }
94              
95 1     1   2 sub import {
96 1         6 my $class = shift;
97 1         8 my $self = $class->new(@_);
98             my $who = $self->_caller;
99 1 50       37  
100 0         0 unless ( -f $self->{file} ) {
101 0         0 require "$self->{path}/$self->{dispatch}.pm";
102 0         0 File::Path::mkpath("$self->{prefix}/$self->{author}");
103 0         0 $self->{admin} = "$self->{name}::$self->{dispatch}"->new( _top => $self );
104 0         0 $self->{admin}->init;
105 0         0 @_ = ($class, _self => $self);
  0         0  
106             goto &{"$self->{name}::import"};
107             }
108 1         5  
  1         17  
109 1         12 *{"${who}::AUTOLOAD"} = $self->autoload;
110             $self->preload;
111              
112 1         6 # Unregister loader and worker packages so subdirs can use them again
113 1         3335 delete $INC{"$self->{file}"};
114             delete $INC{"$self->{path}.pm"};
115             }
116              
117 1     1 0 5 sub preload {
118             my ($self) = @_;
119 1 50       49  
120 1         27 unless ( $self->{extensions} ) {
121             $self->load_extensions(
122             "$self->{prefix}/$self->{path}", $self
123             );
124             }
125 1         2  
  1         6  
126 1 50       4 my @exts = @{$self->{extensions}};
127 0         0 unless ( @exts ) {
128 0         0 my $admin = $self->{admin};
129             @exts = $admin->load_all_extensions;
130             }
131 1         2  
132 1         3 my %seen;
133 10         14 foreach my $obj ( @exts ) {
  181         1871  
134 171 100       2572 while (my ($method, $glob) = each %{ref($obj) . '::'}) {
135 129 100       483 next unless $obj->can($method);
136 127 100       415 next if $method =~ /^_/;
137 116         492 next if $method eq uc($method);
138             $seen{$method}++;
139             }
140             }
141 1         6  
142 1         57 my $who = $self->_caller;
143 78         400 foreach my $name ( sort keys %seen ) {
144 9     9   49 *{"${who}::$name"} = sub {
  9         81  
145 9         18 ${"${who}::AUTOLOAD"} = "${who}::$name";
  9         79  
146 78         611 goto &{"${who}::AUTOLOAD"};
147             };
148             }
149             }
150              
151 1     1 0 10 sub new {
152             my ($class, %args) = @_;
153              
154 1         191 # ignore the prefix on extension modules built from top level.
155 1 50       7287 my $base_path = Cwd::abs_path($FindBin::Bin);
156 0         0 unless ( Cwd::abs_path(Cwd::cwd()) eq $base_path ) {
157             delete $args{prefix};
158             }
159 1 50       15  
160             return $args{_self} if $args{_self};
161 1   50     27  
162 1   50     19 $args{dispatch} ||= 'Admin';
163 1 50 33     37 $args{prefix} ||= 'inc';
164 1   50     18 $args{author} ||= ($^O eq 'VMS' ? '_author' : '.author');
165 1   33     19 $args{bundle} ||= 'inc/BUNDLES';
166 1         50 $args{base} ||= $base_path;
167 1   33     17 $class =~ s/^\Q$args{prefix}\E:://;
168 1   33     42 $args{name} ||= $class;
169 1 50       10 $args{version} ||= $class->VERSION;
170 1         47 unless ( $args{path} ) {
171 1         10 $args{path} = $args{name};
172             $args{path} =~ s!::!/!g;
173 1   33     16 }
174             $args{file} ||= "$args{base}/$args{prefix}/$args{path}.pm";
175 1         16  
176             bless( \%args, $class );
177             }
178              
179 13     13 0 70 sub call {
180 13 50       195 my ($self, $method) = @_;
181 13         80 my $obj = $self->load($method) or return;
182 13         31 splice(@_, 0, 2, $obj);
  13         337  
183             goto &{$obj->can($method)};
184             }
185              
186 13     13 0 57 sub load {
187             my ($self, $method) = @_;
188 13 50       147  
189             $self->load_extensions(
190             "$self->{prefix}/$self->{path}", $self
191             ) unless $self->{extensions};
192 13         21  
  13         223  
193 95 100       2787 foreach my $obj (@{$self->{extensions}}) {
194             return $obj if $obj->can($method);
195             }
196 0 0       0  
197             my $admin = $self->{admin} or die <<"END_DIE";
198             The '$method' method does not exist in the '$self->{prefix}' path!
199             Please remove the '$self->{prefix}' directory and run $0 again to load it.
200             END_DIE
201 0         0  
202 0         0 my $obj = $admin->load($method, 1);
  0         0  
203             push @{$self->{extensions}}, $obj;
204 0         0  
205             $obj;
206             }
207              
208 1     1 0 6 sub load_extensions {
209             my ($self, $path, $top) = @_;
210 1 50       10  
  10         40  
211 0         0 unless ( grep { lc $_ eq lc $self->{prefix} } @INC ) {
212             unshift @INC, $self->{prefix};
213             }
214 1         7  
215 10         20 foreach my $rv ( $self->find_extensions($path) ) {
  10         28  
216 10 50       40 my ($file, $pkg) = @{$rv};
217             next if $self->{pathnames}{$pkg};
218 10         16  
219 10         19 local $@;
  10         30663  
  10         301  
220 10 50       46 my $new = eval { require $file; $pkg->can('new') };
221 0 0       0 unless ( $new ) {
222 0         0 warn $@ if $@;
223             next;
224 10         139 }
225 10         21 $self->{pathnames}{$pkg} = delete $INC{$file};
  10         37  
  10         38  
226             push @{$self->{extensions}}, &{$new}($pkg, _top => $top );
227             }
228 1   50     15  
229             $self->{extensions} ||= [];
230             }
231              
232 1     1 0 3 sub find_extensions {
233             my ($self, $path) = @_;
234 1         7  
235             my @found;
236 11     11   14 File::Find::find( sub {
237 11 100       257 my $file = $File::Find::name;
238 10         23 return unless $file =~ m!^\Q$path\E/(.+)\.pm\Z!is;
239 10 50       28 my $subpath = $1;
240             return if lc($subpath) eq lc($self->{dispatch});
241 10         20  
242 10         20 $file = "$self->{path}/$subpath.pm";
243 10         14 my $pkg = "$self->{name}::$subpath";
244             $pkg =~ s!/!::!g;
245              
246             # If we have a mixed-case package name, assume case has been preserved
247             # correctly. Otherwise, root through the file to locate the case-preserved
248 10 50 33     60 # version of the package name.
249 0 0       0 if ( $subpath eq lc($subpath) || $subpath eq uc($subpath) ) {
250 0         0 open PKGFILE, "<$subpath.pm" or die "find_extensions: Can't open $subpath.pm: $!";
251 0         0 my $in_pod = 0;
252 0 0       0 while ( <PKGFILE> ) {
253 0 0       0 $in_pod = 1 if /^=\w/;
254 0 0 0     0 $in_pod = 0 if /^=cut/;
255 0 0       0 next if ($in_pod || /^=cut/); # skip pod text
256 0 0       0 next if /^\s*#/; # and comments
257 0         0 if ( m/^\s*package\s+($pkg)\s*;/i ) {
258 0         0 $pkg = $1;
259             last;
260             }
261 0         0 }
262             close PKGFILE;
263             }
264 10         222  
265 1 50       237 push @found, [ $file, $pkg ];
266             }, $path ) if -d $path;
267 1         20  
268             @found;
269             }
270              
271 7     7   17 sub _caller {
272 7         34 my $depth = 0;
273 7         28 my $call = caller($depth);
274 9         13 while ( $call eq __PACKAGE__ ) {
275 9         39 $depth++;
276             $call = caller($depth);
277 7         19 }
278             return $call;
279             }
280              
281             1;