blib/lib/Find/Lib.pm

filter: Statementstmt 97.3% 74 / 76 Branchbran 92.3% 24 / 26 Conditioncond 70.8% 17 / 24 MC/DCmcdc 53.3% 8 / 15 Subroutinesub 100% 17 / 17 Podpod 37.5% 3 / 8 totaltotal 86.1% 143 / 166 CC 23 SCAR 18.7
CC
23
Cov
91%
import
13.4
shell_resolve
7.0
guess_base
3.0
CRAP 6.5
↑ Summary t/mylib/MyLibNoTest.pm »
1 package Find::Lib;
2 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use lib;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 18 ▶use File::Spec();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 ▶use vars qw/$Base $VERSION @base/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 18 ▶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 18 $VERSION = '1.04';
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 display 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 18 ▶use Carp();
Subroutine BEGIN: called CC=1 SCAR=0.0
120 18 ▶use Cwd();
Subroutine BEGIN: called CC=1 SCAR=0.0
121  
122 18 $Script = $Base = guess_base();
123  
124 18 ▶sub guess_base {
Subroutine guess_base: called CC=3 SCAR=11.0
Pod undocumented
125 18 my $base;
126 18 $base = guess_shell_path();
127 18 ▶ return $base if $base && -e $base;
Branch: 1 branch
BranchTrueFalseTotal
if $base and -e $base 16 2 18
Condition66%2/3
$base and -e $base
!ll&&!rl&&r
0216
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
128 2 return guess_system_path();
129 }
130  
131 ## we want to use PWD if it exists (it's not guaranteed on all platforms)
132 ## so that we have a sense of the shell current working dir, with unresolved
133 ## symlinks
134 18 ▶sub guess_pwd {
Subroutine guess_pwd: called CC=2 SCAR=9.2
Pod undocumented
135 18 ▶ return $ENV{PWD} || Cwd::cwd();
Condition33%1/3
$ENV{'PWD'} || Cwd::cwd()
l!l&&r!l&&!r
1800
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
136 }
137  
138 18 ▶sub guess_shell_path {
Subroutine guess_shell_path: called CC=2 SCAR=6.9
Pod undocumented
139 18 my $pwd = guess_pwd();
140 18 my ($volume, $path, $file) = File::Spec->splitpath($pwd);
141 18 my @path = File::Spec->splitdir($path);
142 18 ▶ pop @path unless $path[-1];
Branch: 1 branch
BranchTrueFalseTotal
unless $path[-1] 18 0 18
143 18 @base = (@path, $file);
144 18 my @zero = File::Spec->splitdir($0);
145 18 pop @zero; # get rid of the script
146 ## a clean base is also important for the pop business below
147 #@base = grep { $_ && $_ ne '.' } shell_resolve(\@base, \@zero);
148 18 @base = shell_resolve(\@base, \@zero);
149 18 return File::Spec->catpath( $volume, (File::Spec->catdir( @base )), '' );
150 }
151  
152 ## naive method, but really DWIM from a developer perspective
153 37 ▶sub shell_resolve {
Subroutine shell_resolve: called CC=7 SCAR=19.5
Pod undocumented
154 37 my ($left, $right) = @_;
155 37 ▶ while (@$right && $right->[0] eq '.') { shift @$right }
Branch: 1 branch
BranchTrueFalseTotal
if (@$right and $right->[0] eq ".") 2 37 39
Condition100%3/3
@$right and $right->[0] eq "."
!ll&&!rl&&r
1362
MC/DC100%2/2
@$right and $right->[0] eq "."
@$right $right->[0] eq "."
Truth table100%3/3
A and B
A= @$rightB= $right->[0] eq "."
ABresult
0X0
100
111
156 37 ▶ while (@$right && $right->[0] eq '..') {
Branch: 1 branch
BranchTrueFalseTotal
if (@$right and $right->[0] eq "..") 9 37 46
Condition100%3/3
@$right and $right->[0] eq ".."
!ll&&!rl&&r
1369
MC/DC100%2/2
@$right and $right->[0] eq ".."
@$right $right->[0] eq ".."
Truth table100%3/3
A and B
A= @$rightB= $right->[0] eq ".."
ABresult
0X0
100
111
157 9 shift @$right;
158 ## chop off @left until we removed a significant path part
159 9 my $part;
160 9 ▶ while (@$left && !$part) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$left and not $part) 11 9 20
Condition66%2/3
@$left and not $part
!ll&&!rl&&r
0911
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
161 11 $part = pop @$left;
162 }
163 }
164  
165 37 return (@$left, @$right);
166 }
167  
168 2 ▶sub guess_system_path {
Subroutine guess_system_path: called CC=1 SCAR=0.0
Pod undocumented
169 2 my @split = (File::Spec->splitpath( File::Spec->rel2abs($0) ))[ 0, 1 ];
170 2 return File::Spec->catpath( @split, '' );
171 }
172  
173 22 ▶sub import {
Subroutine import: called CC=13 SCAR=26.0
174 22 my $class = shift;
175 22 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 5 17 22
176  
177 17 ▶ Carp::croak("The script/base dir cannot be found") unless -e $Base;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $Find::Lib::Base 2 15 17
178  
179 15 my @libs;
180  
181 15 ▶ if ($_[0] eq 'libs') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] eq "libs") 4 11 15
182 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
183 ## backward compat mode;
184 1 @libs = @{ $_[1] };
185 }
186 }
187 15 ▶ @libs = @_ unless @libs;
Branch: 1 branch
BranchTrueFalseTotal
unless @libs 14 1 15
188  
189 15 for ( reverse @libs ) {
190 19 my @lib = File::Spec->splitdir($_);
191 19 ▶ if (@lib && ! $lib[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lib and not $lib[0]) 0 19 19
Condition33%1/3
@lib and not $lib[0]
!ll&&!rl&&r
0190
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
192 # '/abs/olute/' path
193 0 lib->import($_);
194 0 next;
195 }
196 19 my $dir = File::Spec->catdir( shell_resolve( [ @base ], \@lib ) );
197 19 ▶ unless (-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 2 17 19
198 ## Try the old way (<0.03)
199 2 $dir = File::Spec->catdir($Base, $_);
200 }
201 19 ▶ next unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 2 17 19
202 17 lib->import( $dir );
203 }
204 }
205  
206 =head2 base
207  
208 Returns the detected base (the directory where the script lives in). It's a
209 string, and is the same as C<$Find::Lib::Base>.
210  
211 =cut
212  
213 1 ▶sub base { return $Base }
Subroutine base: called CC=1 SCAR=0.0
Pod documented
214  
215 =head2 catfile
216  
217 A shorcut to L<File::Spec::catfile> using B<Find::Lib>'s base.
218  
219 =cut
220  
221 1 ▶sub catfile {
Subroutine catfile: called CC=1 SCAR=0.0
Pod documented
222 1 my $class = shift;
223 1 return File::Spec->catfile($Base, @_);
224 }
225  
226 =head2 catdir
227  
228 A shorcut to L<File::Spec::catdir> using B<Find::Lib>'s base.
229  
230 =cut
231  
232 3 ▶sub catdir {
Subroutine catdir: called CC=1 SCAR=0.0
Pod documented
233 3 my $class = shift;
234 3 return File::Spec->catdir($Base, @_);
235 }
236  
237 =head1 BACKWARD COMPATIBILITY
238  
239 in versions <1.0 of Find::Lib, the import arguments allowed you to specify
240 a Bootstrap package. This option is now B<removed> breaking backward
241 compatibility. I'm sorry about that, but that was a dumb idea of mine to
242 save more typing. But it saves, like, 3 characters at the expense of
243 readability. So, I'm sure I didn't break anybody, because probabaly no one
244 was relying on a stupid behaviour.
245  
246 However, the multiple libs argument passing is kept intact: you can still
247 use:
248  
249 use Find::Lib libs => [ 'a', 'b', 'c' ];
250  
251  
252 where C<libs> is a reference to a list of path to add to C<@INC>.
253  
254 The short forms implies that the first argument passed to import is not C<libs>
255 or C<pkgs>. An example of usage is given in the SYNOPSIS section.
256  
257  
258 =head1 SEE ALSO
259  
260 L<FindBin>, L<FindBin::libs>, L<lib>, L<rlib>, L<local::lib>
261 L<http://blog.cyberion.net/2009/10/ive-done-something-bad-i-broke-backward-compatibility.html>
262  
263 =head1 AUTHOR
264  
265 Yann Kerherve, C<< <yann.kerherve at gmail.com> >>
266  
267 =head1 BUGS
268  
269 Please report any bugs or feature requests to
270 C<bug-find-lib at rt.cpan.org>, or through the web interface at
271 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Find-Lib>.
272 I will be notified, and then you'll automatically be notified of progress on
273 your bug as I make changes.
274  
275 =head1 ACKNOWLEDGEMENT
276  
277 Six Apart hackers nourrished the discussion that led to this module creation.
278  
279 Jonathan Steinert (hachi) for doing all the conception of 0.03 shell expansion
280 mode with me.
281  
282 =head1 SUPPORT & CRITICS
283  
284 I welcome feedback about this module, don't hesitate to contact me regarding this
285 module, usage or code.
286  
287 You can find documentation for this module with the perldoc command.
288  
289 perldoc Find::Lib
290  
291 You can also look for information at:
292  
293 =over 4
294  
295 =item * AnnoCPAN: Annotated CPAN documentation
296  
297 L<http://annocpan.org/dist/Find-Lib>
298  
299 =item * CPAN Ratings
300  
301 L<http://cpanratings.perl.org/d/Find-Lib>
302  
303 =item * RT: CPAN's request tracker
304  
305 L<http://rt.cpan.org/NoAuth/Bugs.html?Find-Lib>
306  
307 =item * Search CPAN
308  
309 L<http://search.cpan.org/dist/Find-Lib>
310  
311 =back
312  
313 =head1 COPYRIGHT & LICENSE
314  
315 Copyright 2007, 2009 Yann Kerherve, all rights reserved.
316  
317 This program is free software; you can redistribute it and/or modify it
318 under the same terms as Perl itself.
319  
320 =cut
321  
322 18 1;
↑ Summary t/mylib/MyLibNoTest.pm »