lib/lib/abs.pm

filter: Statementstmt 72.6% 61 / 84 Branchbran 43.3% 13 / 30 Conditioncond 39.1% 9 / 23 MC/DCmcdc 15.3% 2 / 13 Subroutinesub 75.0% 12 / 16 Podpod 33.3% 1 / 3 totaltotal 57.9% 98 / 169 CC 23 SCAR 29.0
CC
23
Cov
61%
transform
38.2
path
12.0
unimport
6.0
CRAP 18.1
↑ Summary
1 #
2 # Copyright (c) 2007-2020 Mons Anderson <mons@cpan.org>. All rights reserved
3 # This program is free software; you can redistribute it and/or
4 # modify it under the same terms as Perl itself.
5 package lib::abs;
6  
7 =head1 NAME
8  
9 lib::abs - C<lib> that makes relative path absolute to caller.
10  
11 =cut
12  
13 1 our $VERSION = '0.95';
14  
15 =head1 SYNOPSIS
16  
17 Simple use like C<use lib ...>:
18  
19 use lib::abs qw(./mylibs1 ../mylibs2);
20 use lib::abs 'mylibs';
21
22 # if your path may not exists and it is ok, then:
23 use lib::abs -soft => qw(./mylibs1 ../mylibs2);
24  
25 Extended syntax (glob)
26  
27 use lib::abs 'modules/*/lib';
28  
29 There are also may be used helper function from lib::abs (see example/ex4):
30  
31 use lib::abs;
32 # ...
33 my $path = lib::abs::path('../path/relative/to/me'); # returns absolute path
34  
35 =head1 DESCRIPTION
36  
37 The main reason of this library is transformate relative paths to absolute at the C<BEGIN> stage, and push transformed to C<@INC>.
38 Relative path basis is not the current working directory, but the location of file, where the statement is (caller file).
39 When using common C<lib>, relative paths stays relative to curernt working directory,
40  
41 # For ex:
42 # script: /opt/scripts/my.pl
43 use lib::abs '../lib';
44  
45 # We run `/opt/scripts/my.pl` having cwd /home/mons
46 # The @INC will contain '/opt/lib';
47  
48 # We run `./my.pl` having cwd /opt
49 # The @INC will contain '/opt/lib';
50  
51 # We run `../my.pl` having cwd /opt/lib
52 # The @INC will contain '/opt/lib';
53  
54 Also this module is useful when writing tests, when you want to load strictly the module from ../lib, respecting the test file.
55  
56 # t/00-test.t
57 use lib::abs '../lib';
58  
59 Also this is useful, when you running under C<mod_perl>, use something like C<Apache::StatINC>, and your application may change working directory.
60 So in case of chdir C<StatINC> fails to reload module if the @INC contain relative paths.
61  
62 =head1 RATIONALE
63  
64 Q: We already have C<FindBin> and C<lib>, why we need this module?
65  
66 A: There are several reasons:
67  
68 =over 4
69  
70 =item 1) C<FindBin> could find path incorrectly under C<mod_perl>
71  
72 =item 2) C<FindBin> works relatively to executed binary instead of relatively to caller
73  
74 =item 3) Perl is linguistic language, and C<`use lib::abs "..."'> semantically more clear and looks more beautiful than C<`use FindBin; use lib "$FindBin::Bin/../lib";'>
75  
76 =item 4) C<FindBin> b<will> work incorrectly, if will be called not from executed binary (see L<http://github.com/Mons/lib-abs-vs-findbin> comparison for details)
77  
78 =back
79  
80 =head1 BUGS
81  
82 None known
83  
84 =head1 COPYRIGHT AND LICENSE
85  
86 This software is copyright (c) 2007-2020 by Mons Anderson.
87  
88 This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
89  
90 =head1 AUTHOR
91  
92 Mons Anderson, C<< <mons@cpan.org> >>
93  
94 =head1 CONTRIBUTORS
95  
96 Oleg Kostyuk, C<< <cub@cpan.org> >>
97  
98 =cut
99  
100 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
102 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
103 1 ▶use lib ();
Subroutine BEGIN: called CC=1 SCAR=0.0
104 1 ▶use Cwd 3.12 qw(abs_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
105 $lib::abs::sep = {
106 4 ( map { $_ => qr{[^\\/]+$}o } qw(mswin32 netware symbian dos) ),
107 1 ( map { $_ => qr{[^:]+:?$}o } qw(macos) ),
108 1 ▶}->{lc$^O} || qr{[^/]+$}o;
Condition50%1/2
{map({$_, qr"[^\\/]+$"o;} 'mswin32', 'netware', 'symbian', 'dos'), map({$_, qr/[^:]+:?$/o;} 'macos')}->{lc $^O} || qr"[^/]+$"o
l!l
01
MC/DC0%0/1
{map({$_, qr"[^\\/]+$"o;} 'mswin32', 'netware', 'symbian', 'dos'), map({$_, qr/[^:]+:?$/o;} 'macos')}->{lc $^O} || qr"[^/]+$"o
{map({$_, qr"[^\\/]+$"o;} 'mswin32', 'netware', 'symbian', 'dos'), map({$_, qr/[^:]+:?$/o;} 'macos')}->{lc $^O}
Truth table50%1/2
A
A= {map({$_, qr"[^\\/]+$"o;} 'mswin32', 'netware', 'symbian', 'dos'), map({$_, qr/[^:]+:?$/o;} 'macos')}->{lc $^O}
Aresult
11
00
109  
110 1 ▶BEGIN { *DEBUG = sub () { 0 } unless defined &DEBUG } # use constants is heavy
Subroutine BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless defined &lib::abs::DEBUG 1 0 1
111  
112 0 ▶sub _carp { require Carp; goto &Carp::carp }
Subroutine _carp: not called CC=1 SCAR=6.9
113 2 ▶sub _croak { require Carp; goto &Carp::croak }
Subroutine _croak: called CC=1 SCAR=0.0
114 0 ▶sub _debug ($@) { printf STDERR shift()." at @{[ (caller)[1,2] ]}\n",@_ }
Subroutine _debug: not called CC=1 SCAR=6.9
115  
116 5 ▶sub mkapath($) {
Subroutine mkapath: called CC=2 SCAR=6.9
Pod undocumented
117 5 my $depth = shift;
118
119 # Prepare absolute base bath
120 5 my ($pkg,$file) = (caller($depth))[0,1];
121 5 _debug "file = $file " if DEBUG > 1;
122 5 $file =~ s/${lib::abs::sep}//s;
123 5 ▶ $file = '.' unless length $file;
Branch: 1 branch
BranchTrueFalseTotal
unless length $file 0 5 5
124 5 _debug "base path = $file" if DEBUG > 1;
125 5 my $f = abs_path($file) . '/';
126 5 _debug "source dir = $f " if DEBUG > 1;
127 5 $f;
128 }
129  
130 0 ▶sub path {
Subroutine path: not called CC=3 SCAR=24.8
Pod documented
131 0 local $_ = shift;
132 0 s{^\./+}{};
133 0 local $!;
134 0 my $abs = mkapath(1) . $_;
135 0 ▶ my $ret = abs_path( $abs ) or _carp("Bad path specification: `$_' => `$abs'" . ($! ? " ($!)" : ''));
Branch: 2 branches
BranchTrueFalseTotal
my $ret = abs_path($abs) or _carp("Bad path specification: `${_}' => `${abs}'" . ($! ? " ($!)" : "")) 0 0 0
$! ? : 0 0 0
136 0 _debug "$_ => $ret" if DEBUG > 1;
137 0 $ret;
138 }
139 1 our $SOFT;
140 5 ▶sub transform {
Subroutine transform: called CC=16 SCAR=36.4
Pod undocumented
141 5 my $prefix;
142 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
143 map {
144 5 ▶ ref || m{^/} ? $_ : do {
Branch: 1 branch
BranchTrueFalseTotal
ref $_ || m[^/] ? : 0 5 5
Condition33%1/3
ref $_ or m[^/]
l!l&&r!l&&!r
005
MC/DC0%0/2
ref $_ or m[^/]
ref $_ m[^/]
Truth table33%1/3
A or B
A= ref $_B= m[^/]
ABresult
1X1
011
000
145 5 my $lib = $_;
146 5 s{^\./+}{};
147 5 local $!;
148 5 ▶ my $abs = ( $prefix ||= mkapath(2) ) . $_;
Condition33%1/3
$prefix ||= &mkapath(2)
l!l&&r!l&&!r
050
MC/DC0%0/2
$prefix ||= &mkapath(2)
$prefix &mkapath(2)
Truth table33%1/3
A ||= B
A= $prefixB= &mkapath(2)
ABresult
1X1
011
000
149 5 ▶ if (index($abs,'*') != -1 or index($abs,'?') !=-1) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($abs, '*') != -1 or index($abs, '?') != -1) { } 0 5 5
Condition33%1/3
index($abs, "*") != -1 or index($abs, "?") != -1
l!l&&r!l&&!r
005
MC/DC0%0/2
index($abs, "*") != -1 or index($abs, "?") != -1
index($abs, "*") != -1 index($abs, "?") != -1
Truth table33%1/3
A or B
A= index($abs, "*") != -1B= index($abs, "?") != -1
ABresult
1X1
011
000
150 0 _debug "transforming $abs using glob" if DEBUG > 1;
151 map {
152 0 my $x;
153 0 ▶ $x = abs_path( $_ ) and -d $x
Branch: 2 branches
BranchTrueFalseTotal
$x = abs_path($_) and -d $x or $lib::abs::SOFT or _croak("Bad path specification: `${lib}' => `${x}'" . ($! ? " ($!)" : "")) 0 0 0
$! ? : 0 0 0
Condition0%0/6
$x = abs_path($_) and -d $x or $lib::abs::SOFT
l!l&&r!l&&!r
000
$x = abs_path($_) and -d $x
!ll&&!rl&&r
000
MC/DC0%0/3
$x = abs_path($_) and -d $x or $lib::abs::SOFT
$x = abs_path($_) -d $x $lib::abs::SOFT
Truth table0%0/5
A and B or C
A= $x = abs_path($_)B= -d $xC= $lib::abs::SOFT
ABCresult
11X1
0X11
1011
0X00
1000
154 or $SOFT or _croak("Bad path specification: `$lib' => `$x'" . ($! ? " ($!)" : ''));
155 0 ▶ defined $x ? ($x) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $x ? : 0 0 0
156 1 ▶ } glob $abs;
Subroutine BEGIN: called CC=1 SCAR=0.0
157 } else {
158 5 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $_ = abs_path($abs); 1 } } and -d $_ or $lib::abs::SOFT or _croak("Bad path specification: `${lib}' => `${abs}'" . ($! ? " ($!)" : "")) 3 2 5
Condition83%5/6
eval { do { $_ = abs_path($abs); 1 } } and -d $_ or $lib::abs::SOFT
l!l&&r!l&&!r
122
eval { do { $_ = abs_path($abs); 1 } } and -d $_
!ll&&!rl&&r
041
MC/DC66%2/3
eval { do { $_ = abs_path($abs); 1 } } and -d $_ or $lib::abs::SOFT
eval { do { $_ = abs_path($abs); 1 } } -d $_ $lib::abs::SOFT
Truth table60%3/5
A and B or C
A= eval { do { $_ = abs_path($abs); 1 } }B= -d $_C= $lib::abs::SOFT
ABCresult
11X1
0X11
1011
0X00
1000
159 5 $_ = abs_path( $abs );
160 5 ▶ 1} and -d $_
Branch: 1 branch
BranchTrueFalseTotal
$! ? : 2 0 2
161 or $SOFT or _croak("Bad path specification: `$lib' => `$abs'" . ($! ? " ($!)" : ''));
162 3 _debug "$lib => $_" if DEBUG > 1;
163 3 ▶ defined $_ ? ($_) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 2 1 3
164 }
165 }
166 } @_;
167 }
168  
169 6 ▶sub import {
Subroutine import: called CC=3 SCAR=11.0
170 6 shift;
171 6 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 5 6
172 5 my $soft = 0;
173 5 ▶ if ($_[0] eq '-soft') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] eq "-soft") 2 3 5
174 2 $soft = 1;
175 2 shift @_;
176 }
177 5 local $SOFT = $soft;
178 5 @_ = ( lib => transform @_ = @_ );
179 3 _debug "use @_\n" if DEBUG > 0;
180 3 goto &lib::import;
181 0 return;
182 }
183  
184 0 ▶sub unimport {
Subroutine unimport: not called CC=2 SCAR=17.9
185 0 shift;
186 0 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
187 0 @_ = ( lib => transform @_ = @_ );
188 0 _debug "no @_\n" if DEBUG > 0;
189 0 goto &lib::unimport;
190 0 return;
191 }
192  
193 1 1;
↑ Summary