2 package Module::Install;
5 # The load order for Module::Install is a bit magic.
6 # It goes something like this...
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
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
23 use vars qw{$VERSION $MAIN};
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 # Module::Install extensions.
33 # Storage for the pseudo-singleton
36 *inc::Module::Install::VERSION = *VERSION;
37 @inc::Module::Install::ISA = __PACKAGE__;
45 # Whether or not inc::Module::Install is actually loaded, the
46 # $INC{inc/Module/Install.pm} is what will still get set as long as
47 # the caller loaded module this in the documented manner.
48 # If not set, the caller may NOT have loaded the bundled version, and thus
49 # they may not have a MI version that works with the Makefile.PL. This would
50 # result in false errors or unexpected behaviour. And we don't want that.
51 my $file = join( '/', 'inc', split /::/, __PACKAGE__ ) . '.pm';
52 unless ( $INC{$file} ) { die <<"END_DIE" }
54 Please invoke ${\__PACKAGE__} with:
56 use inc::${\__PACKAGE__};
68 # If the script that is loading Module::Install is from the future,
69 # then make will detect this and cause it to re-run over and over
70 # again. This is bad. Rather than taking action to touch it (which
71 # is unreliable on some platforms and requires write permissions)
72 # for now we should catch this and refuse to run.
74 my $s = (stat($0))[9];
76 # If the modification time is only slightly in the future,
77 # sleep briefly to remove the problem.
79 if ( $a > 0 and $a < 5 ) { sleep 5 }
81 # Too far in the future, throw an error.
83 if ( $s > $t ) { die <<"END_DIE" }
85 Your installer $0 has a modification time in the future ($s > $t).
87 This is known to create infinite loops in make.
89 Please correct this, then run $0 again.
98 # Build.PL was formerly supported, but no longer is due to excessive
99 # difficulty in implementing every single feature twice.
100 if ( $0 =~ /Build.PL$/i ) { die <<"END_DIE" }
102 Module::Install no longer supports Build.PL.
104 It was impossible to maintain duel backends, and has been deprecated.
106 Please remove all Build.PL files and only use the Makefile.PL installer.
114 # To save some more typing in Module::Install installers, every...
115 # use inc::Module::Install
116 # ...also acts as an implicit use strict.
117 $^H |= strict::bits(qw(refs subs vars));
130 my $who = $self->_caller;
131 my $cwd = Cwd::cwd();
132 my $sym = "${who}::AUTOLOAD";
134 my $pwd = Cwd::cwd();
135 if ( my $code = $sym->{$pwd} ) {
136 # Delegate back to parent dirs
137 goto &$code unless $cwd eq $pwd;
139 $$sym =~ /([^:]+)$/ or die "Cannot autoload $who - $sym";
141 if ( uc($method) eq $method ) {
144 } elsif ( $method =~ /^_/ and $self->can($method) ) {
145 # Dispatch to the root M:I class
146 return $self->$method(@_);
149 # Dispatch to the appropriate plugin
150 unshift @_, ( $self, $1 );
151 goto &{$self->can('call')};
157 my $self = $class->new(@_);
158 my $who = $self->_caller;
160 unless ( -f $self->{file} ) {
161 require "$self->{path}/$self->{dispatch}.pm";
162 File::Path::mkpath("$self->{prefix}/$self->{author}");
163 $self->{admin} = "$self->{name}::$self->{dispatch}"->new( _top => $self );
164 $self->{admin}->init;
165 @_ = ($class, _self => $self);
166 goto &{"$self->{name}::import"};
169 *{"${who}::AUTOLOAD"} = $self->autoload;
172 # Unregister loader and worker packages so subdirs can use them again
173 delete $INC{"$self->{file}"};
174 delete $INC{"$self->{path}.pm"};
176 # Save to the singleton
184 unless ( $self->{extensions} ) {
185 $self->load_extensions(
186 "$self->{prefix}/$self->{path}", $self
190 my @exts = @{$self->{extensions}};
192 @exts = $self->{admin}->load_all_extensions;
196 foreach my $obj ( @exts ) {
197 while (my ($method, $glob) = each %{ref($obj) . '::'}) {
198 next unless $obj->can($method);
199 next if $method =~ /^_/;
200 next if $method eq uc($method);
205 my $who = $self->_caller;
206 foreach my $name ( sort keys %seen ) {
207 *{"${who}::$name"} = sub {
208 ${"${who}::AUTOLOAD"} = "${who}::$name";
209 goto &{"${who}::AUTOLOAD"};
215 my ($class, %args) = @_;
217 # ignore the prefix on extension modules built from top level.
218 my $base_path = Cwd::abs_path($FindBin::Bin);
219 unless ( Cwd::abs_path(Cwd::cwd()) eq $base_path ) {
220 delete $args{prefix};
223 return $args{_self} if $args{_self};
225 $args{dispatch} ||= 'Admin';
226 $args{prefix} ||= 'inc';
227 $args{author} ||= ($^O eq 'VMS' ? '_author' : '.author');
228 $args{bundle} ||= 'inc/BUNDLES';
229 $args{base} ||= $base_path;
230 $class =~ s/^\Q$args{prefix}\E:://;
231 $args{name} ||= $class;
232 $args{version} ||= $class->VERSION;
233 unless ( $args{path} ) {
234 $args{path} = $args{name};
235 $args{path} =~ s!::!/!g;
237 $args{file} ||= "$args{base}/$args{prefix}/$args{path}.pm";
240 bless( \%args, $class );
244 my ($self, $method) = @_;
245 my $obj = $self->load($method) or return;
246 splice(@_, 0, 2, $obj);
247 goto &{$obj->can($method)};
251 my ($self, $method) = @_;
253 $self->load_extensions(
254 "$self->{prefix}/$self->{path}", $self
255 ) unless $self->{extensions};
257 foreach my $obj (@{$self->{extensions}}) {
258 return $obj if $obj->can($method);
261 my $admin = $self->{admin} or die <<"END_DIE";
262 The '$method' method does not exist in the '$self->{prefix}' path!
263 Please remove the '$self->{prefix}' directory and run $0 again to load it.
266 my $obj = $admin->load($method, 1);
267 push @{$self->{extensions}}, $obj;
272 sub load_extensions {
273 my ($self, $path, $top) = @_;
275 unless ( grep { ! ref $_ and lc $_ eq lc $self->{prefix} } @INC ) {
276 unshift @INC, $self->{prefix};
279 foreach my $rv ( $self->find_extensions($path) ) {
280 my ($file, $pkg) = @{$rv};
281 next if $self->{pathnames}{$pkg};
284 my $new = eval { require $file; $pkg->can('new') };
289 $self->{pathnames}{$pkg} = delete $INC{$file};
290 push @{$self->{extensions}}, &{$new}($pkg, _top => $top );
293 $self->{extensions} ||= [];
296 sub find_extensions {
297 my ($self, $path) = @_;
300 File::Find::find( sub {
301 my $file = $File::Find::name;
302 return unless $file =~ m!^\Q$path\E/(.+)\.pm\Z!is;
304 return if lc($subpath) eq lc($self->{dispatch});
306 $file = "$self->{path}/$subpath.pm";
307 my $pkg = "$self->{name}::$subpath";
310 # If we have a mixed-case package name, assume case has been preserved
311 # correctly. Otherwise, root through the file to locate the case-preserved
312 # version of the package name.
313 if ( $subpath eq lc($subpath) || $subpath eq uc($subpath) ) {
314 my $content = Module::Install::_read($subpath . '.pm');
316 foreach ( split //, $content ) {
317 $in_pod = 1 if /^=\w/;
318 $in_pod = 0 if /^=cut/;
319 next if ($in_pod || /^=cut/); # skip pod text
320 next if /^\s*#/; # and comments
321 if ( m/^\s*package\s+($pkg)\s*;/i ) {
328 push @found, [ $file, $pkg ];
329 }, $path ) if -d $path;
338 #####################################################################
339 # Common Utility Functions
343 my $call = caller($depth);
344 while ( $call eq __PACKAGE__ ) {
346 $call = caller($depth);
354 open( FH, '<', $_[0] ) or die "open($_[0]): $!";
356 open( FH, "< $_[0]" ) or die "open($_[0]): $!";
358 my $string = do { local $/; <FH> };
359 close FH or die "close($_[0]): $!";
364 my $string = Module::Install::_read($_[0]);
365 $string =~ s/(?:\015{1,2}\012|\015|\012)/\n/sg;
366 $string =~ s/(\n)\n*__(?:DATA|END)__\b.*\z/$1/s;
367 $string =~ s/\n\n=\w+.+?\n\n=cut\b.+?\n+/\n\n/sg;
372 my $string = Module::Install::_read($_[0]);
373 $string =~ s/(?:\015{1,2}\012|\015|\012)/\n/sg;
374 return $string if $_[0] =~ /\.pod\z/;
375 $string =~ s/(^|\n=cut\b.+?\n+)[^=\s].+?\n(\n=\w+|\z)/$1$2/sg;
376 $string =~ s/\n*=pod\b[^\n]*\n+/\n\n/sg;
377 $string =~ s/\n*=cut\b[^\n]*\n+/\n\n/sg;
378 $string =~ s/^\n+//s;
385 open( FH, '>', $_[0] ) or die "open($_[0]): $!";
387 open( FH, "> $_[0]" ) or die "open($_[0]): $!";
389 foreach ( 1 .. $#_ ) {
390 print FH $_[$_] or die "print($_[0]): $!";
392 close FH or die "close($_[0]): $!";
395 # _version is for processing module versions (eg, 1.03_05) not
396 # Perl versions (eg, 5.8.1).
399 my $d =()= $s =~ /(\.)/g;
401 # Normalise multipart versions
402 $s =~ s/(\.)(\d{1,3})/sprintf("$1%03d",$2)/eg;
407 $_ . '0' x (3 - length $_)
408 } $s =~ /(\d{1,3})\D?/g;
409 $l = $l . '.' . join '', @v if @v;
414 _version($_[0]) <=> _version($_[1]);
417 # Cloned from Params::Util::_CLASS
424 $_[0] =~ m/^[^\W\d]\w*(?:::\w+)*\z/s
430 # Copyright 2008 - 2009 Adam Kennedy.