1 package Module::Install;
4 # The load order for Module::Install is a bit magic.
5 # It goes something like this...
7 # IF ( host has Module::Install installed, creating author mode ) {
8 # 1. Makefile.PL calls "use inc::Module::Install"
9 # 2. $INC{inc/Module/Install.pm} set to installed version of inc::Module::Install
10 # 3. The installed version of inc::Module::Install loads
11 # 4. inc::Module::Install calls "require Module::Install"
12 # 5. The ./inc/ version of Module::Install loads
14 # 1. Makefile.PL calls "use inc::Module::Install"
15 # 2. $INC{inc/Module/Install.pm} set to ./inc/ version of Module::Install
16 # 3. The ./inc/ version of Module::Install loads
22 use vars qw{$VERSION $MAIN};
24 # All Module::Install core packages now require synchronised versions.
25 # This will be used to ensure we don't accidentally load old or
26 # different versions of modules.
27 # This is not enforced yet, but will be some time in the next few
28 # releases once we can make sure it won't clash with custom
29 # Module::Install extensions.
32 # Storage for the pseudo-singleton
35 *inc::Module::Install::VERSION = *VERSION;
36 @inc::Module::Install::ISA = __PACKAGE__;
44 # Whether or not inc::Module::Install is actually loaded, the
45 # $INC{inc/Module/Install.pm} is what will still get set as long as
46 # the caller loaded module this in the documented manner.
47 # If not set, the caller may NOT have loaded the bundled version, and thus
48 # they may not have a MI version that works with the Makefile.PL. This would
49 # result in false errors or unexpected behaviour. And we don't want that.
50 my $file = join( '/', 'inc', split /::/, __PACKAGE__ ) . '.pm';
51 unless ( $INC{$file} ) { die <<"END_DIE" }
53 Please invoke ${\__PACKAGE__} with:
55 use inc::${\__PACKAGE__};
67 # If the script that is loading Module::Install is from the future,
68 # then make will detect this and cause it to re-run over and over
69 # again. This is bad. Rather than taking action to touch it (which
70 # is unreliable on some platforms and requires write permissions)
71 # for now we should catch this and refuse to run.
73 my $s = (stat($0))[9];
75 # If the modification time is only slightly in the future,
76 # sleep briefly to remove the problem.
78 if ( $a > 0 and $a < 5 ) { sleep 5 }
80 # Too far in the future, throw an error.
82 if ( $s > $t ) { die <<"END_DIE" }
84 Your installer $0 has a modification time in the future ($s > $t).
86 This is known to create infinite loops in make.
88 Please correct this, then run $0 again.
97 # Build.PL was formerly supported, but no longer is due to excessive
98 # difficulty in implementing every single feature twice.
99 if ( $0 =~ /Build.PL$/i ) { die <<"END_DIE" }
101 Module::Install no longer supports Build.PL.
103 It was impossible to maintain duel backends, and has been deprecated.
105 Please remove all Build.PL files and only use the Makefile.PL installer.
113 # To save some more typing in Module::Install installers, every...
114 # use inc::Module::Install
115 # ...also acts as an implicit use strict.
116 $^H |= strict::bits(qw(refs subs vars));
129 my $who = $self->_caller;
130 my $cwd = Cwd::cwd();
131 my $sym = "${who}::AUTOLOAD";
133 my $pwd = Cwd::cwd();
134 if ( my $code = $sym->{$pwd} ) {
135 # Delegate back to parent dirs
136 goto &$code unless $cwd eq $pwd;
138 $$sym =~ /([^:]+)$/ or die "Cannot autoload $who - $sym";
140 if ( uc($method) eq $method ) {
143 } elsif ( $method =~ /^_/ and $self->can($method) ) {
144 # Dispatch to the root M:I class
145 return $self->$method(@_);
148 # Dispatch to the appropriate plugin
149 unshift @_, ( $self, $1 );
150 goto &{$self->can('call')};
156 my $self = $class->new(@_);
157 my $who = $self->_caller;
159 unless ( -f $self->{file} ) {
160 require "$self->{path}/$self->{dispatch}.pm";
161 File::Path::mkpath("$self->{prefix}/$self->{author}");
162 $self->{admin} = "$self->{name}::$self->{dispatch}"->new( _top => $self );
163 $self->{admin}->init;
164 @_ = ($class, _self => $self);
165 goto &{"$self->{name}::import"};
168 *{"${who}::AUTOLOAD"} = $self->autoload;
171 # Unregister loader and worker packages so subdirs can use them again
172 delete $INC{"$self->{file}"};
173 delete $INC{"$self->{path}.pm"};
175 # Save to the singleton
183 unless ( $self->{extensions} ) {
184 $self->load_extensions(
185 "$self->{prefix}/$self->{path}", $self
189 my @exts = @{$self->{extensions}};
191 @exts = $self->{admin}->load_all_extensions;
195 foreach my $obj ( @exts ) {
196 while (my ($method, $glob) = each %{ref($obj) . '::'}) {
197 next unless $obj->can($method);
198 next if $method =~ /^_/;
199 next if $method eq uc($method);
204 my $who = $self->_caller;
205 foreach my $name ( sort keys %seen ) {
206 *{"${who}::$name"} = sub {
207 ${"${who}::AUTOLOAD"} = "${who}::$name";
208 goto &{"${who}::AUTOLOAD"};
214 my ($class, %args) = @_;
216 # ignore the prefix on extension modules built from top level.
217 my $base_path = Cwd::abs_path($FindBin::Bin);
218 unless ( Cwd::abs_path(Cwd::cwd()) eq $base_path ) {
219 delete $args{prefix};
222 return $args{_self} if $args{_self};
224 $args{dispatch} ||= 'Admin';
225 $args{prefix} ||= 'inc';
226 $args{author} ||= ($^O eq 'VMS' ? '_author' : '.author');
227 $args{bundle} ||= 'inc/BUNDLES';
228 $args{base} ||= $base_path;
229 $class =~ s/^\Q$args{prefix}\E:://;
230 $args{name} ||= $class;
231 $args{version} ||= $class->VERSION;
232 unless ( $args{path} ) {
233 $args{path} = $args{name};
234 $args{path} =~ s!::!/!g;
236 $args{file} ||= "$args{base}/$args{prefix}/$args{path}.pm";
239 bless( \%args, $class );
243 my ($self, $method) = @_;
244 my $obj = $self->load($method) or return;
245 splice(@_, 0, 2, $obj);
246 goto &{$obj->can($method)};
250 my ($self, $method) = @_;
252 $self->load_extensions(
253 "$self->{prefix}/$self->{path}", $self
254 ) unless $self->{extensions};
256 foreach my $obj (@{$self->{extensions}}) {
257 return $obj if $obj->can($method);
260 my $admin = $self->{admin} or die <<"END_DIE";
261 The '$method' method does not exist in the '$self->{prefix}' path!
262 Please remove the '$self->{prefix}' directory and run $0 again to load it.
265 my $obj = $admin->load($method, 1);
266 push @{$self->{extensions}}, $obj;
271 sub load_extensions {
272 my ($self, $path, $top) = @_;
274 unless ( grep { ! ref $_ and lc $_ eq lc $self->{prefix} } @INC ) {
275 unshift @INC, $self->{prefix};
278 foreach my $rv ( $self->find_extensions($path) ) {
279 my ($file, $pkg) = @{$rv};
280 next if $self->{pathnames}{$pkg};
283 my $new = eval { require $file; $pkg->can('new') };
288 $self->{pathnames}{$pkg} = delete $INC{$file};
289 push @{$self->{extensions}}, &{$new}($pkg, _top => $top );
292 $self->{extensions} ||= [];
295 sub find_extensions {
296 my ($self, $path) = @_;
299 File::Find::find( sub {
300 my $file = $File::Find::name;
301 return unless $file =~ m!^\Q$path\E/(.+)\.pm\Z!is;
303 return if lc($subpath) eq lc($self->{dispatch});
305 $file = "$self->{path}/$subpath.pm";
306 my $pkg = "$self->{name}::$subpath";
309 # If we have a mixed-case package name, assume case has been preserved
310 # correctly. Otherwise, root through the file to locate the case-preserved
311 # version of the package name.
312 if ( $subpath eq lc($subpath) || $subpath eq uc($subpath) ) {
313 my $content = Module::Install::_read($subpath . '.pm');
315 foreach ( split //, $content ) {
316 $in_pod = 1 if /^=\w/;
317 $in_pod = 0 if /^=cut/;
318 next if ($in_pod || /^=cut/); # skip pod text
319 next if /^\s*#/; # and comments
320 if ( m/^\s*package\s+($pkg)\s*;/i ) {
327 push @found, [ $file, $pkg ];
328 }, $path ) if -d $path;
337 #####################################################################
338 # Common Utility Functions
342 my $call = caller($depth);
343 while ( $call eq __PACKAGE__ ) {
345 $call = caller($depth);
353 open( FH, '<', $_[0] ) or die "open($_[0]): $!";
355 open( FH, "< $_[0]" ) or die "open($_[0]): $!";
357 my $string = do { local $/; <FH> };
358 close FH or die "close($_[0]): $!";
363 my $string = Module::Install::_read($_[0]);
364 $string =~ s/(?:\015{1,2}\012|\015|\012)/\n/sg;
365 $string =~ s/(\n)\n*__(?:DATA|END)__\b.*\z/$1/s;
366 $string =~ s/\n\n=\w+.+?\n\n=cut\b.+?\n+/\n\n/sg;
371 my $string = Module::Install::_read($_[0]);
372 $string =~ s/(?:\015{1,2}\012|\015|\012)/\n/sg;
373 return $string if $_[0] =~ /\.pod\z/;
374 $string =~ s/(^|\n=cut\b.+?\n+)[^=\s].+?\n(\n=\w+|\z)/$1$2/sg;
375 $string =~ s/\n*=pod\b[^\n]*\n+/\n\n/sg;
376 $string =~ s/\n*=cut\b[^\n]*\n+/\n\n/sg;
377 $string =~ s/^\n+//s;
384 open( FH, '>', $_[0] ) or die "open($_[0]): $!";
386 open( FH, "> $_[0]" ) or die "open($_[0]): $!";
388 foreach ( 1 .. $#_ ) {
389 print FH $_[$_] or die "print($_[0]): $!";
391 close FH or die "close($_[0]): $!";
394 # _version is for processing module versions (eg, 1.03_05) not
395 # Perl versions (eg, 5.8.1).
398 my $d =()= $s =~ /(\.)/g;
400 # Normalise multipart versions
401 $s =~ s/(\.)(\d{1,3})/sprintf("$1%03d",$2)/eg;
406 $_ . '0' x (3 - length $_)
407 } $s =~ /(\d{1,3})\D?/g;
408 $l = $l . '.' . join '', @v if @v;
413 _version($_[0]) <=> _version($_[1]);
416 # Cloned from Params::Util::_CLASS
423 $_[0] =~ m/^[^\W\d]\w*(?:::\w+)*\z/s
429 # Copyright 2008 - 2009 Adam Kennedy.