blib/lib/Find/Lib.pm

filter: Statementstmt 97.2% 71 / 73 Branchbran 92.3% 24 / 26 Conditioncond 62.5% 15 / 24 MC/DCmcdc 40.0% 6 / 15 Subroutinesub 100% 16 / 16 Podpod 37.5% 3 / 8 totaltotal 83.3% 135 / 162 SCAR 31.6
CC
23
Cov
89%
import
13.4
shell_resolve
7.1
guess_base
3.0
CRAP 23.6
↑ Summary /root/.cpan/build/Find-Lib-1.03_01-0/t/mylib/MyLibNoTest.pm »
1 package Find::Lib;
2 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use lib;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 16 ▶use File::Spec();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use vars qw/$Base $VERSION @base/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use vars qw/$Script/; # compat
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Find::Lib - Helper to smartly find libs to use in the filesystem tree
13  
14 =head1 VERSION
15  
16 Version 1.01
17  
18 =cut
19  
20 16 $VERSION = '1.03_01';
21  
22 =head1 SYNOPSIS
23  
24 #!/usr/bin/perl -w;
25 use strict;
26  
27 ## simple usage
28 use Find::Lib '../mylib';
29  
30 ## more libraries
31 use Find::Lib '../mylib', 'local-lib';
32  
33 ## More verbose and backward compatible with Find::Lib < 1.0
34 use Find::Lib libs => [ 'lib', '../lib', 'devlib' ];
35  
36 ## resolve some path with minimum typing
37 $dir = Find::Lib->catdir("..", "data");
38 $path = Find::Lib->catfile("..", "data", "test.yaml");
39  
40 $base = Find::Lib->base;
41 # or
42 $base = Find::Lib::Base;
43  
44 =head1 DESCRIPTION
45  
46 The purpose of this module is to replace
47  
48 use FindBin;
49 use lib "$FindBin::Bin/../bootstrap/lib";
50  
51 with something shorter. This is specially useful if your project has a lot
52 of scripts (For instance tests scripts).
53  
54 use Find::Lib '../bootstrap/lib';
55  
56 The important differences between L<FindBin> and L<Find::Lib> are:
57  
58 =over 4
59  
60 =item * symlinks and '..'
61  
62 If you have symlinks in your path it respects them, so basically you can forget
63 you have symlinks, because Find::Lib will do the natural thing (NOT ignore
64 them), and resolve '..' correctly. L<FindBin> breaks if you do:
65  
66 use lib "$Bin/../lib";
67  
68 and you currently are in a symlinked directory, because $Bin resolved to the
69 filesystem path (without the symlink) and not the shell path.
70  
71 =item * convenience
72  
73 it's faster too type, and more intuitive (Exporting C<$Bin> always
74 felt weird to me).
75  
76 =back
77  
78 =head1 DISCUSSION
79  
80 =head2 Installation and availability of this module
81  
82 The usefulness of this module is seriously reduced if L<Find::Lib> is not
83 already in your @INC / $ENV{PERL5LIB} -- Chicken and egg problem. This is
84 the big disavantage of L<FindBin> over L<Find::Lib>: FindBin is distributed
85 with Perl. To mitigate that, you need to be sure of global availability of
86 the module in the system (You could install it via your favorite package
87 managment system for instance).
88  
89 =head2 modification of $0 and chdir (BEGIN blocks, other 'use')
90  
91 As soon as L<Find::Lib> is compiled it saves the location of the script and
92 the initial cwd (current working directory), which are the two pieces of
93 information the module relies on to interpret the relative path given by the
94 calling program.
95  
96 If one of cwd, $ENV{PWD} or $0 is changed before Find::Lib has a chance to do
97 its job, then Find::Lib will most probably die, saying "The script cannot be
98 found". I don't know a workaround that. So be sure to load Find::Lib as soon
99 as possible in your script to minimize problems (you are in control!).
100  
101 (some programs alter $0 to customize the diplay line of the process in
102 the system process-list (C<ps> on unix).
103  
104 (Note, see L<perlvar> for explanation of $0)
105  
106 =head1 USAGE
107  
108 =head2 import
109  
110 All the work is done in import. So you need to C<'use Find::Lib'> and pass
111 a list of paths to add to @INC. See L<BACKWARD COMPATIBILITY> section for
112 more retails on this topic.
113  
114 The paths given are (should) be relative to the location of the current script.
115 The paths won't be added unless the path actually exists on disk
116  
117 =cut
118  
119 16 ▶use Carp();
Subroutine BEGIN: called CC=1 SCAR=0.0
120  
121 16 $Script = $Base = guess_base();
122  
123 sub guess_base {
124 16 ▶ my $base;
Subroutine guess_base: called CC=3 SCAR=11.0
Pod undocumented
125 16 $base = guess_shell_path();
126 16 ▶ return $base if $base && -e $base;
Branch: 1 branch
BranchTrueFalseTotal
if $base and -e $base 14 2 16
Condition66%2/3
$base and -e $base
!ll&&!rl&&r
0214
MC/DC50%1/2
$base and -e $base
$base -e $base
Truth table66%2/3
A and B
A= $baseB= -e $base
ABresult
0X0
100
111
127 2 return guess_system_path();
128 }
129  
130 ## we want to use PWD if it exists (it's not guaranteed on all platforms)
131 ## so that we have a sense of the shell current working dir, with unresolved
132 ## symlinks
133 sub guess_pwd {
134 16 ▶ return $ENV{PWD} || Cwd::cwd();
Subroutine guess_pwd: called CC=2 SCAR=9.2
Pod undocumented
Condition33%1/3
$ENV{'PWD'} || Cwd::cwd()
l!l&&r!l&&!r
1600
MC/DC0%0/2
$ENV{'PWD'} || Cwd::cwd()
$ENV{'PWD'} Cwd::cwd()
Truth table33%1/3
A || B
A= $ENV{'PWD'}B= Cwd::cwd()
ABresult
1X1
011
000
135 }
136  
137 sub guess_shell_path {
138 16 ▶ my $pwd = guess_pwd();
Subroutine guess_shell_path: called CC=2 SCAR=6.9
Pod undocumented
139 16 my ($volume, $path, $file) = File::Spec->splitpath($pwd);
140 16 my @path = File::Spec->splitdir($path);
141 16 ▶ pop @path unless $path[-1];
Branch: 1 branch
BranchTrueFalseTotal
unless $path[-1] 16 0 16
142 16 @base = (@path, $file);
143 16 my @zero = File::Spec->splitdir($0);
144 16 pop @zero; # get rid of the script
145 ## a clean base is also important for the pop business below
146 #@base = grep { $_ && $_ ne '.' } shell_resolve(\@base, \@zero);
147 16 @base = shell_resolve(\@base, \@zero);
148 16 return File::Spec->catpath( $volume, (File::Spec->catdir( @base )), '' );
149 }
150  
151 ## naive method, but really DWIM from a developer perspective
152 sub shell_resolve {
153 33 ▶ my ($left, $right) = @_;
Subroutine shell_resolve: called CC=7 SCAR=19.6
Pod undocumented
154 33 ▶ while (@$right && $right->[0] eq '.') { shift @$right }
Branch: 1 branch
BranchTrueFalseTotal
if (@$right and $right->[0] eq ".") 1 33 34
Condition66%2/3
@$right and $right->[0] eq "."
!ll&&!rl&&r
0331
MC/DC50%1/2
@$right and $right->[0] eq "."
@$right $right->[0] eq "."
Truth table66%2/3
A and B
A= @$rightB= $right->[0] eq "."
ABresult
0X0
100
111
155 33 ▶ while (@$right && $right->[0] eq '..') {
Branch: 1 branch
BranchTrueFalseTotal
if (@$right and $right->[0] eq "..") 7 33 40
Condition66%2/3
@$right and $right->[0] eq ".."
!ll&&!rl&&r
0337
MC/DC50%1/2
@$right and $right->[0] eq ".."
@$right $right->[0] eq ".."
Truth table66%2/3
A and B
A= @$rightB= $right->[0] eq ".."
ABresult
0X0
100
111
156 7 shift @$right;
157 ## chop off @left until we removed a significant path part
158 7 my $part;
159 7 ▶ while (@$left && !$part) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$left and not $part) 7 7 14
Condition66%2/3
@$left and not $part
!ll&&!rl&&r
077
MC/DC50%1/2
@$left and not $part
@$left not $part
Truth table66%2/3
A and not(B)
A= @$leftB= not $part
ABresult
0X0
100
111
160 7 $part = pop @$left;
161 }
162 }
163  
164 33 return (@$left, @$right);
165 }
166  
167 sub guess_system_path {
168 2 ▶ my @split = (File::Spec->splitpath( File::Spec->rel2abs($0) ))[ 0, 1 ];
Subroutine guess_system_path: called CC=1 SCAR=0.0
Pod undocumented
169 2 return File::Spec->catpath( @split, '' );
170 }
171  
172 sub import {
173 20 ▶ my $class = shift;
Subroutine import: called CC=13 SCAR=26.0
174 20 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 5 15 20
175  
176 15 ▶ Carp::croak("The script/base dir cannot be found") unless -e $Base;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $Find::Lib::Base 2 13 15
177  
178 13 my @libs;
179  
180 13 ▶ if ($_[0] eq 'libs') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] eq "libs") 4 9 13
181 4 ▶ if ($_[1] && ref $_[1] && ref $_[1] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1] and ref $_[1] and ref $_[1] eq "ARRAY") 1 3 4
Condition83%5/6
$_[1] and ref $_[1] and ref $_[1] eq "ARRAY"
!ll&&!rl&&r
301
$_[1] and ref $_[1]
!ll&&!rl&&r
211
MC/DC66%2/3
$_[1] and ref $_[1] and ref $_[1] eq "ARRAY"
$_[1] ref $_[1] ref $_[1] eq "ARRAY"
Truth table75%3/4
A and B and C
A= $_[1]B= ref $_[1]C= ref $_[1] eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
182 ## backward compat mode;
183 1 @libs = @{ $_[1] };
184 }
185 }
186 13 ▶ @libs = @_ unless @libs;
Branch: 1 branch
BranchTrueFalseTotal
unless @libs 12 1 13
187  
188 13 for ( reverse @libs ) {
189 17 my @lib = File::Spec->splitdir($_);
190 17 ▶ if (@lib && ! $lib[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lib and not $lib[0]) 0 17 17
Condition33%1/3
@lib and not $lib[0]
!ll&&!rl&&r
0170
MC/DC0%0/2
@lib and not $lib[0]
@lib not $lib[0]
Truth table33%1/3
A and not(B)
A= @libB= not $lib[0]
ABresult
0X0
100
111
191 # '/abs/olute/' path
192 0 lib->import($_);
193 0 next;
194 }
195 17 my $dir = File::Spec->catdir( shell_resolve( [ @base ], \@lib ) );
196 17 ▶ unless (-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 2 15 17
197 ## Try the old way (<0.03)
198 2 $dir = File::Spec->catdir($Base, $_);
199 }
200 17 ▶ next unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 2 15 17
201 15 lib->import( $dir );
202 }
203 }
204  
205 =head2 base
206  
207 Returns the detected base (the directory where the script lives in). It's a
208 string, and is the same as C<$Find::Lib::Base>.
209  
210 =cut
211  
212 1 ▶sub base { return $Base }
Subroutine base: called CC=1 SCAR=0.0
Pod documented
213  
214 =head2 catfile
215  
216 A shorcut to L<File::Spec::catfile> using B<Find::Lib>'s base.
217  
218 =cut
219  
220 sub catfile {
221 1 ▶ my $class = shift;
Subroutine catfile: called CC=1 SCAR=0.0
Pod documented
222 1 return File::Spec->catfile($Base, @_);
223 }
224  
225 =head2 catdir
226  
227 A shorcut to L<File::Spec::catdir> using B<Find::Lib>'s base.
228  
229 =cut
230  
231 sub catdir {
232 3 ▶ my $class = shift;
Subroutine catdir: called CC=1 SCAR=0.0
Pod documented
233 3 return File::Spec->catdir($Base, @_);
234 }
235  
236 =head1 BACKWARD COMPATIBILITY
237  
238 in versions <1.0 of Find::Lib, the import arguments allowed you to specify
239 a Bootstrap package. This option is now B<removed> breaking backward
240 compatibility. I'm sorry about that, but that was a dumb idea of mine to
241 save more typing. But it saves, like, 3 characters at the expense of
242 readability. So, I'm sure I didn't break anybody, because probabaly no one
243 was relying on a stupid behaviour.
244  
245 However, the multiple libs argument passing is kept intact: you can still
246 use:
247  
248 use Find::Lib libs => [ 'a', 'b', 'c' ];
249  
250  
251 where C<libs> is a reference to a list of path to add to C<@INC>.
252  
253 The short forms implies that the first argument passed to import is not C<libs>
254 or C<pkgs>. An example of usage is given in the SYNOPSIS section.
255  
256  
257 =head1 SEE ALSO
258  
259 L<FindBin>, L<FindBin::libs>, L<lib>, L<rlib>, L<local::lib>
260 L<http://blog.cyberion.net/2009/10/ive-done-something-bad-i-broke-backward-compatibility.html>
261  
262 =head1 AUTHOR
263  
264 Yann Kerherve, C<< <yann.kerherve at gmail.com> >>
265  
266 =head1 BUGS
267  
268 Please report any bugs or feature requests to
269 C<bug-find-lib at rt.cpan.org>, or through the web interface at
270 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Find-Lib>.
271 I will be notified, and then you'll automatically be notified of progress on
272 your bug as I make changes.
273  
274 =head1 ACKNOWLEDGEMENT
275  
276 Six Apart hackers nourrished the discussion that led to this module creation.
277  
278 Jonathan Steinert (hachi) for doing all the conception of 0.03 shell expansion
279 mode with me.
280  
281 =head1 SUPPORT & CRITICS
282  
283 I welcome feedback about this module, don't hesitate to contact me regarding this
284 module, usage or code.
285  
286 You can find documentation for this module with the perldoc command.
287  
288 perldoc Find::Lib
289  
290 You can also look for information at:
291  
292 =over 4
293  
294 =item * AnnoCPAN: Annotated CPAN documentation
295  
296 L<http://annocpan.org/dist/Find-Lib>
297  
298 =item * CPAN Ratings
299  
300 L<http://cpanratings.perl.org/d/Find-Lib>
301  
302 =item * RT: CPAN's request tracker
303  
304 L<http://rt.cpan.org/NoAuth/Bugs.html?Find-Lib>
305  
306 =item * Search CPAN
307  
308 L<http://search.cpan.org/dist/Find-Lib>
309  
310 =back
311  
312 =head1 COPYRIGHT & LICENSE
313  
314 Copyright 2007, 2009 Yann Kerherve, all rights reserved.
315  
316 This program is free software; you can redistribute it and/or modify it
317 under the same terms as Perl itself.
318  
319 =cut
320  
321 16 1;
↑ Summary /root/.cpan/build/Find-Lib-1.03_01-0/t/mylib/MyLibNoTest.pm »