Bin.pm untested

filter: Statementstmt 0.0% 0 / 91 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 168 CC 18 SCAR 35.3
CC
18
Cov
0%
import
56.0
pwd
42.0
canon_dir
42.0
CRAP 34.1
1 ########################################################################
2 # housekeeping
3 ########################################################################
4 package FindBin::Bin v2.0.1;
5 use v5.40;
6  
7 use parent qw( Exporter );
8  
9 use File::stat;
10  
11 use Cwd qw( getcwd );
12 use Symbol qw( qualify_to_ref );
13  
14 use File::Spec::Functions
15 qw
16 (
17 canonpath
18 catdir
19 catpath
20 curdir
21 file_name_is_absolute
22 rel2abs
23 rootdir
24 splitdir
25 splitpath
26 );
27  
28 sub pwd()
29 {
30 # preserve symlinks in the path as with
31 # "pwd -L" while still working on MSW, VMS.
32  
33 if( $^O eq 'MSWin32' )
34 {
35 # getcwd does not resolve junctions or symlinks here, and
36 # there is no usable inode to validate $PWD against.
37  
38 getcwd
39 }
40 elsif( $^O eq 'VMS' )
41 {
42 # $ENV{DEFAULT} keeps rooted logicals unexpanded.
43  
44 $ENV{ DEFAULT } // getcwd
45 }
46 else
47 {
48 # *NIX, Cygwin, MSYS: trust $PWD only if it
49 # still names the current directory (same
50 # check as "pwd -L"). otherwise live with
51 # cwd and having rel2abs resolve symlinks.
52  
53 if( my $pwd = $ENV{ PWD } )
54 {
55 if( $pwd =~ m{^/} )
56 {
57 my $env = File::stat::stat $pwd ;
58 my $cur = File::stat::stat '.' ;
59  
60 $env->ino
61 && $cur->ino
62 && $env->dev == $cur->dev
63 && $env->ino == $cur->ino
64 ? $pwd
65 : getcwd
66 }
67 else
68 {
69 getcwd
70 }
71 }
72 else
73 {
74 getcwd
75 }
76 }
77 }
78  
79 sub canon_dir( $dir )
80 {
81 # notice that this ignores any volume, the caller
82 # can deal with it. this also expects a dir, not a
83 # file, path.
84  
85 $dir or return;
86  
87 my $raw
88 = file_name_is_absolute( $dir )
89 ? $dir
90 : catdir pwd, $dir
91 ;
92
93 # caller gets back the path.
94  
95 if( $^O eq 'VMS' )
96 {
97 $raw
98 }
99 else
100 {
101 # MSW and *NIX have the same process:
102 # clean up '.' and '..' in the path without
103 # calling rel2abs to resolve the symlinks.
104  
105 state $root_marker = ( splitdir rootdir )[ 0 ];
106
107 my @dirz = splitdir $raw;
108 my @stack = ();
109  
110 while( @dirz )
111 {
112 my $part = shift @dirz;
113  
114 next if $part eq q{.};
115  
116 if( $part eq q{..} )
117 {
118 pop @stack
119 if @stack && $stack[ -1 ] ne $root_marker;
120 }
121 else
122 {
123 push @stack, $part;
124 }
125 }
126  
127 catdir @stack
128 }
129 }
130  
131 BEGIN
132 {
133 # recall that $0 is an lvalue!!
134 # grab it quickly before it can change.
135 # this will not change during execution, compute it once.
136 #
137 # note that $0 is initialized to a file path, not a dir.
138  
139 my ( $logical, $physical )
140 = do
141 {
142 if( $0 =~ m{^ (?: -e | - | interactive ) }x )
143 {
144 my $raw = pwd;
145  
146 (
147 $raw
148 , rel2abs( $raw )
149 )
150 }
151 else
152 {
153 my $phys
154 = do
155 {
156 my ( $v, $d ) = ( splitpath rel2abs( $0 ), '' )[0,1];
157 catpath $v, canonpath( $d ), ''
158 };
159  
160 my $log
161 = do
162 {
163 my ( $v, $d ) = ( splitpath $0, '' )[0,1];
164 catpath $v, canon_dir( $d ), ''
165 };
166  
167 ( $log, $phys )
168 }
169 };
170
171 # notice that these are installed into the *local* package.
172 # callers inherit them via import(), below.
173  
174 # $Bin_l -- logical path (canon_dir, like "pwd -L")
175  
176 eval qq| *{ qualify_to_ref 'Bin_l' } = \\'$logical' |;
177 eval qq| sub Bin_l(){ '$logical' } |;
178  
179 # $Bin_p -- physical path (rel2abs of $0, like "pwd -P")
180  
181 eval qq| *{ qualify_to_ref 'Bin_p' } = \\'$physical' |;
182 eval qq| sub Bin_p(){ '$physical' } |;
183  
184 # $Bin -- module-level default = logical; import() may
185 # alias to physical
186  
187 eval qq| *{ qualify_to_ref 'Bin' } = \\'$physical' |;
188 eval qq| sub Bin(){ '$physical' } |;
189 }
190  
191 ########################################################################
192 # package variables & sanity checks
193 ########################################################################
194  
195 our @EXPORT = qw( $Bin Bin );
196 our @EXPORT_OK = qw( $Bin Bin $Bin_l Bin_l $Bin_p Bin_p pwd canon_dir );
197  
198 ########################################################################
199 # interface
200 ########################################################################
201  
202 sub import
203 {
204 my $pkg = shift;
205 my $caller = caller;
206  
207 # list context captures the matched string, not a count.
208 my ($flag) = grep { m{abspath} } @_;
209 $flag //= '';
210 my @want = grep { ! m{abspath} } @_;
211  
212 my $use_phys
213 = $flag eq 'noabspath'
214 ? ''
215 : 1
216 ;
217  
218 # No explicit requests -- use default @EXPORT list.
219  
220 @want = @EXPORT
221 unless @want;
222  
223 if( grep { m{ ^ [&\$]? Bin $ }x } @want )
224 {
225 my $flavor = $use_phys ? 'Bin_p' : 'Bin_l';
226 my $src = qualify_to_ref $flavor => __PACKAGE__;
227 my $dst = qualify_to_ref 'Bin' => $caller;
228  
229 # Alias $Bin (SCALAR slot) -- preserves read-only constraint.
230 *{ $dst } = *{ $src }{ SCALAR };
231  
232 # Alias Bin() (CODE slot) when the bare sub was requested.
233 *{ $dst } = *{ $src }{ CODE }
234 if grep { m{ ^ [&]? Bin $ }x } @want;
235  
236 @want = grep { ! m{ ^ [&\$]? Bin $ }x } @want;
237 }
238  
239 $pkg->export_to_level( 1, $pkg, @want )
240 if @want;
241 }
242  
243 1
244 __END__