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
25 use vars qw{$VERSION};
27 # All Module::Install core packages now require synchronised versions.
28 # This will be used to ensure we don't accidentally load old or
29 # different versions of modules.
30 # This is not enforced yet, but will be some time in the next few
31 # releases once we can make sure it won't clash with custom
32 # Module::Install extensions.
40 # Whether or not inc::Module::Install is actually loaded, the
41 # $INC{inc/Module/Install.pm} is what will still get set as long as
42 # the caller loaded module this in the documented manner.
43 # If not set, the caller may NOT have loaded the bundled version, and thus
44 # they may not have a MI version that works with the Makefile.PL. This would
45 # result in false errors or unexpected behaviour. And we don't want that.
46 my $file = join( '/', 'inc', split /::/, __PACKAGE__ ) . '.pm';
47 unless ( $INC{$file} ) { die <<"END_DIE" }
49 Please invoke ${\__PACKAGE__} with:
51 use inc::${\__PACKAGE__};
63 # If the script that is loading Module::Install is from the future,
64 # then make will detect this and cause it to re-run over and over
65 # again. This is bad. Rather than taking action to touch it (which
66 # is unreliable on some platforms and requires write permissions)
67 # for now we should catch this and refuse to run.
68 if ( -f $0 and (stat($0))[9] > time ) { die <<"END_DIE" }
70 Your installer $0 has a modification time in the future.
72 This is known to create infinite loops in make.
74 Please correct this, then run $0 again.
82 # Build.PL was formerly supported, but no longer is due to excessive
83 # difficulty in implementing every single feature twice.
84 if ( $0 =~ /Build.PL$/i or -f 'Build.PL' ) { die <<"END_DIE" }
86 Module::Install no longer supports Build.PL.
88 It was impossible to maintain duel backends, and has been deprecated.
90 Please remove all Build.PL files and only use the Makefile.PL installer.
103 *inc::Module::Install::VERSION = *VERSION;
104 @inc::Module::Install::ISA = __PACKAGE__;
108 my $who = $self->_caller;
109 my $cwd = Cwd::cwd();
110 my $sym = "${who}::AUTOLOAD";
112 my $pwd = Cwd::cwd();
113 if ( my $code = $sym->{$pwd} ) {
114 # delegate back to parent dirs
115 goto &$code unless $cwd eq $pwd;
117 $$sym =~ /([^:]+)$/ or die "Cannot autoload $who - $sym";
118 unshift @_, ( $self, $1 );
119 goto &{$self->can('call')} unless uc($1) eq $1;
125 my $self = $class->new(@_);
126 my $who = $self->_caller;
128 unless ( -f $self->{file} ) {
129 require "$self->{path}/$self->{dispatch}.pm";
130 File::Path::mkpath("$self->{prefix}/$self->{author}");
131 $self->{admin} = "$self->{name}::$self->{dispatch}"->new( _top => $self );
132 $self->{admin}->init;
133 @_ = ($class, _self => $self);
134 goto &{"$self->{name}::import"};
137 *{"${who}::AUTOLOAD"} = $self->autoload;
140 # Unregister loader and worker packages so subdirs can use them again
141 delete $INC{"$self->{file}"};
142 delete $INC{"$self->{path}.pm"};
149 unless ( $self->{extensions} ) {
150 $self->load_extensions(
151 "$self->{prefix}/$self->{path}", $self
155 my @exts = @{$self->{extensions}};
157 my $admin = $self->{admin};
158 @exts = $admin->load_all_extensions;
162 foreach my $obj ( @exts ) {
163 while (my ($method, $glob) = each %{ref($obj) . '::'}) {
164 next unless $obj->can($method);
165 next if $method =~ /^_/;
166 next if $method eq uc($method);
171 my $who = $self->_caller;
172 foreach my $name ( sort keys %seen ) {
173 *{"${who}::$name"} = sub {
174 ${"${who}::AUTOLOAD"} = "${who}::$name";
175 goto &{"${who}::AUTOLOAD"};
181 my ($class, %args) = @_;
183 # ignore the prefix on extension modules built from top level.
184 my $base_path = Cwd::abs_path($FindBin::Bin);
185 unless ( Cwd::abs_path(Cwd::cwd()) eq $base_path ) {
186 delete $args{prefix};
189 return $args{_self} if $args{_self};
191 $args{dispatch} ||= 'Admin';
192 $args{prefix} ||= 'inc';
193 $args{author} ||= ($^O eq 'VMS' ? '_author' : '.author');
194 $args{bundle} ||= 'inc/BUNDLES';
195 $args{base} ||= $base_path;
196 $class =~ s/^\Q$args{prefix}\E:://;
197 $args{name} ||= $class;
198 $args{version} ||= $class->VERSION;
199 unless ( $args{path} ) {
200 $args{path} = $args{name};
201 $args{path} =~ s!::!/!g;
203 $args{file} ||= "$args{base}/$args{prefix}/$args{path}.pm";
206 bless( \%args, $class );
210 my ($self, $method) = @_;
211 my $obj = $self->load($method) or return;
212 splice(@_, 0, 2, $obj);
213 goto &{$obj->can($method)};
217 my ($self, $method) = @_;
219 $self->load_extensions(
220 "$self->{prefix}/$self->{path}", $self
221 ) unless $self->{extensions};
223 foreach my $obj (@{$self->{extensions}}) {
224 return $obj if $obj->can($method);
227 my $admin = $self->{admin} or die <<"END_DIE";
228 The '$method' method does not exist in the '$self->{prefix}' path!
229 Please remove the '$self->{prefix}' directory and run $0 again to load it.
232 my $obj = $admin->load($method, 1);
233 push @{$self->{extensions}}, $obj;
238 sub load_extensions {
239 my ($self, $path, $top) = @_;
241 unless ( grep { lc $_ eq lc $self->{prefix} } @INC ) {
242 unshift @INC, $self->{prefix};
245 foreach my $rv ( $self->find_extensions($path) ) {
246 my ($file, $pkg) = @{$rv};
247 next if $self->{pathnames}{$pkg};
250 my $new = eval { require $file; $pkg->can('new') };
255 $self->{pathnames}{$pkg} = delete $INC{$file};
256 push @{$self->{extensions}}, &{$new}($pkg, _top => $top );
259 $self->{extensions} ||= [];
262 sub find_extensions {
263 my ($self, $path) = @_;
266 File::Find::find( sub {
267 my $file = $File::Find::name;
268 return unless $file =~ m!^\Q$path\E/(.+)\.pm\Z!is;
270 return if lc($subpath) eq lc($self->{dispatch});
272 $file = "$self->{path}/$subpath.pm";
273 my $pkg = "$self->{name}::$subpath";
276 # If we have a mixed-case package name, assume case has been preserved
277 # correctly. Otherwise, root through the file to locate the case-preserved
278 # version of the package name.
279 if ( $subpath eq lc($subpath) || $subpath eq uc($subpath) ) {
280 my $content = Module::Install::_read($subpath . '.pm');
282 foreach ( split //, $content ) {
283 $in_pod = 1 if /^=\w/;
284 $in_pod = 0 if /^=cut/;
285 next if ($in_pod || /^=cut/); # skip pod text
286 next if /^\s*#/; # and comments
287 if ( m/^\s*package\s+($pkg)\s*;/i ) {
294 push @found, [ $file, $pkg ];
295 }, $path ) if -d $path;
304 #####################################################################
309 my $call = caller($depth);
310 while ( $call eq __PACKAGE__ ) {
312 $call = caller($depth);
319 open FH, "< $_[0]" or die "open($_[0]): $!";
320 my $str = do { local $/; <FH> };
321 close FH or die "close($_[0]): $!";
327 open FH, "> $_[0]" or die "open($_[0]): $!";
328 foreach ( 1 .. $#_ ) { print FH $_[$_] or die "print($_[0]): $!" }
329 close FH or die "close($_[0]): $!";
336 my @v = map { $_ . '0' x (3 - length $_) } $s =~ /(\d{1,3})\D?/g;
337 $l = $l . '.' . join '', @v if @v;
343 # Copyright 2008 Adam Kennedy.