blib/lib/FindBin/Real.pm

filter: Statementstmt 81.8% 63 / 77 Branchbran 45.6% 21 / 46 Conditioncond 21.0% 8 / 38 MC/DCmcdc 9.0% 2 / 22 Subroutinesub 100% 15 / 15 Podpod 87.5% 7 / 8 totaltotal 56.3% 116 / 206 CC 38 SCAR 53.0
CC
38
Cov
57%
mastermind
287.5
BinDepth
3.3
BEGIN
1.0
CRAP 199.5
↑ Summary
1 # FindBin/Real.pm
2 #
3 # Copyright (c) 1995 Graham Barr & Nick Ing-Simmons. All rights reserved.
4 # Copyright (c) 2003-2005 Serguei Trouchelle. All rights reserved.
5 #
6 # This program is free software; you can redistribute it and/or modify it
7 # under the same terms as Perl itself.
8
9 # History:
10 # 1.05 2007/02/04 Quality update (Test::Pod, Test::Pod::Coverage)
11 # 1.04 2005/02/07 Refactured version. About +50% in performance.
12 # Version is corrected to $FindBin::Real::VERSION.
13 # Fixed problem with Dir/RealDir
14 # Some tests added.
15 # 1.03 2004/02/15 Added BinDepth() function
16 # (Suggested by Tielman de Villiers)
17 # 1.02 2003/08/10 Fixed bug in Makefile.PM (Findbin -> FindBin)
18 # ^M symbols are removed from sources
19 # (Thanks to Mike Castle)
20 # 1.01 2003/08/08 Added some tests and README
21 # 1.00 2003/08/06 Initial revision
22
23 =head1 NAME
24
25 FindBin::Real - Locate directory of original perl script
26
27 =head1 SYNOPSIS
28
29 use FindBin::Real;
30 use lib FindBin::Real::Bin() . '/../lib';
31
32 or
33
34 use FindBin::Real qw(Bin);
35 use lib Bin() . '/../lib';
36
37 or
38
39 # Run from /usr/bin/www/some/path/ or /usr/bin/www/some/other/path or any
40 use FindBin::Real qw(BinDepth);
41 use lib BinDepth(3) . '/lib';
42 # And always got /usr/bin/www/lib !
43
44 =head1 DESCRIPTION
45
46 Locates the full path to the script bin directory to allow the use
47 of paths relative to the bin directory.
48
49 This allows a user to setup a directory tree for some software with
50 directories E<lt>rootE<gt>/bin and E<lt>rootE<gt>/lib and then the above example will allow
51 the use of modules in the lib directory without knowing where the software
52 tree is installed.
53
54 If perl is invoked using the B<-e> option or the perl script is read from
55 C<STDIN> then FindBin sets both C<Bin()> and C<RealBin()> return values to the current
56 directory.
57
58 =head1 EXPORTABLE FUNCTIONS
59
60 =head2 Bin
61
62 - path to bin directory from where script was invoked
63
64 =head2 Script
65
66 - basename of script from which perl was invoked
67
68 =head2 RealBin
69
70 - Bin() with all links resolved
71
72 =head2 RealScript
73
74 - Script() with all links resolved
75
76 =head2 BinDepth(n)
77
78 - path to n-level parent directory
79
80 =head2 Dir()
81
82 - the same as Bin()
83
84 =head2 RealDir()
85
86 - the same as RealBin()
87
88 =head1 KNOWN ISSUES
89
90 If there are two modules using C<FindBin::Real> from different directories
91 under the same interpreter, this WOULD work. Since C<FindBin::Real> uses
92 functions instead of C<BEGIN> block in C<FindBin>, it'll be executed on every script,
93 and all callers will get it right. This module can be used under mod_perl and other persistent
94 Perl environments, where you shouldn't use C<FindBin>.
95
96 =head1 KNOWN BUGS
97
98 If perl is invoked as
99
100 perl filename
101
102 and I<filename> does not have executable rights and a program called I<filename>
103 exists in the users C<$ENV{PATH}> which satisfies both B<-x> and B<-T> then FindBin
104 assumes that it was invoked via the C<$ENV{PATH}>.
105
106 Workaround is to invoke perl as
107
108 perl ./filename
109
110 =head1 AUTHORS
111
112 Serguei Trouchelle E<lt>F<stro@railways.dp.ua>E<gt>
113
114 FindBin::Real uses code from FindBin module, which was written by
115
116 Graham Barr E<lt>F<gbarr@pobox.com>E<gt>
117 Nick Ing-Simmons E<lt>F<nik@tiuk.ti.com>E<gt>
118
119 =head1 COPYRIGHT
120
121 Copyright (c) 1995 Graham Barr & Nick Ing-Simmons. All rights reserved.
122 Copyright (c) 2003-2005 Serguei Trouchelle. All rights reserved.
123
124 This program is free software; you can redistribute it and/or modify it
125 under the same terms as Perl itself.
126
127 =cut
128
129 package FindBin::Real;
130 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
131 9 require 5.006;
132 9 require Exporter;
133 9 ▶use Cwd qw(getcwd abs_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
134 9 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
135 9 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
136 9 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
137
138 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
139 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
140
141 9 our @EXPORT_OK = qw(Bin Script RealBin RealScript Dir RealDir BinDepth);
142 9 our %EXPORT_TAGS = (ALL => [qw(Bin Script RealBin RealScript Dir RealDir BinDepth)]);
143 9 our @ISA = qw(Exporter);
144
145 9 $FindBin::Real::VERSION = "1.05";
146
147 9 my $keyBin = 1;
148 9 my $keyScript = 2;
149 9 my $keyRealBin = 3;
150 9 my $keyRealScript = 4;
151
152 #
153 # mastermind
154 #
155 11 ▶sub mastermind {
Subroutine mastermind: called CC=36 SCAR=56.6
Pod undocumented
156 11 ▶ my $meth = shift || die 'Invalid call to mastermind';
Condition50%1/2
shift() || die('Invalid call to mastermind')
l!l
110
MC/DC0%0/1
shift() || die('Invalid call to mastermind')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
157
158 11 ▶ if ($0 eq '-e' || $0 eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($0 eq "-e" or $0 eq "-") 0 11 11
Condition33%1/3
$0 eq "-e" or $0 eq "-"
l!l&&r!l&&!r
0011
MC/DC0%0/2
$0 eq "-e" or $0 eq "-"
$0 eq "-e" $0 eq "-"
Truth table33%1/3
A or B
A= $0 eq "-e"B= $0 eq "-"
ABresult
1X1
011
000
159 0 ▶ return getcwd() if $meth == $keyBin || $meth == $keyRealBin;
Branch: 1 branch
BranchTrueFalseTotal
if $meth == $keyBin or $meth == $keyRealBin 0 0 0
Condition0%0/3
$meth == $keyBin or $meth == $keyRealBin
l!l&&r!l&&!r
000
MC/DC0%0/2
$meth == $keyBin or $meth == $keyRealBin
$meth == $keyBin $meth == $keyRealBin
Truth table0%0/3
A or B
A= $meth == $keyBinB= $meth == $keyRealBin
ABresult
1X1
011
000
160 0 ▶ return $0 if $meth == $keyScript || $meth == $keyRealScript;
Branch: 1 branch
BranchTrueFalseTotal
if $meth == $keyScript or $meth == $keyRealScript 0 0 0
Condition0%0/3
$meth == $keyScript or $meth == $keyRealScript
l!l&&r!l&&!r
000
MC/DC0%0/2
$meth == $keyScript or $meth == $keyRealScript
$meth == $keyScript $meth == $keyRealScript
Truth table0%0/3
A or B
A= $meth == $keyScriptB= $meth == $keyRealScript
ABresult
1X1
011
000
161 }
162 11 ▶ if ($^O eq 'VMS') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "VMS") 0 11 11
163 # ($Bin,$Script) = VMS::Filespec::rmsexpand($0) =~ /(.*\])(.*)/s;
164 0 ▶ return VMS::Filespec::rmsexpand($0) =~ /(.*\])/s if $meth == $keyBin || $meth == $keyRealBin;
Branch: 1 branch
BranchTrueFalseTotal
if $meth == $keyBin or $meth == $keyRealBin 0 0 0
Condition0%0/3
$meth == $keyBin or $meth == $keyRealBin
l!l&&r!l&&!r
000
MC/DC0%0/2
$meth == $keyBin or $meth == $keyRealBin
$meth == $keyBin $meth == $keyRealBin
Truth table0%0/3
A or B
A= $meth == $keyBinB= $meth == $keyRealBin
ABresult
1X1
011
000
165 0 ▶ return VMS::Filespec::rmsexpand($0) =~ /.*\](.*)/s if $meth == $keyScript || $meth == $keyRealScript;
Branch: 1 branch
BranchTrueFalseTotal
if $meth == $keyScript or $meth == $keyRealScript 0 0 0
Condition0%0/3
$meth == $keyScript or $meth == $keyRealScript
l!l&&r!l&&!r
000
MC/DC0%0/2
$meth == $keyScript or $meth == $keyRealScript
$meth == $keyScript $meth == $keyRealScript
Truth table0%0/3
A or B
A= $meth == $keyScriptB= $meth == $keyRealScript
ABresult
1X1
011
000
166 }
167
168 11 my ($Bin, $Script, $RealBin, $RealScript, $Dir, $RealDir);
169 11 my $script = $0;
170
171 11 ▶ my $dosish = ($^O eq 'MSWin32' or $^O eq 'os2');
Condition33%1/3
$^O eq 'MSWin32' || $^O eq 'os2'
l!l&&r!l&&!r
0011
MC/DC0%0/2
$^O eq 'MSWin32' || $^O eq 'os2'
$^O eq 'MSWin32' $^O eq 'os2'
Truth table33%1/3
A || B
A= $^O eq 'MSWin32'B= $^O eq 'os2'
ABresult
1X1
011
000
172 11 ▶ unless(($script =~ m#/# || ($dosish && $script =~ m#\\#))
Branch: 1 branch
BranchTrueFalseTotal
unless ($script =~ m[/] || $dosish && $script =~ /\\/ and -f $script) 0 11 11
Condition22%2/9
$script =~ m[/] || $dosish && $script =~ /\\/ and -f $script
!ll&&!rl&&r
0011
$script =~ m[/] or $dosish and $script =~ /\\/
l!l&&r!l&&!r
1100
$dosish and $script =~ /\\/
!ll&&!rl&&r
000
MC/DC0%0/4
$script =~ m[/] || $dosish && $script =~ /\\/ and -f $script
$script =~ m[/] $dosish $script =~ /\\/ -f $script
Truth table16%1/6
A or B and C and D
A= $script =~ m[/]B= $dosishC= $script =~ /\\/D= -f $script
ABCDresult
00XX0
010X0
1XX00
01100
1XX11
01111
173 && -f $script)
174 {
175 0 my $dir;
176 0 foreach $dir (File::Spec->path)
177 {
178 0 my $scr = File::Spec->catfile($dir, $script);
179 0 ▶ if(-r $scr && (!$dosish || -x _))
Branch: 1 branch
BranchTrueFalseTotal
if (-r $scr and !$dosish || -x _) 0 0 0
Condition0%0/6
-r $scr and !$dosish || -x _
!ll&&!rl&&r
000
not $dosish or -x _
l!l&&r!l&&!r
000
MC/DC0%0/3
-r $scr and !$dosish || -x _
-r $scr not $dosish -x _
Truth table0%0/4
A and not(B) or C
A= -r $scrB= not $dosishC= -x _
ABCresult
0XX0
1000
11X1
1011
180 {
181 0 $script = $scr;
182
183 0 ▶ if (-f $0)
Branch: 1 branch
BranchTrueFalseTotal
if (-f $0) 0 0 0
184 {
185 # $script has been found via PATH but perl could have
186 # been invoked as 'perl file'. Do a dumb check to see
187 # if $script is a perl program, if not then $script = $0
188 #
189 # well we actually only check that it is an ASCII file
190 # we know its executable so it is probably a script
191 # of some sort.
192
193 0 ▶ $script = $0 unless(-T $script);
Branch: 1 branch
BranchTrueFalseTotal
unless -T $script 0 0 0
194 }
195 0 last;
196 }
197 }
198 }
199
200 11 ▶ croak("Cannot find current script '$0'") unless(-f $script);
Branch: 1 branch
BranchTrueFalseTotal
unless -f $script 0 11 11
201
202 # Ensure $script contains the complete path incase we C<chdir>
203
204 11 ▶ $script = File::Spec->catfile(getcwd(), $script)
Branch: 1 branch
BranchTrueFalseTotal
unless "File::Spec"->file_name_is_absolute($script) 11 0 11
205 unless File::Spec->file_name_is_absolute($script);
206
207 11 ▶ if ($meth == $keyBin or $meth == $keyScript) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meth == $keyBin or $meth == $keyScript) { } 8 3 11
Condition100%3/3
$meth == $keyBin or $meth == $keyScript
l!l&&r!l&&!r
713
MC/DC100%2/2
$meth == $keyBin or $meth == $keyScript
$meth == $keyBin $meth == $keyScript
Truth table100%3/3
A or B
A= $meth == $keyBinB= $meth == $keyScript
ABresult
1X1
011
000
208 8 ($Script,$Bin) = fileparse($script);
209 } else {
210 # RealBin/RealScript:
211 # Resolve $script if it is a link
212 3 while(1) {
213 3 my $linktext = readlink($script);
214
215 3 ($RealScript,$RealBin) = fileparse($script);
216 3 ▶ last unless defined $linktext;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $linktext 3 0 3
217
218 0 ▶ $script = (File::Spec->file_name_is_absolute($linktext))
Branch: 1 branch
BranchTrueFalseTotal
'File::Spec'->file_name_is_absolute($linktext) ? : 0 0 0
219 ? $linktext
220 : File::Spec->catfile($RealBin, $linktext);
221 }
222 }
223 # Get absolute paths to directories
224 11 ▶ $Bin = abs_path($Bin) if $Bin;
Branch: 1 branch
BranchTrueFalseTotal
if $Bin 8 3 11
225 11 ▶ $RealBin = abs_path($RealBin) if $RealBin;
Branch: 1 branch
BranchTrueFalseTotal
if $RealBin 3 8 11
226
227 11 ▶ return $Bin if $meth == $keyBin;
Branch: 1 branch
BranchTrueFalseTotal
if $meth == $keyBin 7 4 11
228 4 ▶ return $Script if $meth == $keyScript;
Branch: 1 branch
BranchTrueFalseTotal
if $meth == $keyScript 1 3 4
229 3 ▶ return $RealBin if $meth == $keyRealBin;
Branch: 1 branch
BranchTrueFalseTotal
if $meth == $keyRealBin 2 1 3
230 1 ▶ return $RealScript if $meth == $keyRealScript;
Branch: 1 branch
BranchTrueFalseTotal
if $meth == $keyRealScript 1 0 1
231 }
232
233 6 ▶sub Bin {
Subroutine Bin: called CC=1 SCAR=0.0
Pod documented
234 6 return mastermind($keyBin);
235 }
236
237 1 ▶sub Script {
Subroutine Script: called CC=1 SCAR=0.0
Pod documented
238 1 return mastermind($keyScript);
239 }
240
241 1 ▶sub RealBin {
Subroutine RealBin: called CC=1 SCAR=0.0
Pod documented
242 1 return mastermind($keyRealBin);
243 }
244
245 1 ▶sub RealScript {
Subroutine RealScript: called CC=1 SCAR=0.0
Pod documented
246 1 return mastermind($keyRealScript);
247 }
248
249 1 ▶sub Dir {
Subroutine Dir: called CC=1 SCAR=0.0
Pod documented
250 1 return mastermind($keyBin);
251 }
252
253 1 ▶sub RealDir {
Subroutine RealDir: called CC=1 SCAR=0.0
Pod documented
254 1 return mastermind($keyRealBin);
255 }
256
257 5 ▶sub BinDepth($) {
Subroutine BinDepth: called CC=3 SCAR=12.0
Pod documented
258 5 my $depth = shift;
259 5 my $Bin = Bin();
260 5 ▶ return $Bin unless $depth =~ /\d+/;
Branch: 1 branch
BranchTrueFalseTotal
unless $depth =~ /\d+/ 0 5 5
261 5 ▶ return $1 . $2 if $Bin =~ m!(.*?)((/[^/]+?){$depth})/!;
Branch: 1 branch
BranchTrueFalseTotal
if $Bin =~ m[(.*?)((/[^/]+?){$depth})/] 5 0 5
262 0 return $Bin;
263 }
264
265 9 1; # Keep require happy
266
267
↑ Summary