lib/Module/Install/Admin.pm

filter: Statementstmt 69.0% 87 / 126 Branchbran 50.0% 30 / 60 Conditioncond 45.8% 11 / 24 MC/DCmcdc 26.6% 4 / 15 Subroutinesub 83.3% 15 / 18 Podpod 0.0% 0 / 8 totaltotal 58.5% 147 / 251 CC 45 SCAR 34.6
CC
45
Cov
61%
delete_package
132.0
pick
34.2
copy
11.2
CRAP 31.8
« lib/Module/Install/Bundle.pm ↑ Summary lib/Module/Install/Scripts.pm »
1 package Module::Install::Admin;
2  
3 48 ▶use strict 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 48 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 48 ▶use inc::Module::Install ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 48 ▶use vars qw{$VERSION @ISA};
Subroutine BEGIN: called CC=1 SCAR=0.0
8 48 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 48 $VERSION = '1.21';
10 48 @ISA = 'Module::Install';
11 }
12  
13 =pod
14  
15 =head1 NAME
16  
17 Module::Install::Admin - Author-side manager for Module::Install
18  
19 =head1 SYNOPSIS
20  
21 In a B<Module::Install> extension module:
22  
23 sub extension_method {
24 my $self = shift;
25 $self->admin->some_method(@args);
26 }
27  
28 As an one-liner:
29  
30 % perl "-MModule::Install::Admin" -e'&some_method(@args);'
31  
32 The two snippets above are really shorthands for
33  
34 $some_obj->some_method(@args)
35  
36 where C<$some_obj> is the singleton object of a class under the
37 C<Module::Install::Admin::*> namespace that provides the method
38 C<some_method>. See L</METHODS> for a list of built-in methods.
39  
40 =head1 DESCRIPTION
41  
42 This module implements the internal mechanism for initializing,
43 including and managing extensions, and should only be of interest to
44 extension developers; it is I<never> included under a distribution's
45 F<inc/> directory, nor are any of the B<Module::Install::Admin::*>
46 extensions.
47  
48 For normal usage of B<Module::Install>, please see L<Module::Install>
49 and L<Module::Install/"COOKBOOK / EXAMPLES"> instead.
50  
51 =head2 Bootstrapping
52  
53 When someone runs a F<Makefile.PL> that has C<use inc::Module::Install>,
54 and there is no F<inc/> in the current directory, B<Module::Install>
55 will load this module bootstrap itself, through the steps below:
56  
57 =over 4
58  
59 =item *
60  
61 First, F<Module/Install.pm> is POD-stripped and copied from C<@INC> to
62 F<inc/>. This should only happen on the author's side, never on the
63 end-user side.
64  
65 =item *
66  
67 Reload F<inc/Module/Install.pm> if the current file is somewhere else.
68 This ensures that the included version of F<inc/Module/Install.pm> is
69 always preferred over the installed version.
70  
71 =item *
72  
73 Look at F<inc/Module/Install/*.pm> and load all of them.
74  
75 =item *
76  
77 Set up a C<main::AUTOLOAD> function to delegate missing function calls
78 to C<Module::Install::Admin::load> -- again, this should only happen
79 at the author's side.
80  
81 =item *
82  
83 Provide a C<Module::Install::purge_self> function for removing included
84 files under F<inc/>.
85  
86 =back
87  
88 =head1 METHODS
89  
90 =cut
91  
92 2 ▶sub import {
Subroutine import: called CC=1 SCAR=2.0
93 2 my $class = shift;
94 2 my $self = $class->new( _top => Module::Install->new, @_ );
95 2 local $^W;
96 2 ▶ *{caller(0) . "::AUTOLOAD"} = sub {
Subroutine __ANON__: not called CC=4 SCAR=23.7
97 48 ▶ no strict 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
98 0 ▶ $AUTOLOAD =~ /([^:]+)$/ or die "Cannot load";
Branch: 1 branch
BranchTrueFalseTotal
$AUTOLOAD =~ /([^:]+)$/ or die "Cannot load" 0 0 0
99 0 ▶ return if uc($1) eq $1;
Branch: 1 branch
BranchTrueFalseTotal
if uc $1 eq $1 0 0 0
100 0 ▶ my $obj = $self->load($1) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $obj = $self->load($1) or return 0 0 0
101 0 unshift @_, $obj;
102 0 goto &{$obj->can($1)};
103 2 };
104 }
105  
106 47 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
107 47 my ($class, %args) = @_;
108 return $class->SUPER::new(
109 47 %{$args{_top}}, %args,
110 extensions => undef,
111 pathnames => undef,
112 );
113 }
114  
115 45 ▶sub init {
Subroutine init: called CC=2 SCAR=7.1
Pod undocumented
116 45 my $self = shift;
117 45 $self->copy($INC{"$self->{path}.pm"} => $self->{file});
118  
119 45 ▶ unless ( grep { $_ eq $self->{prefix} } @INC ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $self->{"prefix"};} @INC) 0 45 45
120 0 unshift @INC, $self->{prefix};
121 }
122 45 delete $INC{"$self->{path}.pm"};
123  
124 45 local $^W;
125 45 do "$self->{path}.pm";
126 }
127  
128 350 ▶sub copy {
Subroutine copy: called CC=11 SCAR=24.2
Pod undocumented
129 350 my ($self, $from, $to) = @_;
130  
131 350 my @parts = split('/', $to);
132 350 ▶ File::Path::mkpath([ join('/', @parts[ 0 .. $#parts-1 ])])
Branch: 1 branch
BranchTrueFalseTotal
if @parts > 1 349 1 350
133 if @parts > 1;
134  
135 350 chomp $to;
136  
137 350 local ($_);
138 350 ▶ open my $FROM, "<", $from or die "Can't open $from for input:\n$!";
Branch: 1 branch
BranchTrueFalseTotal
open my $FROM, "<", $from or die "Can't open $from for input:\n$!" 350 0 350
139 350 ▶ open my $TO, ">", $to or die "Can't open $to for output:\n$!";
Branch: 1 branch
BranchTrueFalseTotal
open my $TO, ">", $to or die "Can't open $to for output:\n$!" 350 0 350
140 350 binmode $FROM;
141 350 binmode $TO;
142 350 print $TO "#line 1\n";
143  
144 350 my $content;
145 my $in_pod;
146  
147 350 ▶ while ( <$FROM> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $FROM)) 103756 350 104106
148 103756 ▶ if ( /^=(?:b(?:egin|ack)|head\d|(?:po|en)d|item|(?:ove|fo)r)/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if (/^=(?:b(?:egin|ack)|head\d|(?:po|en)d|item|(?:ove|fo)r)/) { } 2541 101215 103756
elsif (/^=cut\s*\z/ and $in_pod) { } 374 100841 101215
elsif (not $in_pod) { } 89548 11293 100841
149 2541 $in_pod = 1;
150 ▶ } elsif ( /^=cut\s*\z/ and $in_pod ) {
Condition66%2/3
/^=cut\s*\z/ and $in_pod
!ll&&!rl&&r
1008410374
MC/DC50%1/2
/^=cut\s*\z/ and $in_pod
/^=cut\s*\z/ $in_pod
Truth table66%2/3
A and B
A= /^=cut\s*\z/B= $in_pod
ABresult
0X0
100
111
151 374 $in_pod = 0;
152 374 print $TO "#line $.\n";
153 } elsif ( ! $in_pod ) {
154 89548 print $TO $_;
155 }
156 }
157  
158 350 ▶ close $FROM or die "Can't close $from for input:\n$!";
Branch: 1 branch
BranchTrueFalseTotal
close $FROM or die "Can't close $from for input:\n$!" 350 0 350
159 350 ▶ close $TO or die "Can't close $to for output:\n$!";
Branch: 1 branch
BranchTrueFalseTotal
close $TO or die "Can't close $to for output:\n$!" 350 0 350
160  
161 350 print "include $to\n";
162 }
163  
164 # scan through our target to find
165 469 ▶sub load_all_extensions {
Subroutine load_all_extensions: called CC=5 SCAR=16.1
Pod undocumented
166 469 my $self = shift;
167 469 ▶ unless ($self->{extensions}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"extensions"}) 45 424 469
168 45 $self->{extensions} = [];
169 45 foreach my $inc (@INC) {
170 450 ▶ next if ref($inc) or $inc eq $self->{prefix};
Branch: 1 branch
BranchTrueFalseTotal
if ref $inc or $inc eq $self->{"prefix"} 45 405 450
Condition66%2/3
ref $inc or $inc eq $self->{"prefix"}
l!l&&r!l&&!r
045405
MC/DC50%1/2
ref $inc or $inc eq $self->{"prefix"}
ref $inc $inc eq $self->{"prefix"}
Truth table66%2/3
A or B
A= ref $incB= $inc eq $self->{"prefix"}
ABresult
1X1
011
000
171 405 $self->load_extensions("$inc/$self->{path}", $self->{_top});
172 }
173 }
174 469 return @{$self->{extensions}};
175 }
176  
177 424 ▶sub load {
Subroutine load: called CC=7 SCAR=19.5
Pod undocumented
178 424 my ($self, $method, $copy) = @_;
179  
180 424 my @extobj;
181 424 foreach my $obj ($self->load_all_extensions) {
182 12296 ▶ next unless defined &{ref($obj)."::$method"};
Branch: 1 branch
BranchTrueFalseTotal
unless defined &{ref($obj) . "::$method";} 11747 549 12296
183 549 my $is_admin = (ref($obj) =~ /^\Q$self->{name}::$self->{dispatch}::/);
184 # Don't ever include admin modules, and vice versa.
185 # $copy = 0 if $XXX and $is_admin;
186 549 ▶ push @extobj, $obj if $copy xor $is_admin;
Branch: 1 branch
BranchTrueFalseTotal
if $copy xor $is_admin 424 125 549
Condition100%4/4
$copy xor $is_admin
l&&rl&&!r!l&&r!l&&!r
4326016482
MC/DC100%2/2
$copy xor $is_admin
$copy $is_admin
Truth table100%4/4
A xor B
A= $copyB= $is_admin
ABresult
110
101
011
000
187 }
188 424 ▶ unless ( @extobj ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@extobj) 0 424 424
189 0 die "Cannot find an extension with method '$method'";
190 }
191  
192 # XXX - do we need to reload $obj from the new location?
193 424 my $obj = $self->pick($method, \@extobj);
194 424 ▶ $self->copy_package(ref($obj)) if $copy;
Branch: 1 branch
BranchTrueFalseTotal
if $copy 260 164 424
195  
196 424 return $obj;
197 }
198  
199 # Copy a package to inc/, with its @ISA tree. $pathname is optional.
200 521 ▶sub copy_package {
Subroutine copy_package: called CC=5 SCAR=16.2
Pod undocumented
201 521 my ($self, $pkg, $pathname) = @_;
202 521 ▶ return unless ($pathname ||= $self->{pathnames}{$pkg});
Branch: 1 branch
BranchTrueFalseTotal
unless $pathname ||= $self->{"pathnames"}{$pkg} 0 521 521
Condition66%2/3
$pathname ||= $self->{"pathnames"}{$pkg}
l!l&&r!l&&!r
15200
MC/DC0%0/2
$pathname ||= $self->{"pathnames"}{$pkg}
$pathname $self->{"pathnames"}{$pkg}
Truth table66%2/3
A ||= B
A= $pathnameB= $self->{"pathnames"}{$pkg}
ABresult
1X1
011
000
203  
204 521 my $file = $pkg; $file =~ s!::!/!g;
205 521 $file = "$self->{prefix}/$file.pm";
206 521 ▶ return if -f $file; # prevents infinite recursion
Branch: 1 branch
BranchTrueFalseTotal
if -f $file 217 304 521
207  
208 304 $self->copy($pathname => $file);
209 304 foreach my $pkg (@{"$pkg\::ISA"}) {
210 260 $self->copy_package($pkg);
211 }
212 }
213  
214 424 ▶sub pick {
Subroutine pick: called CC=7 SCAR=35.3
Pod undocumented
215 # determine which name to load
216 424 my ($self, $method, $objects) = @_;
217  
218 # XXX this whole thing needs to be discussed
219 424 ▶ return $objects->[0] unless $#{$objects} > 0 and -t STDIN;
Branch: 1 branch
BranchTrueFalseTotal
unless $#{$objects;} > 0 and -t STDIN 424 0 424
Condition33%1/3
$#{$objects;} > 0 and -t STDIN
!ll&&!rl&&r
42400
MC/DC0%0/2
$#{$objects;} > 0 and -t STDIN
$#{$objects;} > 0 -t STDIN
Truth table33%1/3
A and B
A= $#{$objects;} > 0B= -t STDIN
ABresult
0X0
100
111
220  
221 # sort by last modified time
222 0 @$objects = map { $_->[0] }
223 0 sort { $a->[1] <=> $b->[1] }
224 0 map { [ $_ => -M $self->{pathnames}{ref($_)} ] } @$objects;
225  
226 0 print "Multiple extensions found for method '$method':\n";
227 0 foreach my $i ( 1 .. @$objects ) {
228 0 print "\t$i. ", ref($objects->[$i-1]), "\n";
229 }
230  
231 0 while ( 1 ) {
232 0 print "Please select one [1]: ";
233 0 chomp(my $choice = <STDIN>);
234 0 ▶ $choice ||= 1;
Condition0%0/2
$choice ||= 1
l!l
00
MC/DC0%0/1
$choice ||= 1
$choice
Truth table0%0/2
A
A= $choice
Aresult
11
00
235 0 ▶ return $objects->[$choice-1] if $choice > 0 and $choice <= @$objects;
Branch: 1 branch
BranchTrueFalseTotal
if $choice > 0 and $choice <= @$objects 0 0 0
Condition0%0/3
$choice > 0 and $choice <= @$objects
!ll&&!rl&&r
000
MC/DC0%0/2
$choice > 0 and $choice <= @$objects
$choice > 0 $choice <= @$objects
Truth table0%0/3
A and B
A= $choice > 0B= $choice <= @$objects
ABresult
0X0
100
111
236 0 print "Invalid choice. ";
237 }
238 }
239  
240 0 ▶sub delete_package {
Subroutine delete_package: not called CC=11 SCAR=48.8
Pod undocumented
241 0 my ($self, $pkg) = @_;
242  
243 # expand to full symbol table name if needed
244 0 ▶ unless ( $pkg =~ /^main::.*::$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pkg =~ /^main::.*::$/) 0 0 0
245 0 ▶ $pkg = "main$pkg" if $pkg =~ /^::/;
Branch: 1 branch
BranchTrueFalseTotal
if $pkg =~ /^::/ 0 0 0
246 0 ▶ $pkg = "main::$pkg" unless $pkg =~ /^main::/;
Branch: 1 branch
BranchTrueFalseTotal
unless $pkg =~ /^main::/ 0 0 0
247 0 ▶ $pkg .= '::' unless $pkg =~ /::$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $pkg =~ /::$/ 0 0 0
248 }
249  
250 0 my($stem, $leaf) = $pkg =~ m/(.*::)(\w+::)$/;
251 0 my $stem_symtab = *{$stem}{HASH};
252 0 ▶ return unless defined $stem_symtab and exists $stem_symtab->{$leaf};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stem_symtab and exists $stem_symtab->{$leaf} 0 0 0
Condition0%0/3
defined $stem_symtab and exists $stem_symtab->{$leaf}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $stem_symtab and exists $stem_symtab->{$leaf}
defined $stem_symtab exists $stem_symtab->{$leaf}
Truth table0%0/3
A and B
A= defined $stem_symtabB= exists $stem_symtab->{$leaf}
ABresult
0X0
100
111
253  
254 # free all the symbols in the package
255 0 my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH};
256 0 foreach my $name (keys %$leaf_symtab) {
257 0 ▶ next if $name eq "$self->{dispatch}::";
Branch: 1 branch
BranchTrueFalseTotal
if $name eq qq[$self->{"dispatch"}::] 0 0 0
258 0 undef *{$pkg . $name};
259 }
260  
261 # delete the symbol table
262 0 foreach my $name (keys %$leaf_symtab) {
263 0 ▶ next if $name eq "$self->{dispatch}::";
Branch: 1 branch
BranchTrueFalseTotal
if $name eq qq[$self->{"dispatch"}::] 0 0 0
264 0 delete $leaf_symtab->{$name};
265 }
266 }
267  
268 164 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=1 SCAR=0.0
269 164 goto &{shift->autoload};
270 }
271  
272 0 ▶sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
273  
274 48 1;
275  
276 __END__
« lib/Module/Install/Bundle.pm ↑ Summary lib/Module/Install/Scripts.pm »