/usr/local/lib/perl5/site_perl/5.44.0/Path/Tiny.pm

filter: Statementstmt 24.7% 188 / 760 Branchbran 11.2% 56 / 498 Conditioncond 10.7% 27 / 251 MC/DCmcdc 0.0% 0 / 150 Subroutinesub 29.2% 31 / 106 Podpod 98.4% 63 / 64 totaltotal 19.9% 365 / 1829 CC 339 SCAR 44.2
CC
339
Cov
18%
relative
342.0
__ANON__
272.0
filehandle
242.8
CRAP 83.1
« /usr/local/lib/perl5/5.44.0/x86_64-linux/Storable.pm ↑ Summary lib/Skilletor/ClaudeSettings.pm »
1 7 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Path::Tiny;
6 # ABSTRACT: File path utility
7  
8 7 our $VERSION = '0.150';
9  
10 # Dependencies
11 7 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Exporter 5.57 (qw/import/);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use File::Spec 0.86 (); # shipped with 5.8.1
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 7 our @EXPORT = qw/path/;
17 7 our @EXPORT_OK = qw/cwd rootdir tempfile tempdir/;
18  
19 7 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
20 7 PATH => 0,
21 CANON => 1,
22 VOL => 2,
23 DIR => 3,
24 FILE => 4,
25 TEMP => 5,
26 IS_WIN32 => ( $^O eq 'MSWin32' ),
27 7 };
28  
29 7 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
30 q{""} => 'stringify',
31 bool => sub () { 1 },
32 7 fallback => 1,
33 7 );
34  
35 # FREEZE/THAW per Sereal/CBOR/Types::Serialiser protocol
36 0 ▶sub THAW { return path( $_[2] ) }
Subroutine THAW: not called CC=1 SCAR=6.9
Pod undocumented
37 7 ▶{ no warnings 'once'; *TO_JSON = *FREEZE = \&stringify };
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 7 my $HAS_UU; # has Unicode::UTF8; lazily populated
40  
41 0 ▶sub _check_UU {
Subroutine _check_UU: not called CC=1 SCAR=6.9
42 0 local $SIG{__DIE__}; # prevent outer handler from being called
43 0 !!eval {
44 0 require Unicode::UTF8;
45 0 Unicode::UTF8->VERSION(0.58);
46 0 1;
47 };
48 }
49  
50 my $HAS_PU; # has PerlIO::utf8_strict; lazily populated
51  
52 0 ▶sub _check_PU {
Subroutine _check_PU: not called CC=1 SCAR=6.9
53 0 local $SIG{__DIE__}; # prevent outer handler from being called
54 0 !!eval {
55 # MUST preload Encode or $SIG{__DIE__} localization fails
56 # on some Perl 5.8.8 (maybe other 5.8.*) compiled with -O2.
57 0 require Encode;
58 0 require PerlIO::utf8_strict;
59 0 PerlIO::utf8_strict->VERSION(0.003);
60 0 1;
61 };
62 }
63  
64 7 ▶my $HAS_FLOCK = $Config{d_flock} || $Config{d_fcntl_can_lock} || $Config{d_lockf};
Condition33%2/6
$Path::Tiny::Config{'d_flock'} || $Path::Tiny::Config{'d_fcntl_can_lock'} || $Path::Tiny::Config{'d_lockf'}
l!l&&r!l&&!r
700
$Path::Tiny::Config{"d_flock"} or $Path::Tiny::Config{"d_fcntl_can_lock"}
l!l&&r!l&&!r
700
MC/DC0%0/3
$Path::Tiny::Config{'d_flock'} || $Path::Tiny::Config{'d_fcntl_can_lock'} || $Path::Tiny::Config{'d_lockf'}
$Path::Tiny::Config{"d_flock"} $Path::Tiny::Config{"d_fcntl_can_lock"} $Path::Tiny::Config{'d_lockf'}
Truth table25%1/4
A or B || C
A= $Path::Tiny::Config{"d_flock"}B= $Path::Tiny::Config{"d_fcntl_can_lock"}C= $Path::Tiny::Config{'d_lockf'}
ABCresult
1XX1
01X1
0011
0000
65  
66 # notions of "root" directories differ on Win32: \\server\dir\ or C:\ or \
67 7 my $SLASH = qr{[\\/]};
68 7 my $NOTSLASH = qr{[^\\/]};
69 7 my $DRV_VOL = qr{[a-z]:}i;
70 7 my $UNC_VOL = qr{$SLASH $SLASH $NOTSLASH+ $SLASH $NOTSLASH+}x;
71 7 my $WIN32_ROOT = qr{(?: $UNC_VOL $SLASH | $DRV_VOL $SLASH | $SLASH )}x;
72  
73 0 ▶sub _win32_vol {
Subroutine _win32_vol: not called CC=3 SCAR=24.8
74 0 my ( $path, $drv ) = @_;
75 0 require Cwd;
76 0 my $dcwd = eval { Cwd::getdcwd($drv) }; # C: -> C:\some\cwd
77 # getdcwd on non-existent drive returns empty string
78 # so just use the original drive Z: -> Z:
79 0 ▶ $dcwd = "$drv" unless defined $dcwd && length $dcwd;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dcwd and length $dcwd 0 0 0
Condition0%0/3
defined $dcwd and length $dcwd
!ll&&!rl&&r
000
MC/DC0%0/2
defined $dcwd and length $dcwd
defined $dcwd length $dcwd
Truth table0%0/3
A and B
A= defined $dcwdB= length $dcwd
ABresult
0X0
100
111
80 # normalize dwcd to end with a slash: might be C:\some\cwd or D:\ or Z:
81 0 $dcwd =~ s{$SLASH?\z}{/};
82 # make the path absolute with dcwd
83 0 $path =~ s{^$DRV_VOL}{$dcwd};
84 0 return $path;
85 }
86  
87 # This is a string test for before we have the object; see is_rootdir for well-formed
88 # object test
89 157 ▶sub _is_root {
Subroutine _is_root: called CC=1 SCAR=0.0
90 157 return IS_WIN32() ? ( $_[0] =~ /^$WIN32_ROOT\z/ ) : ( $_[0] eq '/' );
91 }
92  
93 7 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=2.6
94 0 ▶ *_same = IS_WIN32() ? sub { lc( $_[0] ) eq lc( $_[1] ) } : sub { $_[0] eq $_[1] };
Subroutine __ANON__: not called CC=1 SCAR=2.6
Subroutine __ANON__: not called CC=1 SCAR=2.6
95 }
96  
97 # mode bits encoded for chmod in symbolic mode
98 7 my %MODEBITS = ( om => 0007, gm => 0070, um => 0700 ); ## no critic
99 7 { my $m = 0; $MODEBITS{$_} = ( 1 << $m++ ) for qw/ox ow or gx gw gr ux uw ur/ };
100  
101 0 ▶sub _symbolic_chmod {
Subroutine _symbolic_chmod: not called CC=7 SCAR=40.3
102 0 my ( $mode, $symbolic ) = @_;
103 0 for my $clause ( split /,\s*/, $symbolic ) {
104 0 ▶ if ( $clause =~ m{\A([augo]+)([=+-])([rwx]+)\z} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clause =~ /\A([augo]+)([=+-])([rwx]+)\z/) { } 0 0 0
105 0 my ( $who, $action, $perms ) = ( $1, $2, $3 );
106 0 $who =~ s/a/ugo/g;
107 0 for my $w ( split //, $who ) {
108 0 my $p = 0;
109 0 $p |= $MODEBITS{"$w$_"} for split //, $perms;
110 0 ▶ if ( $action eq '=' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq '=') { } 0 0 0
111 0 $mode = ( $mode & ~$MODEBITS{"${w}m"} ) | $p;
112 }
113 else {
114 0 ▶ $mode = $action eq "+" ? ( $mode | $p ) : ( $mode & ~$p );
Branch: 1 branch
BranchTrueFalseTotal
$action eq '+' ? : 0 0 0
115 }
116 }
117 }
118 else {
119 0 Carp::croak("Invalid mode clause '$clause' for chmod()");
120 }
121 }
122 0 return $mode;
123 }
124  
125 # flock doesn't work on NFS on BSD or on some filesystems like lustre.
126 # Since program authors often can't control or detect that, we warn once
127 # instead of being fatal if we can detect it and people who need it strict
128 # can fatalize the 'flock' category
129  
130 #<<< No perltidy
131 7 ▶{ package flock; use warnings::register }
Subroutine BEGIN: called CC=1 SCAR=0.0
132 #>>>
133  
134 7 my $WARNED_NO_FLOCK = 0;
135  
136 55 ▶sub _throw {
Subroutine _throw: called CC=7 SCAR=28.4
137 55 my ( $self, $function, $file, $msg ) = @_;
138 55 ▶ if ( $function =~ /^flock/
Branch: 1 branch
BranchTrueFalseTotal
if ($function =~ /^flock/ and $! =~ /operation not supported|function not implemented/i and not warnings::fatal_enabled('flock')) { } 0 55 55
Condition33%2/6
$function =~ /^flock/ and $! =~ /operation not supported|function not implemented/i and not warnings::fatal_enabled("flock")
!ll&&!rl&&r
5500
$function =~ /^flock/ and $! =~ /operation not supported|function not implemented/i
!ll&&!rl&&r
5500
MC/DC0%0/3
$function =~ /^flock/ and $! =~ /operation not supported|function not implemented/i and not warnings::fatal_enabled("flock")
$function =~ /^flock/ $! =~ /operation not supported|function not implemented/i not warnings::fatal_enabled("flock")
Truth table25%1/4
A and B and not(C)
A= $function =~ /^flock/B= $! =~ /operation not supported|function not implemented/iC= not warnings::fatal_enabled("flock")
ABCresult
0XX0
10X0
1100
1111
139 && $! =~ /operation not supported|function not implemented/i
140 && !warnings::fatal_enabled('flock') )
141 {
142 0 ▶ if ( !$WARNED_NO_FLOCK ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($WARNED_NO_FLOCK) 0 0 0
143 0 warnings::warn( flock => "Flock not available: '$!': continuing in unsafe mode" );
144 0 $WARNED_NO_FLOCK++;
145 }
146 }
147 else {
148 55 ▶ $msg = $! unless defined $msg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msg 55 0 55
149 55 ▶ Path::Tiny::Error->throw( $function, ( defined $file ? $file : $self->[PATH] ),
Branch: 1 branch
BranchTrueFalseTotal
defined $file ? : 0 55 55
150 $msg );
151 }
152 0 return;
153 }
154  
155 # cheapo option validation
156 142 ▶sub _get_args {
Subroutine _get_args: called CC=6 SCAR=22.4
157 142 my ( $raw, @valid ) = @_;
158 142 ▶ if ( defined($raw) && ref($raw) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $raw and ref $raw ne "HASH") 0 142 142
Condition33%1/3
defined $raw and ref $raw ne "HASH"
!ll&&!rl&&r
01420
MC/DC0%0/2
defined $raw and ref $raw ne "HASH"
defined $raw ref $raw ne "HASH"
Truth table33%1/3
A and B
A= defined $rawB= ref $raw ne "HASH"
ABresult
0X0
100
111
159 0 my ( undef, undef, undef, $called_as ) = caller(1);
160 0 $called_as =~ s{^.*::}{};
161 0 Carp::croak("Options for $called_as must be a hash reference");
162 }
163 142 my $cooked = {};
164 142 for my $k (@valid) {
165 213 ▶ $cooked->{$k} = delete $raw->{$k} if exists $raw->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $raw->{$k} 142 71 213
166 }
167 142 ▶ if ( keys %$raw ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$raw) 0 142 142
168 0 my ( undef, undef, undef, $called_as ) = caller(1);
169 0 $called_as =~ s{^.*::}{};
170 0 Carp::croak( "Invalid option(s) for $called_as: " . join( ", ", keys %$raw ) );
171 }
172 142 return $cooked;
173 }
174  
175 #--------------------------------------------------------------------------#
176 # Constructors
177 #--------------------------------------------------------------------------#
178  
179 #pod =construct path
180 #pod
181 #pod $path = path("foo/bar");
182 #pod $path = path("/tmp", "file.txt"); # list
183 #pod $path = path("."); # cwd
184 #pod
185 #pod Constructs a C<Path::Tiny> object. It doesn't matter if you give a file or
186 #pod directory path. It's still up to you to call directory-like methods only on
187 #pod directories and file-like methods only on files. This function is exported
188 #pod automatically by default.
189 #pod
190 #pod The first argument must be defined and have non-zero length or an exception
191 #pod will be thrown. This prevents subtle, dangerous errors with code like
192 #pod C<< path( maybe_undef() )->remove_tree >>.
193 #pod
194 #pod B<DEPRECATED>: If and only if the B<first> character of the B<first> argument
195 #pod to C<path> is a tilde ('~'), then tilde replacement will be applied to the
196 #pod first path segment. A single tilde will be replaced with C<glob('~')> and a
197 #pod tilde followed by a username will be replaced with output of
198 #pod C<glob('~username')>. B<No other method does tilde expansion on its arguments>.
199 #pod See L</Tilde expansion (deprecated)> for more.
200 #pod
201 #pod On Windows, if the path consists of a drive identifier without a path component
202 #pod (C<C:> or C<D:>), it will be expanded to the absolute path of the current
203 #pod directory on that volume using C<Cwd::getdcwd()>.
204 #pod
205 #pod If called with a single C<Path::Tiny> argument, the original is returned unless
206 #pod the original is holding a temporary file or directory reference in which case a
207 #pod stringified copy is made.
208 #pod
209 #pod $path = path("foo/bar");
210 #pod $temp = Path::Tiny->tempfile;
211 #pod
212 #pod $p2 = path($path); # like $p2 = $path
213 #pod $t2 = path($temp); # like $t2 = path( "$temp" )
214 #pod
215 #pod This optimizes copies without proliferating references unexpectedly if a copy is
216 #pod made by code outside your control.
217 #pod
218 #pod Current API available since 0.017.
219 #pod
220 #pod =cut
221  
222 118 ▶sub path {
Subroutine path: called CC=9 SCAR=32.2
Pod documented
223 118 my $path = shift;
224 Carp::croak("Path::Tiny paths require defined, positive-length parts")
225 118 ▶ unless 1 + @_ == grep { defined && length } $path, @_;
Branch: 1 branch
BranchTrueFalseTotal
unless 1 + @_ == grep({defined $_ and length $_;} $path, @_) 0 118 118
Condition33%1/3
defined $_ && length $_
!ll&&!rl&&r
00118
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
226  
227 # non-temp Path::Tiny objects are effectively immutable and can be reused
228 118 ▶ if ( !@_ && ref($path) eq __PACKAGE__ && !$path->[TEMP] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @_ and ref $path eq __PACKAGE__ and not $path->[5]) 0 118 118
Condition33%2/6
not @_ and ref $path eq __PACKAGE__ and not $path->[5]
!ll&&!rl&&r
11800
not @_ and ref $path eq __PACKAGE__
!ll&&!rl&&r
01180
MC/DC0%0/3
not @_ and ref $path eq __PACKAGE__ and not $path->[5]
not @_ ref $path eq __PACKAGE__ not $path->[5]
Truth table25%1/4
not(A) and B and not(C)
A= not @_B= ref $path eq __PACKAGE__C= not $path->[5]
ABCresult
0XX0
10X0
1100
1111
229 0 return $path;
230 }
231  
232 # stringify objects
233 118 $path = "$path";
234  
235 # do any tilde expansions
236 118 my ($tilde) = $path =~ m{^(~[^/]*)};
237 118 ▶ if ( defined $tilde ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tilde) 0 118 118
238 # Escape File::Glob metacharacters
239 0 (my $escaped = $tilde) =~ s/([\[\{\*\?\\])/\\$1/g;
240 0 require File::Glob;
241 0 my ($homedir) = File::Glob::bsd_glob($escaped);
242 0 ▶ if (defined $homedir && ! $File::Glob::ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $homedir and not $File::Glob::ERROR) 0 0 0
Condition0%0/3
defined $homedir and not $File::Glob::ERROR
!ll&&!rl&&r
000
MC/DC0%0/2
defined $homedir and not $File::Glob::ERROR
defined $homedir not $File::Glob::ERROR
Truth table0%0/3
A and not(B)
A= defined $homedirB= not $File::Glob::ERROR
ABresult
0X0
100
111
243 0 $homedir =~ tr[\\][/] if IS_WIN32();
244 0 $path =~ s{^\Q$tilde\E}{$homedir};
245 }
246 }
247  
248 118 unshift @_, $path;
249 118 goto &_pathify;
250 }
251  
252 # _path is like path but without tilde expansion
253 26 ▶sub _path {
Subroutine _path: called CC=6 SCAR=20.8
254 26 my $path = shift;
255 Carp::croak("Path::Tiny paths require defined, positive-length parts")
256 26 ▶ unless 1 + @_ == grep { defined && length } $path, @_;
Branch: 1 branch
BranchTrueFalseTotal
unless 1 + @_ == grep({defined $_ and length $_;} $path, @_) 0 26 26
Condition33%1/3
defined $_ && length $_
!ll&&!rl&&r
0039
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
257  
258 # non-temp Path::Tiny objects are effectively immutable and can be reused
259 26 ▶ if ( !@_ && ref($path) eq __PACKAGE__ && !$path->[TEMP] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @_ and ref $path eq __PACKAGE__ and not $path->[5]) 0 26 26
Condition50%3/6
not @_ and ref $path eq __PACKAGE__ and not $path->[5]
!ll&&!rl&&r
2600
not @_ and ref $path eq __PACKAGE__
!ll&&!rl&&r
13130
MC/DC0%0/3
not @_ and ref $path eq __PACKAGE__ and not $path->[5]
not @_ ref $path eq __PACKAGE__ not $path->[5]
Truth table50%2/4
not(A) and B and not(C)
A= not @_B= ref $path eq __PACKAGE__C= not $path->[5]
ABCresult
0XX0
10X0
1100
1111
260 0 return $path;
261 }
262  
263 # stringify objects
264 26 $path = "$path";
265  
266 26 unshift @_, $path;
267 26 goto &_pathify;
268 }
269  
270 # _pathify expects one or more string arguments, then joins and canonicalizes
271 # them into an object.
272 144 ▶sub _pathify {
Subroutine _pathify: called CC=5 SCAR=16.5
273 144 my $path = shift;
274  
275 # expand relative volume paths on windows; put trailing slash on UNC root
276 144 if ( IS_WIN32() ) {
277 $path = _win32_vol( $path, $1 ) if $path =~ m{^($DRV_VOL)(?:$NOTSLASH|\z)};
278 $path .= "/" if $path =~ m{^$UNC_VOL\z};
279 }
280  
281 # concatenations stringifies objects, too
282 144 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 13 131 144
283 13 ▶ $path .= ( _is_root($path) ? "" : "/" ) . join( "/", @_ );
Branch: 1 branch
BranchTrueFalseTotal
_is_root($path) ? : 0 13 13
284 }
285  
286  
287 # canonicalize, but with unix slashes and put back trailing volume slash
288 144 my $cpath = $path = File::Spec->canonpath($path);
289 144 $path =~ tr[\\][/] if IS_WIN32();
290 144 ▶ $path = "/" if $path eq '/..'; # for old File::Spec
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "/.." 0 144 144
291 144 $path .= "/" if IS_WIN32() && $path =~ m{^$UNC_VOL\z};
292  
293 # root paths must always have a trailing slash, but other paths must not
294 144 ▶ if ( _is_root($path) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_root($path)) { } 0 144 144
295 0 $path =~ s{/?\z}{/};
296 }
297 else {
298 144 $path =~ s{/\z}{};
299 }
300  
301 144 bless [ $path, $cpath ], __PACKAGE__;
302 }
303  
304 #pod =construct new
305 #pod
306 #pod $path = Path::Tiny->new("foo/bar");
307 #pod
308 #pod This is just like C<path>, but with method call overhead. (Why would you
309 #pod do that?)
310 #pod
311 #pod Current API available since 0.001.
312 #pod
313 #pod =cut
314  
315 0 ▶sub new { shift; path(@_) }
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
316  
317 #pod =construct cwd
318 #pod
319 #pod $path = Path::Tiny->cwd; # path( Cwd::getcwd )
320 #pod $path = cwd; # optional export
321 #pod
322 #pod Gives you the absolute path to the current directory as a C<Path::Tiny> object.
323 #pod This is slightly faster than C<< path(".")->absolute >>.
324 #pod
325 #pod C<cwd> may be exported on request and used as a function instead of as a
326 #pod method.
327 #pod
328 #pod Current API available since 0.018.
329 #pod
330 #pod =cut
331  
332 0 ▶sub cwd {
Subroutine cwd: not called CC=1 SCAR=6.9
Pod documented
333 0 require Cwd;
334 0 return _path( Cwd::getcwd() );
335 }
336  
337 #pod =construct rootdir
338 #pod
339 #pod $path = Path::Tiny->rootdir; # /
340 #pod $path = rootdir; # optional export
341 #pod
342 #pod Gives you C<< File::Spec->rootdir >> as a C<Path::Tiny> object if you're too
343 #pod picky for C<path("/")>.
344 #pod
345 #pod C<rootdir> may be exported on request and used as a function instead of as a
346 #pod method.
347 #pod
348 #pod Current API available since 0.018.
349 #pod
350 #pod =cut
351  
352 0 ▶sub rootdir { _path( File::Spec->rootdir ) }
Subroutine rootdir: not called CC=1 SCAR=6.9
Pod documented
353  
354 #pod =construct tempfile, tempdir
355 #pod
356 #pod $temp = Path::Tiny->tempfile( @options );
357 #pod $temp = Path::Tiny->tempdir( @options );
358 #pod $temp = $dirpath->tempfile( @options );
359 #pod $temp = $dirpath->tempdir( @options );
360 #pod $temp = tempfile( @options ); # optional export
361 #pod $temp = tempdir( @options ); # optional export
362 #pod
363 #pod C<tempfile> passes the options to C<< File::Temp->new >> and returns a
364 #pod C<Path::Tiny> object with the file name. The C<TMPDIR> option will be enabled
365 #pod by default, but you can override that by passing C<< TMPDIR => 0 >> along with
366 #pod the options. (If you use an absolute C<TEMPLATE> option, you will want to
367 #pod disable C<TMPDIR>.)
368 #pod
369 #pod The resulting C<File::Temp> object is cached. When the C<Path::Tiny> object is
370 #pod destroyed, the C<File::Temp> object will be as well.
371 #pod
372 #pod C<File::Temp> annoyingly requires you to specify a custom template in slightly
373 #pod different ways depending on which function or method you call, but
374 #pod C<Path::Tiny> lets you ignore that and can take either a leading template or a
375 #pod C<TEMPLATE> option and does the right thing.
376 #pod
377 #pod $temp = Path::Tiny->tempfile( "customXXXXXXXX" ); # ok
378 #pod $temp = Path::Tiny->tempfile( TEMPLATE => "customXXXXXXXX" ); # ok
379 #pod
380 #pod The tempfile path object will be normalized to have an absolute path, even if
381 #pod created in a relative directory using C<DIR>. If you want it to have
382 #pod the C<realpath> instead, pass a leading options hash like this:
383 #pod
384 #pod $real_temp = tempfile({realpath => 1}, @options);
385 #pod
386 #pod C<tempdir> is just like C<tempfile>, except it calls
387 #pod C<< File::Temp->newdir >> instead.
388 #pod
389 #pod Both C<tempfile> and C<tempdir> may be exported on request and used as
390 #pod functions instead of as methods.
391 #pod
392 #pod The methods can be called on an instances representing a
393 #pod directory. In this case, the directory is used as the base to create the
394 #pod temporary file/directory, setting the C<DIR> option in File::Temp.
395 #pod
396 #pod my $target_dir = path('/to/destination');
397 #pod my $tempfile = $target_dir->tempfile('foobarXXXXXX');
398 #pod $tempfile->spew('A lot of data...'); # not atomic
399 #pod $tempfile->move($target_dir->child('foobar')); # hopefully atomic
400 #pod
401 #pod In this case, any value set for option C<DIR> is ignored.
402 #pod
403 #pod B<Note>: for tempfiles, the filehandles from File::Temp are closed and not
404 #pod reused. This is not as secure as using File::Temp handles directly, but is
405 #pod less prone to deadlocks or access problems on some platforms. Think of what
406 #pod C<Path::Tiny> gives you to be just a temporary file B<name> that gets cleaned
407 #pod up.
408 #pod
409 #pod B<Note 2>: if you don't want these cleaned up automatically when the object
410 #pod is destroyed, File::Temp requires different options for directories and
411 #pod files. Use C<< CLEANUP => 0 >> for directories and C<< UNLINK => 0 >> for
412 #pod files.
413 #pod
414 #pod B<Note 3>: Don't lose the temporary object by chaining a method call instead
415 #pod of storing it:
416 #pod
417 #pod my $lost = tempdir()->child("foo"); # tempdir cleaned up right away
418 #pod
419 #pod B<Note 4>: The cached object may be accessed with the L</cached_temp> method.
420 #pod Keeping a reference to, or modifying the cached object may break the
421 #pod behavior documented above and is not supported. Use at your own risk.
422 #pod
423 #pod Current API available since 0.119.
424 #pod
425 #pod =cut
426  
427 0 ▶sub tempfile {
Subroutine tempfile: not called CC=3 SCAR=24.8
Pod documented
428 0 my ( $opts, $maybe_template, $args )
429 = _parse_file_temp_args(tempfile => @_);
430  
431 # File::Temp->new demands TEMPLATE
432 0 ▶ $args->{TEMPLATE} = $maybe_template->[0] if @$maybe_template;
Branch: 1 branch
BranchTrueFalseTotal
if @$maybe_template 0 0 0
433  
434 0 require File::Temp;
435 0 my $temp = File::Temp->new( TMPDIR => 1, %$args );
436 0 close $temp;
437 0 ▶ my $self = $opts->{realpath} ? _path($temp)->realpath : _path($temp)->absolute;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'realpath'} ? : 0 0 0
438 0 $self->[TEMP] = $temp; # keep object alive while we are
439 0 return $self;
440 }
441  
442 0 ▶sub tempdir {
Subroutine tempdir: not called CC=2 SCAR=17.9
Pod documented
443 0 my ( $opts, $maybe_template, $args )
444 = _parse_file_temp_args(tempdir => @_);
445  
446 0 require File::Temp;
447 0 my $temp = File::Temp->newdir( @$maybe_template, TMPDIR => 1, %$args );
448 0 ▶ my $self = $opts->{realpath} ? _path($temp)->realpath : _path($temp)->absolute;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'realpath'} ? : 0 0 0
449 0 $self->[TEMP] = $temp; # keep object alive while we are
450 # Some ActiveState Perls for Windows break Cwd in ways that lead
451 # File::Temp to get confused about what path to remove; this
452 # monkey-patches the object with our own view of the absolute path
453 0 $temp->{REALNAME} = $self->[CANON] if IS_WIN32;
454 0 return $self;
455 }
456  
457 # normalize the various ways File::Temp does templates
458 0 ▶sub _parse_file_temp_args {
Subroutine _parse_file_temp_args: not called CC=11 SCAR=48.8
459 0 my $called_as = shift;
460 0 ▶ if ( @_ && $_[0] eq 'Path::Tiny' ) { shift } # class method
Branch: 2 branches
BranchTrueFalseTotal
if (@_ and $_[0] eq 'Path::Tiny') { } 0 0 0
elsif (@_ and eval { do { $_[0]->isa('Path::Tiny') } }) { } 0 0 0
Condition0%0/3
@_ and $_[0] eq "Path::Tiny"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and $_[0] eq "Path::Tiny"
@_ $_[0] eq "Path::Tiny"
Truth table0%0/3
A and B
A= @_B= $_[0] eq "Path::Tiny"
ABresult
0X0
100
111
461 0 ▶ elsif ( @_ && eval{$_[0]->isa('Path::Tiny')} ) {
Condition0%0/3
@_ and eval { do { $_[0]->isa("Path::Tiny") } }
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and eval { do { $_[0]->isa("Path::Tiny") } }
@_ eval { do { $_[0]->isa("Path::Tiny") } }
Truth table0%0/3
A and B
A= @_B= eval { do { $_[0]->isa("Path::Tiny") } }
ABresult
0X0
100
111
462 0 my $dir = shift;
463 0 ▶ if (! $dir->is_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dir->is_dir) 0 0 0
464 0 $dir->_throw( $called_as, $dir, "is not a directory object" );
465 }
466 0 push @_, DIR => $dir->stringify; # no overriding
467 }
468 0 ▶ my $opts = ( @_ && ref $_[0] eq 'HASH' ) ? shift @_ : {};
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] eq 'HASH' ? : 0 0 0
Condition0%0/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and ref $_[0] eq "HASH"
@_ ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= @_B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
469 0 $opts = _get_args( $opts, qw/realpath/ );
470  
471 0 ▶ my $leading_template = ( scalar(@_) % 2 == 1 ? shift(@_) : '' );
Branch: 1 branch
BranchTrueFalseTotal
scalar(@_) % 2 == 1 ? : 0 0 0
472 0 my %args = @_;
473 0 %args = map { uc($_), $args{$_} } keys %args;
474 my @template = (
475 exists $args{TEMPLATE} ? delete $args{TEMPLATE}
476 0 ▶ : $leading_template ? $leading_template
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'TEMPLATE'} ? : 0 0 0
$leading_template ? : 0 0 0
477 : ()
478 );
479  
480 0 return ( $opts, \@template, \%args );
481 }
482  
483 #--------------------------------------------------------------------------#
484 # Private methods
485 #--------------------------------------------------------------------------#
486  
487 13 ▶sub _splitpath {
Subroutine _splitpath: called CC=1 SCAR=0.0
488 13 my ($self) = @_;
489 13 @{$self}[ VOL, DIR, FILE ] = File::Spec->splitpath( $self->[PATH] );
490 }
491  
492 0 ▶sub _resolve_symlinks {
Subroutine _resolve_symlinks: not called CC=6 SCAR=37.4
493 0 my ($self) = @_;
494 0 my $new = $self;
495 0 my ( $count, %seen ) = 0;
496 0 ▶ while ( -l $new->[PATH] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-l $new->[0]) 0 0 0
497 0 ▶ if ( $seen{ $new->[PATH] }++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$new->[0]}++) 0 0 0
498 0 $self->_throw( 'readlink', $self->[PATH], "symlink loop detected" );
499 }
500 0 ▶ if ( ++$count > 100 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$count > 100) 0 0 0
501 0 $self->_throw( 'readlink', $self->[PATH], "maximum symlink depth exceeded" );
502 }
503 0 my $resolved = readlink $new->[PATH];
504 0 ▶ $new->_throw( 'readlink', $new->[PATH] ) unless defined $resolved;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $resolved 0 0 0
505 0 $resolved = _path($resolved);
506 0 ▶ $new = $resolved->is_absolute ? $resolved : $new->sibling($resolved);
Branch: 1 branch
BranchTrueFalseTotal
$resolved->is_absolute ? : 0 0 0
507 }
508 0 return $new;
509 }
510  
511 0 ▶sub _replacement_path {
Subroutine _replacement_path: not called CC=2 SCAR=17.9
512 0 my ($self) = @_;
513  
514 0 my $unique_suffix = $$ . int( rand( 2**31 ) );
515 0 my $temp = _path( $self . $unique_suffix );
516  
517 # If filename with process+random suffix is too long, use a shorter
518 # version that doesn't preserve the basename.
519 0 ▶ if ( length $temp->basename > 255 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $temp->basename > 255) 0 0 0
520 0 $temp = $self->sibling( "temp" . $unique_suffix );
521 }
522  
523 0 return $temp;
524 }
525  
526 #--------------------------------------------------------------------------#
527 # Public methods
528 #--------------------------------------------------------------------------#
529  
530 #pod =method absolute
531 #pod
532 #pod $abs = path("foo/bar")->absolute;
533 #pod $abs = path("foo/bar")->absolute("/tmp");
534 #pod
535 #pod Returns a new C<Path::Tiny> object with an absolute path (or itself if already
536 #pod absolute). If no argument is given, the current directory is used as the
537 #pod absolute base path. If an argument is given, it will be converted to an
538 #pod absolute path (if it is not already) and used as the absolute base path.
539 #pod
540 #pod This will not resolve upward directories ("foo/../bar") unless C<canonpath>
541 #pod in L<File::Spec> would normally do so on your platform. If you need them
542 #pod resolved, you must call the more expensive C<realpath> method instead.
543 #pod
544 #pod On Windows, an absolute path without a volume component will have it added
545 #pod based on the current drive.
546 #pod
547 #pod Current API available since 0.101.
548 #pod
549 #pod =cut
550  
551 0 ▶sub absolute {
Subroutine absolute: not called CC=4 SCAR=30.0
Pod documented
552 0 my ( $self, $base ) = @_;
553  
554 # absolute paths handled differently by OS
555 0 if (IS_WIN32) {
556 return $self if length $self->volume;
557 # add missing volume
558 if ( $self->is_absolute ) {
559 require Cwd;
560 # use Win32::GetCwd not Cwd::getdcwd because we're sure
561 # to have the former but not necessarily the latter
562 my ($drv) = Win32::GetCwd() =~ /^($DRV_VOL | $UNC_VOL)/x;
563 return _path( $drv . $self->[PATH] );
564 }
565 }
566 else {
567 0 ▶ return $self if $self->is_absolute;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_absolute 0 0 0
568 }
569  
570 # no base means use current directory as base
571 0 require Cwd;
572 0 ▶ return _path( Cwd::getcwd(), $_[0]->[PATH] ) unless defined $base;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $base 0 0 0
573  
574 # relative base should be made absolute; we check is_absolute rather
575 # than unconditionally make base absolute so that "/foo" doesn't become
576 # "C:/foo" on Windows.
577 0 $base = _path($base);
578 0 ▶ return _path( ( $base->is_absolute ? $base : $base->absolute ), $_[0]->[PATH] );
Branch: 1 branch
BranchTrueFalseTotal
$base->is_absolute ? : 0 0 0
579 }
580  
581 #pod =method append, append_raw, append_utf8
582 #pod
583 #pod path("foo.txt")->append(@data);
584 #pod path("foo.txt")->append(\@data);
585 #pod path("foo.txt")->append({binmode => ":raw"}, @data);
586 #pod path("foo.txt")->append_raw(@data);
587 #pod path("foo.txt")->append_utf8(@data);
588 #pod
589 #pod Appends data to a file. The file is locked with C<flock> prior to writing
590 #pod and closed afterwards. An optional hash reference may be used to pass
591 #pod options. Valid options are:
592 #pod
593 #pod =for :list
594 #pod * C<binmode>: passed to C<binmode()> on the handle used for writing.
595 #pod * C<truncate>: truncates the file after locking and before appending
596 #pod
597 #pod The C<truncate> option is a way to replace the contents of a file
598 #pod B<in place>, unlike L</spew> which writes to a temporary file and then
599 #pod replaces the original (if it exists).
600 #pod
601 #pod C<append_raw> is like C<append> with a C<binmode> of C<:unix> for a fast,
602 #pod unbuffered, raw write.
603 #pod
604 #pod C<append_utf8> is like C<append> with an unbuffered C<binmode>
605 #pod C<:unix:encoding(UTF-8)> (or C<:unix:utf8_strict> with
606 #pod L<PerlIO::utf8_strict>). If L<Unicode::UTF8> 0.58+ is installed, an
607 #pod unbuffered, raw append will be done instead on the data encoded with
608 #pod C<Unicode::UTF8>.
609 #pod
610 #pod Current API available since 0.060.
611 #pod
612 #pod =cut
613  
614 0 ▶sub append {
Subroutine append: not called CC=9 SCAR=45.0
Pod documented
615 0 my ( $self, @data ) = @_;
616 0 ▶ my $args = ( @data && ref $data[0] eq 'HASH' ) ? shift @data : {};
Branch: 1 branch
BranchTrueFalseTotal
@data && ref $data[0] eq 'HASH' ? : 0 0 0
Condition0%0/3
@data and ref $data[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@data and ref $data[0] eq "HASH"
@data ref $data[0] eq "HASH"
Truth table0%0/3
A and B
A= @dataB= ref $data[0] eq "HASH"
ABresult
0X0
100
111
617 0 $args = _get_args( $args, qw/binmode truncate/ );
618 0 my $binmode = $args->{binmode};
619 0 ▶ $binmode = ( ( caller(0) )[10] || {} )->{'open>'} unless defined $binmode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $binmode 0 0 0
Condition0%0/2
(caller 0)[10] || {}
l!l
00
MC/DC0%0/1
(caller 0)[10] || {}
(caller 0)[10]
Truth table0%0/2
A
A= (caller 0)[10]
Aresult
11
00
620 0 ▶ my $mode = $args->{truncate} ? ">" : ">>";
Branch: 1 branch
BranchTrueFalseTotal
$args->{'truncate'} ? : 0 0 0
621 0 my $fh = $self->filehandle( { locked => 1 }, $mode, $binmode );
622 0 ▶ print( {$fh} map { ref eq 'ARRAY' ? @$_ : $_ } @data ) or $self->_throw('print');
Branch: 2 branches
BranchTrueFalseTotal
print {$fh;} map({ref $_ eq "ARRAY" ? @$_ : $_;} @data) or $self->_throw("print") 0 0 0
ref $_ eq 'ARRAY' ? : 0 0 0
623 0 ▶ close $fh or $self->_throw('close');
Branch: 1 branch
BranchTrueFalseTotal
close $fh or $self->_throw("close") 0 0 0
624 }
625  
626 0 ▶sub append_raw {
Subroutine append_raw: not called CC=3 SCAR=24.8
Pod documented
627 0 my ( $self, @data ) = @_;
628 0 ▶ my $args = ( @data && ref $data[0] eq 'HASH' ) ? shift @data : {};
Branch: 1 branch
BranchTrueFalseTotal
@data && ref $data[0] eq 'HASH' ? : 0 0 0
Condition0%0/3
@data and ref $data[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@data and ref $data[0] eq "HASH"
@data ref $data[0] eq "HASH"
Truth table0%0/3
A and B
A= @dataB= ref $data[0] eq "HASH"
ABresult
0X0
100
111
629 0 $args = _get_args( $args, qw/binmode truncate/ );
630 0 $args->{binmode} = ':unix';
631 0 append( $self, $args, @data );
632 }
633  
634 0 ▶sub append_utf8 {
Subroutine append_utf8: not called CC=7 SCAR=40.3
Pod documented
635 0 my ( $self, @data ) = @_;
636 0 ▶ my $args = ( @data && ref $data[0] eq 'HASH' ) ? shift @data : {};
Branch: 1 branch
BranchTrueFalseTotal
@data && ref $data[0] eq 'HASH' ? : 0 0 0
Condition0%0/3
@data and ref $data[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@data and ref $data[0] eq "HASH"
@data ref $data[0] eq "HASH"
Truth table0%0/3
A and B
A= @dataB= ref $data[0] eq "HASH"
ABresult
0X0
100
111
637 0 $args = _get_args( $args, qw/binmode truncate/ );
638 0 ▶ if ( defined($HAS_UU) ? $HAS_UU : ( $HAS_UU = _check_UU() ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $HAS_UU ? $HAS_UU : ($HAS_UU = _check_UU())) { } 0 0 0
elsif (defined $HAS_PU ? $HAS_PU : ($HAS_PU = _check_PU())) { } 0 0 0
defined $HAS_UU ? : 0 0 0
639 0 $args->{binmode} = ":unix";
640 0 append( $self, $args, map { Unicode::UTF8::encode_utf8($_) } @data );
641 }
642 ▶ elsif ( defined($HAS_PU) ? $HAS_PU : ( $HAS_PU = _check_PU() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
defined $HAS_PU ? : 0 0 0
643 0 $args->{binmode} = ":unix:utf8_strict";
644 0 append( $self, $args, @data );
645 }
646 else {
647 0 $args->{binmode} = ":unix:encoding(UTF-8)";
648 0 append( $self, $args, @data );
649 }
650 }
651  
652 #pod =method assert
653 #pod
654 #pod $path = path("foo.txt")->assert( sub { $_->exists } );
655 #pod
656 #pod Returns the invocant after asserting that a code reference argument returns
657 #pod true. When the assertion code reference runs, it will have the invocant
658 #pod object in the C<$_> variable. If it returns false, an exception will be
659 #pod thrown. The assertion code reference may also throw its own exception.
660 #pod
661 #pod If no assertion is provided, the invocant is returned without error.
662 #pod
663 #pod Current API available since 0.062.
664 #pod
665 #pod =cut
666  
667 0 ▶sub assert {
Subroutine assert: not called CC=4 SCAR=30.0
Pod documented
668 0 my ( $self, $assertion ) = @_;
669 0 ▶ return $self unless $assertion;
Branch: 1 branch
BranchTrueFalseTotal
unless $assertion 0 0 0
670 0 ▶ if ( ref $assertion eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $assertion eq 'CODE') { } 0 0 0
671 0 local $_ = $self;
672 0 ▶ $assertion->()
Branch: 1 branch
BranchTrueFalseTotal
&$assertion() or "Path::Tiny::Error"->throw("assert", $self->[0], "failed assertion") 0 0 0
673 or Path::Tiny::Error->throw( "assert", $self->[PATH], "failed assertion" );
674 }
675 else {
676 0 Carp::croak("argument to assert must be a code reference argument");
677 }
678 0 return $self;
679 }
680  
681 #pod =method basename
682 #pod
683 #pod $name = path("foo/bar.txt")->basename; # bar.txt
684 #pod $name = path("foo.txt")->basename('.txt'); # foo
685 #pod $name = path("foo.txt")->basename(qr/.txt/); # foo
686 #pod $name = path("foo.txt")->basename(@suffixes);
687 #pod
688 #pod Returns the file portion or last directory portion of a path.
689 #pod
690 #pod Given a list of suffixes as strings or regular expressions, any that match at
691 #pod the end of the file portion or last directory portion will be removed before
692 #pod the result is returned.
693 #pod
694 #pod Current API available since 0.054.
695 #pod
696 #pod =cut
697  
698 0 ▶sub basename {
Subroutine basename: not called CC=5 SCAR=34.0
Pod documented
699 0 my ( $self, @suffixes ) = @_;
700 0 ▶ $self->_splitpath unless defined $self->[FILE];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->[4] 0 0 0
701 0 my $file = $self->[FILE];
702 0 for my $s (@suffixes) {
703 0 ▶ my $re = ref($s) eq 'Regexp' ? qr/$s\z/ : qr/\Q$s\E\z/;
Branch: 1 branch
BranchTrueFalseTotal
ref $s eq 'Regexp' ? : 0 0 0
704 0 ▶ last if $file =~ s/$re//;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ s/$re// 0 0 0
705 }
706 0 return $file;
707 }
708  
709 #pod =method canonpath
710 #pod
711 #pod $canonical = path("foo/bar")->canonpath; # foo\bar on Windows
712 #pod
713 #pod Returns a string with the canonical format of the path name for
714 #pod the platform. In particular, this means directory separators
715 #pod will be C<\> on Windows.
716 #pod
717 #pod Current API available since 0.001.
718 #pod
719 #pod =cut
720  
721 0 ▶sub canonpath { $_[0]->[CANON] }
Subroutine canonpath: not called CC=1 SCAR=6.9
Pod documented
722  
723 #pod =method cached_temp
724 #pod
725 #pod Returns the cached C<File::Temp> or C<File::Temp::Dir> object if the
726 #pod C<Path::Tiny> object was created with C</tempfile> or C</tempdir>.
727 #pod If there is no such object, this method throws.
728 #pod
729 #pod B<WARNING>: Keeping a reference to, or modifying the cached object may
730 #pod break the behavior documented for temporary files and directories created
731 #pod with C<Path::Tiny> and is not supported. Use at your own risk.
732 #pod
733 #pod Current API available since 0.101.
734 #pod
735 #pod =cut
736  
737 0 ▶sub cached_temp {
Subroutine cached_temp: not called CC=2 SCAR=17.9
Pod documented
738 0 my $self = shift;
739 0 ▶ $self->_throw( "cached_temp", $self, "has no cached File::Temp object" )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->[5] 0 0 0
740 unless defined $self->[TEMP];
741 0 return $self->[TEMP];
742 }
743  
744 #pod =method child
745 #pod
746 #pod $file = path("/tmp")->child("foo.txt"); # "/tmp/foo.txt"
747 #pod $file = path("/tmp")->child(@parts);
748 #pod
749 #pod Returns a new C<Path::Tiny> object relative to the original. Works
750 #pod like C<catfile> or C<catdir> from File::Spec, but without caring about
751 #pod file or directories.
752 #pod
753 #pod B<WARNING>: because the argument could contain C<..> or refer to symlinks,
754 #pod there is no guarantee that the new path refers to an actual descendent of
755 #pod the original. If this is important to you, transform parent and child with
756 #pod L</realpath> and check them with L</subsumes>.
757 #pod
758 #pod Current API available since 0.001.
759 #pod
760 #pod =cut
761  
762 13 ▶sub child {
Subroutine child: called CC=1 SCAR=0.0
Pod documented
763 13 my ( $self, @parts ) = @_;
764 13 return _path( $self->[PATH], @parts );
765 }
766  
767 #pod =method children
768 #pod
769 #pod @paths = path("/tmp")->children;
770 #pod @paths = path("/tmp")->children( qr/\.txt\z/ );
771 #pod
772 #pod Returns a list of C<Path::Tiny> objects for all files and directories
773 #pod within a directory. Excludes "." and ".." automatically.
774 #pod
775 #pod If an optional C<qr//> argument is provided, it only returns objects for child
776 #pod names that match the given regular expression. Only the base name is used
777 #pod for matching:
778 #pod
779 #pod @paths = path("/tmp")->children( qr/^foo/ );
780 #pod # matches children like the glob foo*
781 #pod
782 #pod Current API available since 0.028.
783 #pod
784 #pod =cut
785  
786 0 ▶sub children {
Subroutine children: not called CC=9 SCAR=45.0
Pod documented
787 0 my ( $self, $filter ) = @_;
788 0 my $dh;
789 0 ▶ opendir $dh, $self->[PATH] or $self->_throw('opendir');
Branch: 1 branch
BranchTrueFalseTotal
opendir $dh, $self->[0] or $self->_throw("opendir") 0 0 0
790 0 my @children = readdir $dh;
791 0 ▶ closedir $dh or $self->_throw('closedir');
Branch: 1 branch
BranchTrueFalseTotal
closedir $dh or $self->_throw("closedir") 0 0 0
792  
793 0 ▶ if ( not defined $filter ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $filter) { } 0 0 0
elsif ($filter and ref $filter eq 'Regexp') { } 0 0 0
794 0 ▶ @children = grep { $_ ne '.' && $_ ne '..' } @children;
Condition0%0/3
$_ ne '.' && $_ ne '..'
!ll&&!rl&&r
000
MC/DC0%0/2
$_ ne '.' && $_ ne '..'
$_ ne '.' $_ ne '..'
Truth table0%0/3
A && B
A= $_ ne '.'B= $_ ne '..'
ABresult
0X0
100
111
795 }
796 ▶ elsif ( $filter && ref($filter) eq 'Regexp' ) {
Condition0%0/3
$filter and ref $filter eq "Regexp"
!ll&&!rl&&r
000
MC/DC0%0/2
$filter and ref $filter eq "Regexp"
$filter ref $filter eq "Regexp"
Truth table0%0/3
A and B
A= $filterB= ref $filter eq "Regexp"
ABresult
0X0
100
111
797 0 ▶ @children = grep { $_ ne '.' && $_ ne '..' && $_ =~ $filter } @children;
Condition0%0/6
$_ ne '.' && $_ ne '..' && $_ =~ /$filter/
!ll&&!rl&&r
000
$_ ne "." and $_ ne ".."
!ll&&!rl&&r
000
MC/DC0%0/3
$_ ne '.' && $_ ne '..' && $_ =~ /$filter/
$_ ne "." $_ ne ".." $_ =~ /$filter/
Truth table0%0/4
A and B && C
A= $_ ne "."B= $_ ne ".."C= $_ =~ /$filter/
ABCresult
0XX0
10X0
1100
1111
798 }
799 else {
800 0 Carp::croak("Invalid argument '$filter' for children()");
801 }
802  
803 0 return map { _path( $self->[PATH], $_ ) } @children;
804 }
805  
806 #pod =method chmod
807 #pod
808 #pod path("foo.txt")->chmod(0777);
809 #pod path("foo.txt")->chmod("0755");
810 #pod path("foo.txt")->chmod("go-w");
811 #pod path("foo.txt")->chmod("a=r,u+wx");
812 #pod
813 #pod Sets file or directory permissions. The argument can be a numeric mode, a
814 #pod octal string beginning with a "0" or a limited subset of the symbolic mode use
815 #pod by F</bin/chmod>.
816 #pod
817 #pod The symbolic mode must be a comma-delimited list of mode clauses. Clauses must
818 #pod match C<< qr/\A([augo]+)([=+-])([rwx]+)\z/ >>, which defines "who", "op" and
819 #pod "perms" parameters for each clause. Unlike F</bin/chmod>, all three parameters
820 #pod are required for each clause, multiple ops are not allowed and permissions
821 #pod C<stugoX> are not supported. (See L<File::chmod> for more complex needs.)
822 #pod
823 #pod Current API available since 0.053.
824 #pod
825 #pod =cut
826  
827 0 ▶sub chmod {
Subroutine chmod: not called CC=5 SCAR=34.0
Pod documented
828 0 my ( $self, $new_mode ) = @_;
829  
830 0 my $mode;
831 0 ▶ if ( $new_mode =~ /\d/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($new_mode =~ /\d/) { } 0 0 0
elsif ($new_mode =~ /[=+-]/) { } 0 0 0
832 0 ▶ $mode = ( $new_mode =~ /^0/ ? oct($new_mode) : $new_mode );
Branch: 1 branch
BranchTrueFalseTotal
$new_mode =~ /^0/ ? : 0 0 0
833 }
834 elsif ( $new_mode =~ /[=+-]/ ) {
835 0 $mode = _symbolic_chmod( $self->stat->mode & 07777, $new_mode ); ## no critic
836 }
837 else {
838 0 Carp::croak("Invalid mode argument '$new_mode' for chmod()");
839 }
840  
841 0 ▶ CORE::chmod( $mode, $self->[PATH] ) or $self->_throw("chmod");
Branch: 1 branch
BranchTrueFalseTotal
CORE::chmod $mode, $self->[0] or $self->_throw("chmod") 0 0 0
842  
843 0 return 1;
844 }
845  
846 #pod =method copy
847 #pod
848 #pod path("/tmp/foo.txt")->copy("/tmp/bar.txt");
849 #pod
850 #pod Copies the current path to the given destination using L<File::Copy>'s
851 #pod C<copy> function. Upon success, returns the C<Path::Tiny> object for the
852 #pod newly copied file.
853 #pod
854 #pod Current API available since 0.070.
855 #pod
856 #pod =cut
857  
858 # XXX do recursively for directories?
859 0 ▶sub copy {
Subroutine copy: not called CC=3 SCAR=24.8
Pod documented
860 0 my ( $self, $dest ) = @_;
861 0 require File::Copy;
862 0 ▶ File::Copy::copy( $self->[PATH], $dest )
Branch: 1 branch
BranchTrueFalseTotal
File::Copy::copy($self->[0], $dest) or Carp::croak("copy failed for $self to ${dest}: $!") 0 0 0
863 or Carp::croak("copy failed for $self to $dest: $!");
864  
865 0 ▶ return -d $dest ? _path( $dest, $self->basename ) : _path($dest);
Branch: 1 branch
BranchTrueFalseTotal
-d $dest ? : 0 0 0
866 }
867  
868 #pod =method digest
869 #pod
870 #pod $obj = path("/tmp/foo.txt")->digest; # SHA-256
871 #pod $obj = path("/tmp/foo.txt")->digest("MD5"); # user-selected
872 #pod $obj = path("/tmp/foo.txt")->digest( { chunk_size => 1e6 }, "MD5" );
873 #pod
874 #pod Returns a hexadecimal digest for a file. An optional hash reference of options may
875 #pod be given. The only option is C<chunk_size>. If C<chunk_size> is given, that many
876 #pod bytes will be read at a time. If not provided, the entire file will be slurped
877 #pod into memory to compute the digest.
878 #pod
879 #pod Any subsequent arguments are passed to the constructor for L<Digest> to select
880 #pod an algorithm. If no arguments are given, the default is SHA-256.
881 #pod
882 #pod Current API available since 0.056.
883 #pod
884 #pod =cut
885  
886 0 ▶sub digest {
Subroutine digest: not called CC=7 SCAR=40.3
Pod documented
887 0 my ( $self, @opts ) = @_;
888 0 ▶ my $args = ( @opts && ref $opts[0] eq 'HASH' ) ? shift @opts : {};
Branch: 1 branch
BranchTrueFalseTotal
@opts && ref $opts[0] eq 'HASH' ? : 0 0 0
Condition0%0/3
@opts and ref $opts[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@opts and ref $opts[0] eq "HASH"
@opts ref $opts[0] eq "HASH"
Truth table0%0/3
A and B
A= @optsB= ref $opts[0] eq "HASH"
ABresult
0X0
100
111
889 0 $args = _get_args( $args, qw/chunk_size/ );
890 0 ▶ unshift @opts, 'SHA-256' unless @opts;
Branch: 1 branch
BranchTrueFalseTotal
unless @opts 0 0 0
891 0 require Digest;
892 0 my $digest = Digest->new(@opts);
893 0 ▶ if ( $args->{chunk_size} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'chunk_size'}) { } 0 0 0
894 0 my $fh = $self->filehandle( { locked => 1 }, "<", ":unix" );
895 0 my $buf;
896 0 ▶ while (!eof($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eof $fh) 0 0 0
897 0 my $rc = read $fh, $buf, $args->{chunk_size};
898 0 ▶ $self->_throw('read') unless defined $rc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rc 0 0 0
899 0 $digest->add($buf);
900 }
901 }
902 else {
903 0 $digest->add( $self->slurp_raw );
904 }
905 0 return $digest->hexdigest;
906 }
907  
908 #pod =method dirname (deprecated)
909 #pod
910 #pod $name = path("/tmp/foo.txt")->dirname; # "/tmp/"
911 #pod
912 #pod Returns the directory portion you would get from calling
913 #pod C<< File::Spec->splitpath( $path->stringify ) >> or C<"."> for a path without a
914 #pod parent directory portion. Because L<File::Spec> is inconsistent, the result
915 #pod might or might not have a trailing slash. Because of this, this method is
916 #pod B<deprecated>.
917 #pod
918 #pod A better, more consistently approach is likely C<< $path->parent->stringify >>,
919 #pod which will not have a trailing slash except for a root directory.
920 #pod
921 #pod Deprecated in 0.056.
922 #pod
923 #pod =cut
924  
925 0 ▶sub dirname {
Subroutine dirname: not called CC=3 SCAR=24.8
Pod documented
926 0 my ($self) = @_;
927 0 ▶ $self->_splitpath unless defined $self->[DIR];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->[3] 0 0 0
928 0 ▶ return length $self->[DIR] ? $self->[DIR] : ".";
Branch: 1 branch
BranchTrueFalseTotal
length $self->[3] ? : 0 0 0
929 }
930  
931 #pod =method edit, edit_raw, edit_utf8
932 #pod
933 #pod path("foo.txt")->edit( \&callback, $options );
934 #pod path("foo.txt")->edit_utf8( \&callback );
935 #pod path("foo.txt")->edit_raw( \&callback );
936 #pod
937 #pod These are convenience methods that allow "editing" a file using a single
938 #pod callback argument. They slurp the file using C<slurp>, place the contents
939 #pod inside a localized C<$_> variable, call the callback function (without
940 #pod arguments), and then write C<$_> (presumably mutated) back to the
941 #pod file with C<spew>.
942 #pod
943 #pod An optional hash reference may be used to pass options. The only option is
944 #pod C<binmode>, which is passed to C<slurp> and C<spew>.
945 #pod
946 #pod C<edit_utf8> and C<edit_raw> act like their respective C<slurp_*> and
947 #pod C<spew_*> methods.
948 #pod
949 #pod Current API available since 0.077.
950 #pod
951 #pod =cut
952  
953 0 ▶sub edit {
Subroutine edit: not called CC=4 SCAR=30.0
Pod documented
954 0 my $self = shift;
955 0 my $cb = shift;
956 0 my $args = _get_args( shift, qw/binmode/ );
957 0 ▶ Carp::croak("Callback for edit() must be a code reference")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cb and ref $cb eq "CODE" 0 0 0
Condition0%0/3
defined $cb and ref $cb eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $cb and ref $cb eq "CODE"
defined $cb ref $cb eq "CODE"
Truth table0%0/3
A and B
A= defined $cbB= ref $cb eq "CODE"
ABresult
0X0
100
111
958 unless defined($cb) && ref($cb) eq 'CODE';
959  
960 local $_ =
961 0 ▶ $self->slurp( exists( $args->{binmode} ) ? { binmode => $args->{binmode} } : () );
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'binmode'} ? : 0 0 0
962 0 $cb->();
963 0 $self->spew( $args, $_ );
964  
965 0 return;
966 }
967  
968 # this is done long-hand to benefit from slurp_utf8 optimizations
969 0 ▶sub edit_utf8 {
Subroutine edit_utf8: not called CC=3 SCAR=24.8
Pod documented
970 0 my ( $self, $cb ) = @_;
971 0 ▶ Carp::croak("Callback for edit_utf8() must be a code reference")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cb and ref $cb eq "CODE" 0 0 0
Condition0%0/3
defined $cb and ref $cb eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $cb and ref $cb eq "CODE"
defined $cb ref $cb eq "CODE"
Truth table0%0/3
A and B
A= defined $cbB= ref $cb eq "CODE"
ABresult
0X0
100
111
972 unless defined($cb) && ref($cb) eq 'CODE';
973  
974 0 local $_ = $self->slurp_utf8;
975 0 $cb->();
976 0 $self->spew_utf8($_);
977  
978 0 return;
979 }
980  
981 0 ▶sub edit_raw { $_[2] = { binmode => ":unix" }; goto &edit }
Subroutine edit_raw: not called CC=1 SCAR=6.9
Pod documented
982  
983 #pod =method edit_lines, edit_lines_utf8, edit_lines_raw
984 #pod
985 #pod path("foo.txt")->edit_lines( \&callback, $options );
986 #pod path("foo.txt")->edit_lines_utf8( \&callback );
987 #pod path("foo.txt")->edit_lines_raw( \&callback );
988 #pod
989 #pod These are convenience methods that allow "editing" a file's lines using a
990 #pod single callback argument. They iterate over the file: for each line, the
991 #pod line is put into a localized C<$_> variable, the callback function is
992 #pod executed (without arguments) and then C<$_> is written to a temporary file.
993 #pod When iteration is finished, the temporary file is atomically renamed over
994 #pod the original.
995 #pod
996 #pod An optional hash reference may be used to pass options. The only option is
997 #pod C<binmode>, which is passed to the method that open handles for reading and
998 #pod writing.
999 #pod
1000 #pod C<edit_lines_raw> is like C<edit_lines> with a buffered C<binmode> of
1001 #pod C<:raw>.
1002 #pod
1003 #pod C<edit_lines_utf8> is like C<edit_lines> with a buffered C<binmode>
1004 #pod C<:raw:encoding(UTF-8)> (or C<:raw:utf8_strict> with
1005 #pod L<PerlIO::utf8_strict>).
1006 #pod
1007 #pod Current API available since 0.077.
1008 #pod
1009 #pod =cut
1010  
1011 0 ▶sub edit_lines {
Subroutine edit_lines: not called CC=10 SCAR=47.0
Pod documented
1012 0 my $self = shift;
1013 0 my $cb = shift;
1014 0 my $args = _get_args( shift, qw/binmode/ );
1015 0 ▶ Carp::croak("Callback for edit_lines() must be a code reference")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cb and ref $cb eq "CODE" 0 0 0
Condition0%0/3
defined $cb and ref $cb eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $cb and ref $cb eq "CODE"
defined $cb ref $cb eq "CODE"
Truth table0%0/3
A and B
A= defined $cbB= ref $cb eq "CODE"
ABresult
0X0
100
111
1016 unless defined($cb) && ref($cb) eq 'CODE';
1017  
1018 0 my $binmode = $args->{binmode};
1019 # get default binmode from caller's lexical scope (see "perldoc open")
1020 0 ▶ $binmode = ( ( caller(0) )[10] || {} )->{'open>'} unless defined $binmode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $binmode 0 0 0
Condition0%0/2
(caller 0)[10] || {}
l!l
00
MC/DC0%0/1
(caller 0)[10] || {}
(caller 0)[10]
Truth table0%0/2
A
A= (caller 0)[10]
Aresult
11
00
1021  
1022 # writing needs to follow the link and create the tempfile in the same
1023 # dir for later atomic rename
1024 0 my $resolved_path = $self->_resolve_symlinks;
1025 0 my $temp = $resolved_path->_replacement_path;
1026  
1027 0 my $temp_fh = $temp->filehandle( { exclusive => 1, locked => 1 }, ">", $binmode );
1028 0 my $in_fh = $self->filehandle( { locked => 1 }, '<', $binmode );
1029  
1030 0 local $_;
1031 0 ▶ while (! eof($in_fh) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eof $in_fh) 0 0 0
1032 0 ▶ defined( $_ = readline($in_fh) ) or $self->_throw('readline');
Branch: 1 branch
BranchTrueFalseTotal
defined($_ = readline $in_fh) or $self->_throw("readline") 0 0 0
1033 0 $cb->();
1034 0 ▶ $temp_fh->print($_) or $self->_throw('print', $temp);
Branch: 1 branch
BranchTrueFalseTotal
$temp_fh->print($_) or $self->_throw("print", $temp) 0 0 0
1035 }
1036  
1037 0 ▶ close $temp_fh or $self->_throw( 'close', $temp );
Branch: 1 branch
BranchTrueFalseTotal
close $temp_fh or $self->_throw("close", $temp) 0 0 0
1038 0 ▶ close $in_fh or $self->_throw('close');
Branch: 1 branch
BranchTrueFalseTotal
close $in_fh or $self->_throw("close") 0 0 0
1039  
1040 0 return $temp->move($resolved_path);
1041 }
1042  
1043 0 ▶sub edit_lines_raw { $_[2] = { binmode => ":raw" }; goto &edit_lines }
Subroutine edit_lines_raw: not called CC=1 SCAR=6.9
Pod documented
1044  
1045 0 ▶sub edit_lines_utf8 {
Subroutine edit_lines_utf8: not called CC=3 SCAR=24.8
Pod documented
1046 0 ▶ if ( defined($HAS_PU) ? $HAS_PU : ( $HAS_PU = _check_PU() ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $HAS_PU ? $HAS_PU : ($HAS_PU = _check_PU())) { } 0 0 0
defined $HAS_PU ? : 0 0 0
1047 0 $_[2] = { binmode => ":raw:utf8_strict" };
1048 }
1049 else {
1050 0 $_[2] = { binmode => ":raw:encoding(UTF-8)" };
1051 }
1052 0 goto &edit_lines;
1053 }
1054  
1055 #pod =method exists, is_file, is_dir
1056 #pod
1057 #pod if ( path("/tmp")->exists ) { ... } # -e
1058 #pod if ( path("/tmp")->is_dir ) { ... } # -d
1059 #pod if ( path("/tmp")->is_file ) { ... } # -e && ! -d
1060 #pod
1061 #pod Implements file test operations, this means the file or directory actually has
1062 #pod to exist on the filesystem. Until then, it's just a path.
1063 #pod
1064 #pod B<Note>: C<is_file> is not C<-f> because C<-f> is not the opposite of C<-d>.
1065 #pod C<-f> means "plain file", excluding symlinks, devices, etc. that often can be
1066 #pod read just like files.
1067 #pod
1068 #pod Use C<-f> instead if you really mean to check for a plain file.
1069 #pod
1070 #pod Current API available since 0.053.
1071 #pod
1072 #pod =cut
1073  
1074 0 ▶sub exists { -e $_[0]->[PATH] }
Subroutine exists: not called CC=1 SCAR=6.9
Pod documented
1075  
1076 0 ▶sub is_file { -e $_[0]->[PATH] && !-d _ }
Subroutine is_file: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
-e $_[0][0] and not -d _ 0 0 0
1077  
1078 0 ▶sub is_dir { -d $_[0]->[PATH] }
Subroutine is_dir: not called CC=1 SCAR=6.9
Pod documented
1079  
1080 #pod =method filehandle
1081 #pod
1082 #pod $fh = path("/tmp/foo.txt")->filehandle($mode, $binmode);
1083 #pod $fh = path("/tmp/foo.txt")->filehandle({ locked => 1 }, $mode, $binmode);
1084 #pod $fh = path("/tmp/foo.txt")->filehandle({ exclusive => 1 }, $mode, $binmode);
1085 #pod
1086 #pod Returns an open file handle. The C<$mode> argument must be a Perl-style
1087 #pod read/write mode string ("<" ,">", ">>", etc.). If a C<$binmode>
1088 #pod is given, it is set during the C<open> call.
1089 #pod
1090 #pod An optional hash reference may be used to pass options.
1091 #pod
1092 #pod The C<locked> option governs file locking; if true, handles opened for writing,
1093 #pod appending or read-write are locked with C<LOCK_EX>; otherwise, they are
1094 #pod locked with C<LOCK_SH>. When using C<locked>, ">" or "+>" modes will delay
1095 #pod truncation until after the lock is acquired.
1096 #pod
1097 #pod The C<exclusive> option causes the open() call to fail if the file already
1098 #pod exists. This corresponds to the O_EXCL flag to sysopen / open(2).
1099 #pod C<exclusive> implies C<locked> and will set it for you if you forget it.
1100 #pod
1101 #pod See C<openr>, C<openw>, C<openrw>, and C<opena> for sugar.
1102 #pod
1103 #pod Current API available since 0.066.
1104 #pod
1105 #pod =cut
1106  
1107 # Note: must put binmode on open line, not subsequent binmode() call, so things
1108 # like ":unix" actually stop perlio/crlf from being added
1109  
1110 71 ▶sub filehandle {
Subroutine filehandle: called CC=32 SCAR=54.9
Pod documented
1111 71 my ( $self, @args ) = @_;
1112 71 ▶ my $args = ( @args && ref $args[0] eq 'HASH' ) ? shift @args : {};
Branch: 1 branch
BranchTrueFalseTotal
@args && ref $args[0] eq 'HASH' ? : 71 0 71
Condition33%1/3
@args and ref $args[0] eq "HASH"
!ll&&!rl&&r
0071
MC/DC0%0/2
@args and ref $args[0] eq "HASH"
@args ref $args[0] eq "HASH"
Truth table33%1/3
A and B
A= @argsB= ref $args[0] eq "HASH"
ABresult
0X0
100
111
1113 71 $args = _get_args( $args, qw/locked exclusive/ );
1114 71 ▶ $args->{locked} = 1 if $args->{exclusive};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"exclusive"} 0 71 71
1115 71 my ( $opentype, $binmode ) = @args;
1116  
1117 71 ▶ $opentype = "<" unless defined $opentype;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opentype 0 71 71
1118 Carp::croak("Invalid file mode '$opentype'")
1119 71 ▶ unless grep { $opentype eq $_ } qw/< +< > +> >> +>>/;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$opentype eq $_;} "<", "+<", ">", "+>", ">>", "+>>" 0 71 71
1120  
1121 71 ▶ $binmode = ( ( caller(0) )[10] || {} )->{ 'open' . substr( $opentype, -1, 1 ) }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $binmode 0 71 71
Condition0%0/2
(caller 0)[10] || {}
l!l
00
MC/DC0%0/1
(caller 0)[10] || {}
(caller 0)[10]
Truth table0%0/2
A
A= (caller 0)[10]
Aresult
11
00
1122 unless defined $binmode;
1123 71 ▶ $binmode = "" unless defined $binmode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $binmode 0 71 71
1124  
1125 71 my ( $fh, $lock, $trunc );
1126 71 ▶ if ( $HAS_FLOCK && $args->{locked} && !$ENV{PERL_PATH_TINY_NO_FLOCK} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HAS_FLOCK and $args->{"locked"} and not $ENV{"PERL_PATH_TINY_NO_FLOCK"}) 71 0 71
Condition33%2/6
$HAS_FLOCK and $args->{"locked"} and not $ENV{"PERL_PATH_TINY_NO_FLOCK"}
!ll&&!rl&&r
0071
$HAS_FLOCK and $args->{"locked"}
!ll&&!rl&&r
0071
MC/DC0%0/3
$HAS_FLOCK and $args->{"locked"} and not $ENV{"PERL_PATH_TINY_NO_FLOCK"}
$HAS_FLOCK $args->{"locked"} not $ENV{"PERL_PATH_TINY_NO_FLOCK"}
Truth table25%1/4
A and B and not(C)
A= $HAS_FLOCKB= $args->{"locked"}C= not $ENV{"PERL_PATH_TINY_NO_FLOCK"}
ABCresult
0XX0
10X0
1100
1111
1127 71 require Fcntl;
1128 # truncating file modes shouldn't truncate until lock acquired
1129 71 ▶ if ( grep { $opentype eq $_ } qw( > +> ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (grep {$opentype eq $_;} '>', '+>') { } 0 71 71
elsif ($^O eq 'aix' and $opentype eq '<') { } 0 71 71
1130 # sysopen in write mode without truncation
1131 0 ▶ my $flags = $opentype eq ">" ? Fcntl::O_WRONLY() : Fcntl::O_RDWR();
Branch: 1 branch
BranchTrueFalseTotal
$opentype eq '>' ? : 0 0 0
1132 0 $flags |= Fcntl::O_CREAT();
1133 0 ▶ $flags |= Fcntl::O_EXCL() if $args->{exclusive};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"exclusive"} 0 0 0
1134 0 ▶ sysopen( $fh, $self->[PATH], $flags ) or $self->_throw("sysopen");
Branch: 1 branch
BranchTrueFalseTotal
sysopen $fh, $self->[0], $flags or $self->_throw("sysopen") 0 0 0
1135  
1136 # fix up the binmode since sysopen() can't specify layers like
1137 # open() and binmode() can't start with just :unix like open()
1138 0 ▶ if ( $binmode =~ s/^:unix// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($binmode =~ s/^:unix//) 0 0 0
1139 # eliminate pseudo-layers
1140 0 ▶ binmode( $fh, ":raw" ) or $self->_throw("binmode (:raw)");
Branch: 1 branch
BranchTrueFalseTotal
binmode $fh, ":raw" or $self->_throw("binmode (:raw)") 0 0 0
1141 # strip off real layers until only :unix is left
1142 0 ▶ while ( 1 < ( my $layers =()= PerlIO::get_layers( $fh, output => 1 ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 < (my $layers = () = &PerlIO::get_layers($fh, "output", 1))) 0 0 0
1143 0 ▶ binmode( $fh, ":pop" ) or $self->_throw("binmode (:pop)");
Branch: 1 branch
BranchTrueFalseTotal
binmode $fh, ":pop" or $self->_throw("binmode (:pop)") 0 0 0
1144 }
1145 }
1146  
1147 # apply any remaining binmode layers
1148 0 ▶ if ( length $binmode ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $binmode) 0 0 0
1149 0 ▶ binmode( $fh, $binmode ) or $self->_throw("binmode ($binmode)");
Branch: 1 branch
BranchTrueFalseTotal
binmode $fh, $binmode or $self->_throw("binmode ($binmode)") 0 0 0
1150 }
1151  
1152 # ask for lock and truncation
1153 0 $lock = Fcntl::LOCK_EX();
1154 0 $trunc = 1;
1155 }
1156 ▶ elsif ( $^O eq 'aix' && $opentype eq "<" ) {
Condition33%1/3
$^O eq "aix" and $opentype eq "<"
!ll&&!rl&&r
7100
MC/DC0%0/2
$^O eq "aix" and $opentype eq "<"
$^O eq "aix" $opentype eq "<"
Truth table33%1/3
A and B
A= $^O eq "aix"B= $opentype eq "<"
ABresult
0X0
100
111
1157 # AIX can only lock write handles, so upgrade to RW and LOCK_EX if
1158 # the file is writable; otherwise give up on locking. N.B.
1159 # checking -w before open to determine the open mode is an
1160 # unavoidable race condition
1161 0 ▶ if ( -w $self->[PATH] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-w $self->[0]) 0 0 0
1162 0 $opentype = "+<";
1163 0 $lock = Fcntl::LOCK_EX();
1164 }
1165 }
1166 else {
1167 71 ▶ $lock = $opentype eq "<" ? Fcntl::LOCK_SH() : Fcntl::LOCK_EX();
Branch: 1 branch
BranchTrueFalseTotal
$opentype eq '<' ? : 71 0 71
1168 }
1169 }
1170  
1171 71 ▶ unless ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 71 0 71
1172 71 my $mode = $opentype . $binmode;
1173 71 ▶ open $fh, $mode, $self->[PATH] or $self->_throw("open ($mode)");
Branch: 1 branch
BranchTrueFalseTotal
open $fh, $mode, $self->[0] or $self->_throw("open ($mode)") 16 55 71
1174 }
1175  
1176 16 ▶ do { flock( $fh, $lock ) or $self->_throw("flock ($lock)") } if $lock;
Branch: 2 branches
BranchTrueFalseTotal
if ($lock) 16 0 16
flock $fh, $lock or $self->_throw("flock ($lock)") 16 0 16
1177 0 ▶ do { truncate( $fh, 0 ) or $self->_throw("truncate") } if $trunc;
Branch: 2 branches
BranchTrueFalseTotal
if ($trunc) 0 16 16
truncate $fh, 0 or $self->_throw("truncate") 0 0 0
1178  
1179 16 return $fh;
1180 }
1181  
1182 #pod =method has_same_bytes
1183 #pod
1184 #pod if ( path("foo.txt")->has_same_bytes("bar.txt") ) {
1185 #pod # ...
1186 #pod }
1187 #pod
1188 #pod This method returns true if both the invocant and the argument can be opened as
1189 #pod file handles and the handles contain the same bytes. It returns false if their
1190 #pod contents differ. If either can't be opened as a file (e.g. a directory or
1191 #pod non-existent file), the method throws an exception. If both can be opened and
1192 #pod both have the same C<realpath>, the method returns true without scanning any
1193 #pod data.
1194 #pod
1195 #pod Current API available since 0.125.
1196 #pod
1197 #pod =cut
1198  
1199 0 ▶sub has_same_bytes {
Subroutine has_same_bytes: not called CC=5 SCAR=34.0
Pod documented
1200 0 my ($self, $other_path) = @_;
1201 0 my $other = _path($other_path);
1202  
1203 0 my $fh1 = $self->openr_raw({ locked => 1 });
1204 0 my $fh2 = $other->openr_raw({ locked => 1 });
1205  
1206 # check for directories
1207 0 ▶ if (-d $fh1) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $fh1) 0 0 0
1208 0 $self->_throw('has_same_bytes', $self->[PATH], "directory not allowed");
1209 }
1210 0 ▶ if (-d $fh2) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $fh2) 0 0 0
1211 0 $self->_throw('has_same_bytes', $other->[PATH], "directory not allowed");
1212 }
1213  
1214 # Now that handles are open, we know the inputs are readable files that
1215 # exist, so it's safe to compare via realpath
1216 0 ▶ if ($self->realpath eq $other->realpath) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->realpath eq $other->realpath) 0 0 0
1217 0 return 1
1218 }
1219  
1220 # result is 0 for equal, 1 for unequal, -1 for error
1221 0 require File::Compare;
1222 0 my $res = File::Compare::compare($fh1, $fh2, 65536);
1223 0 ▶ if ($res < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res < 0) 0 0 0
1224 0 $self->_throw('has_same_bytes')
1225 }
1226  
1227 0 return $res == 0;
1228 }
1229  
1230 #pod =method is_absolute, is_relative
1231 #pod
1232 #pod if ( path("/tmp")->is_absolute ) { ... }
1233 #pod if ( path("/tmp")->is_relative ) { ... }
1234 #pod
1235 #pod Booleans for whether the path appears absolute or relative.
1236 #pod
1237 #pod Current API available since 0.001.
1238 #pod
1239 #pod =cut
1240  
1241 0 ▶sub is_absolute { substr( $_[0]->dirname, 0, 1 ) eq '/' }
Subroutine is_absolute: not called CC=1 SCAR=6.9
Pod documented
1242  
1243 0 ▶sub is_relative { substr( $_[0]->dirname, 0, 1 ) ne '/' }
Subroutine is_relative: not called CC=1 SCAR=6.9
Pod documented
1244  
1245 #pod =method is_rootdir
1246 #pod
1247 #pod while ( ! $path->is_rootdir ) {
1248 #pod $path = $path->parent;
1249 #pod ...
1250 #pod }
1251 #pod
1252 #pod Boolean for whether the path is the root directory of the volume. I.e. the
1253 #pod C<dirname> is C<q[/]> and the C<basename> is C<q[]>.
1254 #pod
1255 #pod This works even on C<MSWin32> with drives and UNC volumes:
1256 #pod
1257 #pod path("C:/")->is_rootdir; # true
1258 #pod path("//server/share/")->is_rootdir; #true
1259 #pod
1260 #pod Current API available since 0.038.
1261 #pod
1262 #pod =cut
1263  
1264 0 ▶sub is_rootdir {
Subroutine is_rootdir: not called CC=3 SCAR=24.8
Pod documented
1265 0 my ($self) = @_;
1266 0 ▶ $self->_splitpath unless defined $self->[DIR];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->[3] 0 0 0
1267 0 ▶ return $self->[DIR] eq '/' && $self->[FILE] eq '';
Condition0%0/3
$self->[3] eq '/' && $self->[4] eq ''
!ll&&!rl&&r
000
MC/DC0%0/2
$self->[3] eq '/' && $self->[4] eq ''
$self->[3] eq '/' $self->[4] eq ''
Truth table0%0/3
A && B
A= $self->[3] eq '/'B= $self->[4] eq ''
ABresult
0X0
100
111
1268 }
1269  
1270 #pod =method iterator
1271 #pod
1272 #pod $iter = path("/tmp")->iterator( \%options );
1273 #pod
1274 #pod Returns a code reference that walks a directory lazily. Each invocation
1275 #pod returns a C<Path::Tiny> object or undef when the iterator is exhausted.
1276 #pod
1277 #pod $iter = path("/tmp")->iterator;
1278 #pod while ( $path = $iter->() ) {
1279 #pod ...
1280 #pod }
1281 #pod
1282 #pod The current and parent directory entries ("." and "..") will not
1283 #pod be included.
1284 #pod
1285 #pod If the C<recurse> option is true, the iterator will walk the directory
1286 #pod recursively, breadth-first. If the C<follow_symlinks> option is also true,
1287 #pod directory links will be followed recursively. There is no protection against
1288 #pod loops when following links. If a directory is not readable, it will not be
1289 #pod followed.
1290 #pod
1291 #pod The default is the same as:
1292 #pod
1293 #pod $iter = path("/tmp")->iterator( {
1294 #pod recurse => 0,
1295 #pod follow_symlinks => 0,
1296 #pod } );
1297 #pod
1298 #pod For a more powerful, recursive iterator with built-in loop avoidance, see
1299 #pod L<Path::Iterator::Rule>.
1300 #pod
1301 #pod See also L</visit>.
1302 #pod
1303 #pod Current API available since 0.016.
1304 #pod
1305 #pod =cut
1306  
1307 0 ▶sub iterator {
Subroutine iterator: not called CC=1 SCAR=6.9
Pod documented
1308 0 my $self = shift;
1309 0 my $args = _get_args( shift, qw/recurse follow_symlinks/ );
1310 0 my @dirs = $self;
1311 0 my $current;
1312 0 ▶ return sub {
Subroutine __ANON__: not called CC=16 SCAR=56.1
1313 0 my $next;
1314 0 ▶ while (@dirs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dirs) 0 0 0
1315 0 ▶ if ( ref $dirs[0] eq 'Path::Tiny' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dirs[0] eq "Path::Tiny") 0 0 0
1316 0 ▶ if ( !-r $dirs[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $dirs[0]) 0 0 0
1317 # Directory is missing or not readable, so skip it. There
1318 # is still a race condition possible between the check and
1319 # the opendir, but we can't easily differentiate between
1320 # error cases that are OK to skip and those that we want
1321 # to be exceptions, so we live with the race and let opendir
1322 # be fatal.
1323 0 ▶ shift @dirs and next;
Branch: 1 branch
BranchTrueFalseTotal
shift @dirs and next 0 0 0
1324 }
1325 0 $current = $dirs[0];
1326 0 my $dh;
1327 0 ▶ opendir( $dh, $current->[PATH] )
Branch: 1 branch
BranchTrueFalseTotal
opendir $dh, $current->[0] or $self->_throw("opendir", $current->[0]) 0 0 0
1328 or $self->_throw( 'opendir', $current->[PATH] );
1329 0 $dirs[0] = $dh;
1330 0 ▶ if ( -l $current->[PATH] && !$args->{follow_symlinks} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-l $current->[0] and not $args->{"follow_symlinks"}) 0 0 0
Condition0%0/3
-l $current->[0] and not $args->{"follow_symlinks"}
!ll&&!rl&&r
000
MC/DC0%0/2
-l $current->[0] and not $args->{"follow_symlinks"}
-l $current->[0] not $args->{"follow_symlinks"}
Truth table0%0/3
A and not(B)
A= -l $current->[0]B= not $args->{"follow_symlinks"}
ABresult
0X0
100
111
1331 # Symlink attack! It was a real dir, but is now a symlink!
1332 # N.B. we check *after* opendir so the attacker has to win
1333 # two races: replace dir with symlink before opendir and
1334 # replace symlink with dir before -l check above
1335 0 ▶ shift @dirs and next;
Branch: 1 branch
BranchTrueFalseTotal
shift @dirs and next 0 0 0
1336 }
1337 }
1338 0 ▶ while ( defined( $next = readdir $dirs[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($next = readdir $dirs[0])) 0 0 0
1339 0 ▶ next if $next eq '.' || $next eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $next eq "." or $next eq ".." 0 0 0
Condition0%0/3
$next eq "." or $next eq ".."
l!l&&r!l&&!r
000
MC/DC0%0/2
$next eq "." or $next eq ".."
$next eq "." $next eq ".."
Truth table0%0/3
A or B
A= $next eq "."B= $next eq ".."
ABresult
1X1
011
000
1340 0 my $path = $current->child($next);
1341 push @dirs, $path
1342 0 ▶ if $args->{recurse} && -d $path && !( !$args->{follow_symlinks} && -l $path );
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"recurse"} and -d $path and not !$args->{"follow_symlinks"} && -l $path 0 0 0
Condition0%0/9
$args->{"recurse"} and -d $path and not !$args->{"follow_symlinks"} && -l $path
!ll&&!rl&&r
000
$args->{"recurse"} and -d $path
!ll&&!rl&&r
000
!$args->{'follow_symlinks'} && -l $path
!ll&&!rl&&r
000
MC/DC0%0/4
$args->{"recurse"} and -d $path and not !$args->{"follow_symlinks"} && -l $path
$args->{"recurse"} -d $path !$args->{'follow_symlinks'} -l $path
Truth table0%0/5
A and B and not(not(C) && D)
A= $args->{"recurse"}B= -d $pathC= !$args->{'follow_symlinks'}D= -l $path
ABCDresult
0XXX0
10XX0
11110
110X1
11101
1343 0 return $path;
1344 }
1345 0 shift @dirs;
1346 }
1347 0 return;
1348 0 };
1349 }
1350  
1351 #pod =method lines, lines_raw, lines_utf8
1352 #pod
1353 #pod @contents = path("/tmp/foo.txt")->lines;
1354 #pod @contents = path("/tmp/foo.txt")->lines(\%options);
1355 #pod @contents = path("/tmp/foo.txt")->lines_raw;
1356 #pod @contents = path("/tmp/foo.txt")->lines_utf8;
1357 #pod
1358 #pod @contents = path("/tmp/foo.txt")->lines( { chomp => 1, count => 4 } );
1359 #pod
1360 #pod Returns a list of lines from a file. Optionally takes a hash-reference of
1361 #pod options. Valid options are C<binmode>, C<count> and C<chomp>.
1362 #pod
1363 #pod If C<binmode> is provided, it will be set on the handle prior to reading.
1364 #pod
1365 #pod If a positive C<count> is provided, that many lines will be returned from the
1366 #pod start of the file. If a negative C<count> is provided, the entire file will be
1367 #pod read, but only C<abs(count)> will be kept and returned. If C<abs(count)>
1368 #pod exceeds the number of lines in the file, all lines will be returned.
1369 #pod
1370 #pod If C<chomp> is set, any end-of-line character sequences (C<CR>, C<CRLF>, or
1371 #pod C<LF>) will be removed from the lines returned.
1372 #pod
1373 #pod Because the return is a list, C<lines> in scalar context will return the number
1374 #pod of lines (and throw away the data).
1375 #pod
1376 #pod $number_of_lines = path("/tmp/foo.txt")->lines;
1377 #pod
1378 #pod C<lines_raw> is like C<lines> with a C<binmode> of C<:raw>. We use C<:raw>
1379 #pod instead of C<:unix> so PerlIO buffering can manage reading by line.
1380 #pod
1381 #pod C<lines_utf8> is like C<lines> with a C<binmode> of C<:raw:encoding(UTF-8)>
1382 #pod (or C<:raw:utf8_strict> with L<PerlIO::utf8_strict>). If L<Unicode::UTF8>
1383 #pod 0.58+ is installed, a raw, unbuffered UTF-8 slurp will be done and then the
1384 #pod lines will be split. This is actually faster than relying on
1385 #pod IO layers, though a bit memory intensive. If memory use is a
1386 #pod concern, consider C<openr_utf8> and iterating directly on the handle.
1387 #pod
1388 #pod See also L</slurp> if you want to load a file as a whole chunk.
1389 #pod
1390 #pod Current API available since 0.065.
1391 #pod
1392 #pod =cut
1393  
1394 0 ▶sub lines {
Subroutine lines: not called CC=15 SCAR=54.8
Pod documented
1395 0 my $self = shift;
1396 0 my $args = _get_args( shift, qw/binmode chomp count/ );
1397 0 my $binmode = $args->{binmode};
1398 0 ▶ $binmode = ( ( caller(0) )[10] || {} )->{'open<'} unless defined $binmode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $binmode 0 0 0
Condition0%0/2
(caller 0)[10] || {}
l!l
00
MC/DC0%0/1
(caller 0)[10] || {}
(caller 0)[10]
Truth table0%0/2
A
A= (caller 0)[10]
Aresult
11
00
1399 0 my $fh = $self->filehandle( { locked => 1 }, "<", $binmode );
1400 0 my $chomp = $args->{chomp};
1401 # XXX more efficient to read @lines then chomp(@lines) vs map?
1402 0 ▶ if ( $args->{count} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args->{'count'}) { } 0 0 0
elsif ($chomp) { } 0 0 0
1403 0 my ( $counter, $mod, @result ) = ( 0, abs( $args->{count} ) );
1404 0 my $line;
1405 0 ▶ while ( !eof($fh) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eof $fh) 0 0 0
1406 0 ▶ defined( $line = readline($fh) ) or $self->_throw('readline');
Branch: 1 branch
BranchTrueFalseTotal
defined($line = readline $fh) or $self->_throw("readline") 0 0 0
1407  
1408 0 ▶ $line =~ s/(?:\x{0d}?\x{0a}|\x{0d})\z// if $chomp;
Branch: 1 branch
BranchTrueFalseTotal
if $chomp 0 0 0
1409 0 $result[ $counter++ ] = $line;
1410 # for positive count, terminate after right number of lines
1411 0 ▶ last if $counter == $args->{count};
Branch: 1 branch
BranchTrueFalseTotal
if $counter == $args->{"count"} 0 0 0
1412 # for negative count, eventually wrap around in the result array
1413 0 $counter %= $mod;
1414 }
1415 # reorder results if full and wrapped somewhere in the middle
1416 0 ▶ splice( @result, 0, 0, splice( @result, $counter ) )
Branch: 1 branch
BranchTrueFalseTotal
if @result == $mod and $counter % $mod 0 0 0
Condition0%0/3
@result == $mod and $counter % $mod
!ll&&!rl&&r
000
MC/DC0%0/2
@result == $mod and $counter % $mod
@result == $mod $counter % $mod
Truth table0%0/3
A and B
A= @result == $modB= $counter % $mod
ABresult
0X0
100
111
1417 if @result == $mod && $counter % $mod;
1418 0 return @result;
1419 }
1420 elsif ($chomp) {
1421 0 local $!;
1422 0 my @lines = map { s/(?:\x{0d}?\x{0a}|\x{0d})\z//; $_ } <$fh>; ## no critic
1423 0 ▶ $self->_throw('readline') if $!;
Branch: 1 branch
BranchTrueFalseTotal
if $! 0 0 0
1424 0 return @lines;
1425 }
1426 else {
1427 0 ▶ if ( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
1428 0 local $!;
1429 0 my @lines = <$fh>;
1430 0 ▶ $self->_throw('readline') if $!;
Branch: 1 branch
BranchTrueFalseTotal
if $! 0 0 0
1431 0 return @lines;
1432 } else {
1433 0 local $!;
1434 0 my $count =()= <$fh>;
1435 0 ▶ $self->_throw('readline') if $!;
Branch: 1 branch
BranchTrueFalseTotal
if $! 0 0 0
1436 0 return $count;
1437 }
1438 }
1439 }
1440  
1441 0 ▶sub lines_raw {
Subroutine lines_raw: not called CC=3 SCAR=24.8
Pod documented
1442 0 my $self = shift;
1443 0 my $args = _get_args( shift, qw/binmode chomp count/ );
1444 0 ▶ if ( $args->{chomp} && !$args->{count} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'chomp'} and not $args->{'count'}) { } 0 0 0
Condition0%0/3
$args->{"chomp"} and not $args->{"count"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args->{"chomp"} and not $args->{"count"}
$args->{"chomp"} not $args->{"count"}
Truth table0%0/3
A and not(B)
A= $args->{"chomp"}B= not $args->{"count"}
ABresult
0X0
100
111
1445 0 return split /\n/, slurp_raw($self); ## no critic
1446 }
1447 else {
1448 0 $args->{binmode} = ":raw";
1449 0 return lines( $self, $args );
1450 }
1451 }
1452  
1453 7 my $CRLF = qr/(?:\x{0d}?\x{0a}|\x{0d})/;
1454  
1455 0 ▶sub lines_utf8 {
Subroutine lines_utf8: not called CC=7 SCAR=40.3
Pod documented
1456 0 my $self = shift;
1457 0 my $args = _get_args( shift, qw/binmode chomp count/ );
1458 0 ▶ if ( ( defined($HAS_UU) ? $HAS_UU : ( $HAS_UU = _check_UU() ) )
Branch: 3 branches
BranchTrueFalseTotal
if (defined $HAS_UU ? $HAS_UU : ($HAS_UU = _check_UU()) and $args->{'chomp'} and not $args->{'count'}) { } 0 0 0
elsif (defined $HAS_PU ? $HAS_PU : ($HAS_PU = _check_PU())) { } 0 0 0
defined $HAS_UU ? : 0 0 0
Condition0%0/6
defined $HAS_UU ? $HAS_UU : ($HAS_UU = _check_UU()) and $args->{"chomp"} and not $args->{"count"}
!ll&&!rl&&r
000
defined $HAS_UU ? $HAS_UU : ($HAS_UU = _check_UU()) and $args->{"chomp"}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $HAS_UU ? $HAS_UU : ($HAS_UU = _check_UU()) and $args->{"chomp"} and not $args->{"count"}
defined $HAS_UU ? $HAS_UU : ($HAS_UU = _check_UU()) $args->{"chomp"} not $args->{"count"}
Truth table0%0/4
A and B and not(C)
A= defined $HAS_UU ? $HAS_UU : ($HAS_UU = _check_UU())B= $args->{"chomp"}C= not $args->{"count"}
ABCresult
0XX0
10X0
1100
1111
1459 && $args->{chomp}
1460 && !$args->{count} )
1461 {
1462 0 my $slurp = slurp_utf8($self);
1463 0 $slurp =~ s/$CRLF\z//; # like chomp, but full CR?LF|CR
1464 0 return split $CRLF, $slurp, -1; ## no critic
1465 }
1466 ▶ elsif ( defined($HAS_PU) ? $HAS_PU : ( $HAS_PU = _check_PU() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
defined $HAS_PU ? : 0 0 0
1467 0 $args->{binmode} = ":raw:utf8_strict";
1468 0 return lines( $self, $args );
1469 }
1470 else {
1471 0 $args->{binmode} = ":raw:encoding(UTF-8)";
1472 0 return lines( $self, $args );
1473 }
1474 }
1475  
1476 #pod =method mkdir
1477 #pod
1478 #pod path("foo/bar/baz")->mkdir;
1479 #pod path("foo/bar/baz")->mkdir( \%options );
1480 #pod
1481 #pod Like calling C<make_path> from L<File::Path>. An optional hash reference
1482 #pod is passed through to C<make_path>. Errors will be trapped and an exception
1483 #pod thrown. Returns the the path object to facilitate chaining.
1484 #pod
1485 #pod B<NOTE>: unlike Perl's builtin C<mkdir>, this will create intermediate paths
1486 #pod similar to the Unix C<mkdir -p> command. It will not error if applied to an
1487 #pod existing directory.
1488 #pod
1489 #pod Passing a defined argument I<other> than a hash reference is an error, and an
1490 #pod exception will be thrown.
1491 #pod
1492 #pod Current API available since 0.125.
1493 #pod
1494 #pod =cut
1495  
1496 0 ▶sub mkdir {
Subroutine mkdir: not called CC=9 SCAR=45.0
Pod documented
1497 0 my ( $self, $args, @rest ) = @_;
1498  
1499 0 ▶ $args = {} unless defined $args;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args 0 0 0
1500 0 ▶ if (@rest || (defined $args && ref $args ne 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rest or defined $args and ref $args ne "HASH") 0 0 0
Condition0%0/6
@rest or defined $args and ref $args ne "HASH"
l!l&&r!l&&!r
000
defined $args and ref $args ne "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
@rest or defined $args and ref $args ne "HASH"
@rest defined $args ref $args ne "HASH"
Truth table0%0/4
A or B and C
A= @restB= defined $argsC= ref $args ne "HASH"
ABCresult
1XX1
0111
00X0
0100
1501 0 $self->_throw('mkdir', undef, "method argument was given, but was not a hash reference");
1502 }
1503  
1504 0 my $err;
1505  
1506 0 ▶ $args->{error} = \$err unless defined $args->{error};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"error"} 0 0 0
1507 0 require File::Path;
1508 0 my @dirs;
1509 0 my $ok = eval {
1510 0 File::Path::make_path( $self->[PATH], $args );
1511 0 1;
1512 };
1513 0 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
1514 0 $self->_throw('mkdir', $self->[PATH], "error creating path: $@");
1515 }
1516 0 ▶ if ( $err && @$err ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err and @$err) 0 0 0
Condition0%0/3
$err and @$err
!ll&&!rl&&r
000
MC/DC0%0/2
$err and @$err
$err @$err
Truth table0%0/3
A and B
A= $errB= @$err
ABresult
0X0
100
111
1517 0 my ( $file, $message ) = %{ $err->[0] };
1518 0 $self->_throw('mkdir', $file, $message);
1519 }
1520 0 return $self;
1521 }
1522  
1523 #pod =method mkpath (deprecated)
1524 #pod
1525 #pod Like calling C<mkdir>, but returns the list of directories created or an empty list if
1526 #pod the directories already exist, just like C<make_path>.
1527 #pod
1528 #pod Passing a defined argument I<other> than a hash reference is an error, and an
1529 #pod exception will be thrown.
1530 #pod
1531 #pod Deprecated in 0.125.
1532 #pod
1533 #pod =cut
1534  
1535 44 ▶sub mkpath {
Subroutine mkpath: called CC=8 SCAR=26.7
Pod documented
1536 44 my ( $self, $args, @rest ) = @_;
1537  
1538 44 ▶ $args = {} unless defined $args;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args 44 0 44
1539 44 ▶ if (@rest || (defined $args && ref $args ne 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rest or defined $args and ref $args ne "HASH") 0 44 44
Condition33%2/6
@rest or defined $args and ref $args ne "HASH"
l!l&&r!l&&!r
0044
defined $args and ref $args ne "HASH"
!ll&&!rl&&r
0440
MC/DC0%0/3
@rest or defined $args and ref $args ne "HASH"
@rest defined $args ref $args ne "HASH"
Truth table25%1/4
A or B and C
A= @restB= defined $argsC= ref $args ne "HASH"
ABCresult
1XX1
0111
00X0
0100
1540 0 $self->_throw('mkdir', undef, "method argument was given, but was not a hash reference");
1541 }
1542  
1543 44 my $err;
1544 44 ▶ $args->{error} = \$err unless defined $args->{error};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"error"} 44 0 44
1545 44 require File::Path;
1546 44 my @dirs = File::Path::make_path( $self->[PATH], $args );
1547 44 ▶ if ( $err && @$err ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err and @$err) 0 44 44
Condition33%1/3
$err and @$err
!ll&&!rl&&r
0440
MC/DC0%0/2
$err and @$err
$err @$err
Truth table33%1/3
A and B
A= $errB= @$err
ABresult
0X0
100
111
1548 0 my ( $file, $message ) = %{ $err->[0] };
1549 0 Carp::croak("mkpath failed for $file: $message");
1550 }
1551 44 return @dirs;
1552 }
1553  
1554 #pod =method move
1555 #pod
1556 #pod path("foo.txt")->move("bar.txt");
1557 #pod
1558 #pod Moves the current path to the given destination using L<File::Copy>'s
1559 #pod C<move> function. Upon success, returns the C<Path::Tiny> object for the
1560 #pod newly moved file.
1561 #pod
1562 #pod If the destination already exists and is a directory, and the source is not a
1563 #pod directory, then the source file will be renamed into the directory
1564 #pod specified by the destination.
1565 #pod
1566 #pod If possible, move() will simply rename the file. Otherwise, it
1567 #pod copies the file to the new location and deletes the original. If an
1568 #pod error occurs during this copy-and-delete process, you may be left
1569 #pod with a (possibly partial) copy of the file under the destination
1570 #pod name.
1571 #pod
1572 #pod Current API available since 0.124. Prior versions used Perl's
1573 #pod -built-in (and less robust) L<rename|perlfunc/rename> function
1574 #pod and did not return an object.
1575 #pod
1576 #pod =cut
1577  
1578 0 ▶sub move {
Subroutine move: not called CC=3 SCAR=24.8
Pod documented
1579 0 my ( $self, $dest ) = @_;
1580 0 require File::Copy;
1581 0 ▶ File::Copy::move( $self->[PATH], $dest )
Branch: 1 branch
BranchTrueFalseTotal
File::Copy::move($self->[0], $dest) or $self->_throw("move", $self->[0] . "' -> '$dest") 0 0 0
1582 or $self->_throw( 'move', $self->[PATH] . "' -> '$dest" );
1583  
1584 0 ▶ return -d $dest ? _path( $dest, $self->basename ) : _path($dest);
Branch: 1 branch
BranchTrueFalseTotal
-d $dest ? : 0 0 0
1585 }
1586  
1587 #pod =method openr, openw, openrw, opena
1588 #pod
1589 #pod $fh = path("foo.txt")->openr($binmode); # read
1590 #pod $fh = path("foo.txt")->openr_raw;
1591 #pod $fh = path("foo.txt")->openr_utf8;
1592 #pod
1593 #pod $fh = path("foo.txt")->openw($binmode); # write
1594 #pod $fh = path("foo.txt")->openw_raw;
1595 #pod $fh = path("foo.txt")->openw_utf8;
1596 #pod
1597 #pod $fh = path("foo.txt")->opena($binmode); # append
1598 #pod $fh = path("foo.txt")->opena_raw;
1599 #pod $fh = path("foo.txt")->opena_utf8;
1600 #pod
1601 #pod $fh = path("foo.txt")->openrw($binmode); # read/write
1602 #pod $fh = path("foo.txt")->openrw_raw;
1603 #pod $fh = path("foo.txt")->openrw_utf8;
1604 #pod
1605 #pod Returns a file handle opened in the specified mode. The C<openr> style methods
1606 #pod take a single C<binmode> argument. All of the C<open*> methods have
1607 #pod C<open*_raw> and C<open*_utf8> equivalents that use buffered I/O layers C<:raw>
1608 #pod and C<:raw:encoding(UTF-8)> (or C<:raw:utf8_strict> with
1609 #pod L<PerlIO::utf8_strict>).
1610 #pod
1611 #pod An optional hash reference may be used to pass options. The only option is
1612 #pod C<locked>. If true, handles opened for writing, appending or read-write are
1613 #pod locked with C<LOCK_EX>; otherwise, they are locked for C<LOCK_SH>.
1614 #pod
1615 #pod $fh = path("foo.txt")->openrw_utf8( { locked => 1 } );
1616 #pod
1617 #pod See L</filehandle> for more on locking.
1618 #pod
1619 #pod Current API available since 0.011.
1620 #pod
1621 #pod =cut
1622  
1623 # map method names to corresponding open mode
1624 7 my %opens = (
1625 opena => ">>",
1626 openr => "<",
1627 openw => ">",
1628 openrw => "+<"
1629 );
1630  
1631 7 ▶while ( my ( $k, $v ) = each %opens ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %opens) 28 7 35
1632 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1633 # must check for lexical IO mode hint
1634 28 ▶ *{$k} = sub {
Subroutine __ANON__: not called CC=5 SCAR=32.4
1635 0 my ( $self, @args ) = @_;
1636 0 ▶ my $args = ( @args && ref $args[0] eq 'HASH' ) ? shift @args : {};
Branch: 1 branch
BranchTrueFalseTotal
@args && ref $args[0] eq 'HASH' ? : 0 0 0
Condition0%0/3
@args and ref $args[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@args and ref $args[0] eq "HASH"
@args ref $args[0] eq "HASH"
Truth table0%0/3
A and B
A= @argsB= ref $args[0] eq "HASH"
ABresult
0X0
100
111
1637 0 $args = _get_args( $args, qw/locked/ );
1638 0 my ($binmode) = @args;
1639 0 ▶ $binmode = ( ( caller(0) )[10] || {} )->{ 'open' . substr( $v, -1, 1 ) }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $binmode 0 0 0
Condition0%0/2
(caller 0)[10] || {}
l!l
00
MC/DC0%0/1
(caller 0)[10] || {}
(caller 0)[10]
Truth table0%0/2
A
A= (caller 0)[10]
Aresult
11
00
1640 unless defined $binmode;
1641 0 $self->filehandle( $args, $v, $binmode );
1642 28 };
1643 28 ▶ *{ $k . "_raw" } = sub {
Subroutine __ANON__: not called CC=3 SCAR=22.6
1644 0 my ( $self, @args ) = @_;
1645 0 ▶ my $args = ( @args && ref $args[0] eq 'HASH' ) ? shift @args : {};
Branch: 1 branch
BranchTrueFalseTotal
@args && ref $args[0] eq 'HASH' ? : 0 0 0
Condition0%0/3
@args and ref $args[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@args and ref $args[0] eq "HASH"
@args ref $args[0] eq "HASH"
Truth table0%0/3
A and B
A= @argsB= ref $args[0] eq "HASH"
ABresult
0X0
100
111
1646 0 $args = _get_args( $args, qw/locked/ );
1647 0 $self->filehandle( $args, $v, ":raw" );
1648 28 };
1649 28 ▶ *{ $k . "_utf8" } = sub {
Subroutine __ANON__: not called CC=5 SCAR=32.6
1650 0 my ( $self, @args ) = @_;
1651 0 ▶ my $args = ( @args && ref $args[0] eq 'HASH' ) ? shift @args : {};
Branch: 1 branch
BranchTrueFalseTotal
@args && ref $args[0] eq 'HASH' ? : 0 0 0
Condition0%0/3
@args and ref $args[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@args and ref $args[0] eq "HASH"
@args ref $args[0] eq "HASH"
Truth table0%0/3
A and B
A= @argsB= ref $args[0] eq "HASH"
ABresult
0X0
100
111
1652 0 $args = _get_args( $args, qw/locked/ );
1653 0 my $layer;
1654 0 ▶ if ( defined($HAS_PU) ? $HAS_PU : ( $HAS_PU = _check_PU() ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $HAS_PU ? $HAS_PU : ($HAS_PU = _check_PU())) { } 0 0 0
defined $HAS_PU ? : 0 0 0
1655 0 $layer = ":raw:utf8_strict";
1656 }
1657 else {
1658 0 $layer = ":raw:encoding(UTF-8)";
1659 }
1660 0 $self->filehandle( $args, $v, $layer );
1661 28 };
1662 }
1663  
1664 #pod =method parent
1665 #pod
1666 #pod $parent = path("foo/bar/baz")->parent; # foo/bar
1667 #pod $parent = path("foo/wibble.txt")->parent; # foo
1668 #pod
1669 #pod $parent = path("foo/bar/baz")->parent(2); # foo
1670 #pod
1671 #pod Returns a C<Path::Tiny> object corresponding to the parent directory of the
1672 #pod original directory or file. An optional positive integer argument is the number
1673 #pod of parent directories upwards to return. C<parent> by itself is equivalent to
1674 #pod C<parent(1)>.
1675 #pod
1676 #pod Current API available since 0.014.
1677 #pod
1678 #pod =cut
1679  
1680 # XXX this is ugly and coverage is incomplete. I think it's there for windows
1681 # so need to check coverage there and compare
1682 13 ▶sub parent {
Subroutine parent: called CC=10 SCAR=33.1
Pod documented
1683 13 my ( $self, $level ) = @_;
1684 13 ▶ $level = 1 unless defined $level && $level > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level and $level > 0 13 0 13
Condition33%1/3
defined $level and $level > 0
!ll&&!rl&&r
1300
MC/DC0%0/2
defined $level and $level > 0
defined $level $level > 0
Truth table33%1/3
A and B
A= defined $levelB= $level > 0
ABresult
0X0
100
111
1685 13 ▶ $self->_splitpath unless defined $self->[FILE];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->[4] 13 0 13
1686 13 my $parent;
1687 13 ▶ if ( length $self->[FILE] ) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $self->[4]) { } 13 0 13
elsif (length $self->[3]) { } 0 0 0
1688 13 ▶ if ( $self->[FILE] eq '.' || $self->[FILE] eq ".." ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[4] eq '.' or $self->[4] eq '..') { } 0 13 13
Condition33%1/3
$self->[4] eq "." or $self->[4] eq ".."
l!l&&r!l&&!r
0013
MC/DC0%0/2
$self->[4] eq "." or $self->[4] eq ".."
$self->[4] eq "." $self->[4] eq ".."
Truth table33%1/3
A or B
A= $self->[4] eq "."B= $self->[4] eq ".."
ABresult
1X1
011
000
1689 0 $parent = _path( $self->[PATH] . "/.." );
1690 }
1691 else {
1692 13 $parent = _path( _non_empty( $self->[VOL] . $self->[DIR] ) );
1693 }
1694 }
1695 elsif ( length $self->[DIR] ) {
1696 # because of symlinks, any internal updir requires us to
1697 # just add more updirs at the end
1698 0 ▶ if ( $self->[DIR] =~ m{(?:^\.\./|/\.\./|/\.\.\z)} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[3] =~ m[(?:^\.\./|/\.\./|/\.\.\z)]) { } 0 0 0
1699 0 $parent = _path( $self->[VOL] . $self->[DIR] . "/.." );
1700 }
1701 else {
1702 0 ( my $dir = $self->[DIR] ) =~ s{/[^\/]+/\z}{/};
1703 0 $parent = _path( $self->[VOL] . $dir );
1704 }
1705 }
1706 else {
1707 0 $parent = _path( _non_empty( $self->[VOL] ) );
1708 }
1709 13 ▶ return $level == 1 ? $parent : $parent->parent( $level - 1 );
Branch: 1 branch
BranchTrueFalseTotal
$level == 1 ? : 13 0 13
1710 }
1711  
1712 13 ▶sub _non_empty {
Subroutine _non_empty: called CC=3 SCAR=13.1
1713 13 my ($string) = shift;
1714 13 ▶ return ( ( defined($string) && length($string) ) ? $string : "." );
Branch: 1 branch
BranchTrueFalseTotal
defined $string && length $string ? : 13 0 13
Condition33%1/3
defined $string and length $string
!ll&&!rl&&r
0013
MC/DC0%0/2
defined $string and length $string
defined $string length $string
Truth table33%1/3
A and B
A= defined $stringB= length $string
ABresult
0X0
100
111
1715 }
1716  
1717 #pod =method realpath
1718 #pod
1719 #pod $real = path("/baz/foo/../bar")->realpath;
1720 #pod $real = path("foo/../bar")->realpath;
1721 #pod
1722 #pod Returns a new C<Path::Tiny> object with all symbolic links and upward directory
1723 #pod parts resolved using L<Cwd>'s C<realpath>. Compared to C<absolute>, this is
1724 #pod more expensive as it must actually consult the filesystem.
1725 #pod
1726 #pod If the parent path can't be resolved (e.g. if it includes directories that
1727 #pod don't exist), an exception will be thrown:
1728 #pod
1729 #pod $real = path("doesnt_exist/foo")->realpath; # dies
1730 #pod
1731 #pod However, if the parent path exists and only the last component (e.g. filename)
1732 #pod doesn't exist, the realpath will be the realpath of the parent plus the
1733 #pod non-existent last component:
1734 #pod
1735 #pod $real = path("./aasdlfasdlf")->realpath; # works
1736 #pod
1737 #pod The underlying L<Cwd> module usually worked this way on Unix, but died on
1738 #pod Windows (and some Unixes) if the full path didn't exist. As of version 0.064,
1739 #pod it's safe to use anywhere.
1740 #pod
1741 #pod Current API available since 0.001.
1742 #pod
1743 #pod =cut
1744  
1745 # Win32 and some Unixes need parent path resolved separately so realpath
1746 # doesn't throw an error resolving non-existent basename
1747 0 ▶sub realpath {
Subroutine realpath: not called CC=9 SCAR=45.0
Pod documented
1748 0 my $self = shift;
1749 0 $self = $self->_resolve_symlinks;
1750 0 require Cwd;
1751 0 ▶ $self->_splitpath if !defined $self->[FILE];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->[4] 0 0 0
1752 0 ▶ my $check_parent =
Condition0%0/6
length $self->[4] && $self->[4] ne '.' && $self->[4] ne '..'
!ll&&!rl&&r
000
length $self->[4] and $self->[4] ne "."
!ll&&!rl&&r
000
MC/DC0%0/3
length $self->[4] && $self->[4] ne '.' && $self->[4] ne '..'
length $self->[4] $self->[4] ne "." $self->[4] ne '..'
Truth table0%0/4
A and B && C
A= length $self->[4]B= $self->[4] ne "."C= $self->[4] ne '..'
ABCresult
0XX0
10X0
1100
1111
1753 length $self->[FILE] && $self->[FILE] ne '.' && $self->[FILE] ne '..';
1754 0 my $realpath = eval {
1755 # pure-perl Cwd can carp
1756 0 ▶ local $SIG{__WARN__} = sub { };
Subroutine __ANON__: not called CC=1 SCAR=6.9
1757 0 ▶ Cwd::realpath( $check_parent ? $self->parent->[PATH] : $self->[PATH] );
Branch: 1 branch
BranchTrueFalseTotal
$check_parent ? : 0 0 0
1758 };
1759 # parent realpath must exist; not all Cwd::realpath will error if it doesn't
1760 0 ▶ $self->_throw("resolving realpath")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $realpath and length $realpath and -e $realpath 0 0 0
Condition0%0/6
defined $realpath and length $realpath and -e $realpath
!ll&&!rl&&r
000
defined $realpath and length $realpath
!ll&&!rl&&r
000
MC/DC0%0/3
defined $realpath and length $realpath and -e $realpath
defined $realpath length $realpath -e $realpath
Truth table0%0/4
A and B and C
A= defined $realpathB= length $realpathC= -e $realpath
ABCresult
0XX0
10X0
1100
1111
1761 unless defined $realpath && length $realpath && -e $realpath;
1762 0 ▶ return ( $check_parent ? _path( $realpath, $self->[FILE] ) : _path($realpath) );
Branch: 1 branch
BranchTrueFalseTotal
$check_parent ? : 0 0 0
1763 }
1764  
1765 #pod =method relative
1766 #pod
1767 #pod $rel = path("/tmp/foo/bar")->relative("/tmp"); # foo/bar
1768 #pod
1769 #pod Returns a C<Path::Tiny> object with a path relative to a new base path
1770 #pod given as an argument. If no argument is given, the current directory will
1771 #pod be used as the new base path.
1772 #pod
1773 #pod If either path is already relative, it will be made absolute based on the
1774 #pod current directly before determining the new relative path.
1775 #pod
1776 #pod The algorithm is roughly as follows:
1777 #pod
1778 #pod =for :list
1779 #pod * If the original and new base path are on different volumes, an exception
1780 #pod will be thrown.
1781 #pod * If the original and new base are identical, the relative path is C<".">.
1782 #pod * If the new base subsumes the original, the relative path is the original
1783 #pod path with the new base chopped off the front
1784 #pod * If the new base does not subsume the original, a common prefix path is
1785 #pod determined (possibly the root directory) and the relative path will
1786 #pod consist of updirs (C<"..">) to reach the common prefix, followed by the
1787 #pod original path less the common prefix.
1788 #pod
1789 #pod Unlike C<File::Spec::abs2rel>, in the last case above, the calculation based
1790 #pod on a common prefix takes into account symlinks that could affect the updir
1791 #pod process. Given an original path "/A/B" and a new base "/A/C",
1792 #pod (where "A", "B" and "C" could each have multiple path components):
1793 #pod
1794 #pod =for :list
1795 #pod * Symlinks in "A" don't change the result unless the last component of A is
1796 #pod a symlink and the first component of "C" is an updir.
1797 #pod * Symlinks in "B" don't change the result and will exist in the result as
1798 #pod given.
1799 #pod * Symlinks and updirs in "C" must be resolved to actual paths, taking into
1800 #pod account the possibility that not all path components might exist on the
1801 #pod filesystem.
1802 #pod
1803 #pod Current API available since 0.001. New algorithm (that accounts for
1804 #pod symlinks) available since 0.079.
1805 #pod
1806 #pod =cut
1807  
1808 0 ▶sub relative {
Subroutine relative: not called CC=18 SCAR=58.3
Pod documented
1809 0 my ( $self, $base ) = @_;
1810 0 ▶ $base = _path( defined $base && length $base ? $base : '.' );
Branch: 1 branch
BranchTrueFalseTotal
defined $base && length $base ? : 0 0 0
Condition0%0/3
defined $base and length $base
!ll&&!rl&&r
000
MC/DC0%0/2
defined $base and length $base
defined $base length $base
Truth table0%0/3
A and B
A= defined $baseB= length $base
ABresult
0X0
100
111
1811  
1812 # relative paths must be converted to absolute first
1813 0 ▶ $self = $self->absolute if $self->is_relative;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_relative 0 0 0
1814 0 ▶ $base = $base->absolute if $base->is_relative;
Branch: 1 branch
BranchTrueFalseTotal
if $base->is_relative 0 0 0
1815  
1816 # normalize volumes if they exist
1817 0 ▶ $self = $self->absolute if !length $self->volume && length $base->volume;
Branch: 1 branch
BranchTrueFalseTotal
if not length $self->volume and length $base->volume 0 0 0
Condition0%0/3
not length $self->volume and length $base->volume
!ll&&!rl&&r
000
MC/DC0%0/2
not length $self->volume and length $base->volume
not length $self->volume length $base->volume
Truth table0%0/3
not(A) and B
A= not length $self->volumeB= length $base->volume
ABresult
0X0
100
111
1818 0 ▶ $base = $base->absolute if length $self->volume && !length $base->volume;
Branch: 1 branch
BranchTrueFalseTotal
if length $self->volume and not length $base->volume 0 0 0
Condition0%0/3
length $self->volume and not length $base->volume
!ll&&!rl&&r
000
MC/DC0%0/2
length $self->volume and not length $base->volume
length $self->volume not length $base->volume
Truth table0%0/3
A and not(B)
A= length $self->volumeB= not length $base->volume
ABresult
0X0
100
111
1819  
1820 # can't make paths relative across volumes
1821 0 ▶ if ( !_same( $self->volume, $base->volume ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_same($self->volume, $base->volume)) 0 0 0
1822 0 Carp::croak("relative() can't cross volumes: '$self' vs '$base'");
1823 }
1824  
1825 # if same absolute path, relative is current directory
1826 0 ▶ return _path(".") if _same( $self->[PATH], $base->[PATH] );
Branch: 1 branch
BranchTrueFalseTotal
if _same($self->[0], $base->[0]) 0 0 0
1827  
1828 # if base is a prefix of self, chop prefix off self
1829 0 ▶ if ( $base->subsumes($self) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base->subsumes($self)) 0 0 0
1830 0 ▶ $base = "" if $base->is_rootdir;
Branch: 1 branch
BranchTrueFalseTotal
if $base->is_rootdir 0 0 0
1831 0 my $relative = "$self";
1832 0 $relative =~ s{\A\Q$base/}{};
1833 0 return _path(".", $relative);
1834 }
1835  
1836 # base is not a prefix, so must find a common prefix (even if root)
1837 0 my ( @common, @self_parts, @base_parts );
1838 0 @base_parts = split /\//, $base->_just_filepath;
1839  
1840 # if self is rootdir, then common directory is root (shown as empty
1841 # string for later joins); otherwise, must be computed from path parts.
1842 0 ▶ if ( $self->is_rootdir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_rootdir) { } 0 0 0
1843 0 @common = ("");
1844 0 shift @base_parts;
1845 }
1846 else {
1847 0 @self_parts = split /\//, $self->_just_filepath;
1848  
1849 0 ▶ while ( @self_parts && @base_parts && _same( $self_parts[0], $base_parts[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@self_parts and @base_parts and _same($self_parts[0], $base_parts[0])) 0 0 0
Condition0%0/6
@self_parts and @base_parts and _same($self_parts[0], $base_parts[0])
!ll&&!rl&&r
000
@self_parts and @base_parts
!ll&&!rl&&r
000
MC/DC0%0/3
@self_parts and @base_parts and _same($self_parts[0], $base_parts[0])
@self_parts @base_parts _same($self_parts[0], $base_parts[0])
Truth table0%0/4
A and B and C
A= @self_partsB= @base_partsC= _same($self_parts[0], $base_parts[0])
ABCresult
0XX0
10X0
1100
1111
1850 0 push @common, shift @base_parts;
1851 0 shift @self_parts;
1852 }
1853 }
1854  
1855 # if there are any symlinks from common to base, we have a problem, as
1856 # you can't guarantee that updir from base reaches the common prefix;
1857 # we must resolve symlinks and try again; likewise, any updirs are
1858 # a problem as it throws off calculation of updirs needed to get from
1859 # self's path to the common prefix.
1860 0 ▶ if ( my $new_base = $self->_resolve_between( \@common, \@base_parts ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $new_base = $self->_resolve_between(\@common, \@base_parts)) 0 0 0
1861 0 return $self->relative($new_base);
1862 }
1863  
1864 # otherwise, symlinks in common or from common to A don't matter as
1865 # those don't involve updirs
1866 0 my @new_path = ( ("..") x ( 0+ @base_parts ), @self_parts );
1867 0 return _path(@new_path);
1868 }
1869  
1870 0 ▶sub _just_filepath {
Subroutine _just_filepath: not called CC=2 SCAR=17.9
1871 0 my $self = shift;
1872 0 my $self_vol = $self->volume;
1873 0 ▶ return "$self" if !length $self_vol;
Branch: 1 branch
BranchTrueFalseTotal
unless length $self_vol 0 0 0
1874  
1875 0 ( my $self_path = "$self" ) =~ s{\A\Q$self_vol}{};
1876  
1877 0 return $self_path;
1878 }
1879  
1880 0 ▶sub _resolve_between {
Subroutine _resolve_between: not called CC=6 SCAR=37.4
1881 0 my ( $self, $common, $base ) = @_;
1882 0 my $path = $self->volume . join( "/", @$common );
1883 0 my $changed = 0;
1884 0 for my $p (@$base) {
1885 0 $path .= "/$p";
1886 0 ▶ if ( $p eq '..' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p eq "..") 0 0 0
1887 0 $changed = 1;
1888 0 ▶ if ( -e $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $path) { } 0 0 0
1889 0 $path = _path($path)->realpath->[PATH];
1890 }
1891 else {
1892 0 $path =~ s{/[^/]+/..\z}{/};
1893 }
1894 }
1895 0 ▶ if ( -l $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-l $path) 0 0 0
1896 0 $changed = 1;
1897 0 $path = _path($path)->realpath->[PATH];
1898 }
1899 }
1900 0 ▶ return $changed ? _path($path) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$changed ? : 0 0 0
1901 }
1902  
1903 #pod =method remove
1904 #pod
1905 #pod path("foo.txt")->remove;
1906 #pod
1907 #pod This is just like C<unlink>, except for its error handling: if the path does
1908 #pod not exist, it returns false; if deleting the file fails, it throws an
1909 #pod exception.
1910 #pod
1911 #pod Current API available since 0.012.
1912 #pod
1913 #pod =cut
1914  
1915 0 ▶sub remove {
Subroutine remove: not called CC=4 SCAR=30.0
Pod documented
1916 0 my $self = shift;
1917  
1918 0 ▶ return 0 if !-e $self->[PATH] && !-l $self->[PATH];
Branch: 1 branch
BranchTrueFalseTotal
unless -e $self->[0] or -l $self->[0] 0 0 0
Condition0%0/3
-e $self->[0] or -l $self->[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
-e $self->[0] or -l $self->[0]
-e $self->[0] -l $self->[0]
Truth table0%0/3
A or B
A= -e $self->[0]B= -l $self->[0]
ABresult
1X1
011
000
1919  
1920 0 ▶ return unlink( $self->[PATH] ) || $self->_throw('unlink');
Condition0%0/3
unlink($self->[0]) || $self->_throw('unlink')
l!l&&r!l&&!r
000
MC/DC0%0/2
unlink($self->[0]) || $self->_throw('unlink')
unlink($self->[0]) $self->_throw('unlink')
Truth table0%0/3
A || B
A= unlink($self->[0])B= $self->_throw('unlink')
ABresult
1X1
011
000
1921 }
1922  
1923 #pod =method remove_tree
1924 #pod
1925 #pod # directory
1926 #pod path("foo/bar/baz")->remove_tree;
1927 #pod path("foo/bar/baz")->remove_tree( \%options );
1928 #pod path("foo/bar/baz")->remove_tree( { safe => 0 } ); # force remove
1929 #pod
1930 #pod Like calling C<remove_tree> from L<File::Path>, but defaults to C<safe> mode.
1931 #pod An optional hash reference is passed through to C<remove_tree>. Errors will be
1932 #pod trapped and an exception thrown. Returns the number of directories deleted,
1933 #pod just like C<remove_tree>.
1934 #pod
1935 #pod If you want to remove a directory only if it is empty, use the built-in
1936 #pod C<rmdir> function instead.
1937 #pod
1938 #pod rmdir path("foo/bar/baz/");
1939 #pod
1940 #pod Current API available since 0.013.
1941 #pod
1942 #pod Passing a defined argument I<other> than a hash reference is an error, and an
1943 #pod exception will be thrown.
1944 #pod
1945 #pod =cut
1946  
1947 3 ▶sub remove_tree {
Subroutine remove_tree: called CC=11 SCAR=29.6
Pod documented
1948 3 my ( $self, $args, @rest ) = @_;
1949  
1950 3 ▶ $args = {} unless defined $args;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args 0 3 3
1951 3 ▶ if (@rest || (defined $args && ref $args ne 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rest or defined $args and ref $args ne "HASH") 0 3 3
Condition33%2/6
@rest or defined $args and ref $args ne "HASH"
l!l&&r!l&&!r
003
defined $args and ref $args ne "HASH"
!ll&&!rl&&r
030
MC/DC0%0/3
@rest or defined $args and ref $args ne "HASH"
@rest defined $args ref $args ne "HASH"
Truth table25%1/4
A or B and C
A= @restB= defined $argsC= ref $args ne "HASH"
ABCresult
1XX1
0111
00X0
0100
1952 0 $self->_throw('mkdir', undef, "method argument was given, but was not a hash reference");
1953 }
1954  
1955 3 ▶ return 0 if !-e $self->[PATH] && !-l $self->[PATH];
Branch: 1 branch
BranchTrueFalseTotal
unless -e $self->[0] or -l $self->[0] 0 3 3
Condition33%1/3
-e $self->[0] or -l $self->[0]
l!l&&r!l&&!r
300
MC/DC0%0/2
-e $self->[0] or -l $self->[0]
-e $self->[0] -l $self->[0]
Truth table33%1/3
A or B
A= -e $self->[0]B= -l $self->[0]
ABresult
1X1
011
000
1956  
1957 3 my $err;
1958 3 ▶ $args->{error} = \$err unless defined $args->{error};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"error"} 3 0 3
1959 3 ▶ $args->{safe} = 1 unless defined $args->{safe};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"safe"} 0 3 3
1960 3 require File::Path;
1961 3 my $count = File::Path::remove_tree( $self->[PATH], $args );
1962  
1963 3 ▶ if ( $err && @$err ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err and @$err) 3 0 3
Condition33%1/3
$err and @$err
!ll&&!rl&&r
003
MC/DC0%0/2
$err and @$err
$err @$err
Truth table33%1/3
A and B
A= $errB= @$err
ABresult
0X0
100
111
1964 3 my ( $file, $message ) = %{ $err->[0] };
1965 3 Carp::croak("remove_tree failed for $file: $message");
1966 }
1967 0 return $count;
1968 }
1969  
1970 #pod =method sibling
1971 #pod
1972 #pod $foo = path("/tmp/foo.txt");
1973 #pod $sib = $foo->sibling("bar.txt"); # /tmp/bar.txt
1974 #pod $sib = $foo->sibling("baz", "bam.txt"); # /tmp/baz/bam.txt
1975 #pod
1976 #pod Returns a new C<Path::Tiny> object relative to the parent of the original.
1977 #pod This is slightly more efficient than C<< $path->parent->child(...) >>.
1978 #pod
1979 #pod Current API available since 0.058.
1980 #pod
1981 #pod =cut
1982  
1983 0 ▶sub sibling {
Subroutine sibling: not called CC=1 SCAR=6.9
Pod documented
1984 0 my $self = shift;
1985 0 return _path( $self->parent->[PATH], @_ );
1986 }
1987  
1988 #pod =method size, size_human
1989 #pod
1990 #pod my $p = path("foo"); # with size 1025 bytes
1991 #pod
1992 #pod $p->size; # "1025"
1993 #pod $p->size_human; # "1.1 K"
1994 #pod $p->size_human( {format => "iec"} ); # "1.1 KiB"
1995 #pod
1996 #pod Returns the size of a file. The C<size> method is just a wrapper around C<-s>.
1997 #pod
1998 #pod The C<size_human> method provides a human-readable string similar to
1999 #pod C<ls -lh>. Like C<ls>, it rounds upwards and provides one decimal place for
2000 #pod single-digit sizes and no decimal places for larger sizes. The only available
2001 #pod option is C<format>, which has three valid values:
2002 #pod
2003 #pod =for :list
2004 #pod * 'ls' (the default): base-2 sizes, with C<ls> style single-letter suffixes (K, M, etc.)
2005 #pod * 'iec': base-2 sizes, with IEC binary suffixes (KiB, MiB, etc.)
2006 #pod * 'si': base-10 sizes, with SI decimal suffixes (kB, MB, etc.)
2007 #pod
2008 #pod If C<-s> would return C<undef>, C<size_human> returns the empty string.
2009 #pod
2010 #pod Current API available since 0.122.
2011 #pod
2012 #pod =cut
2013  
2014 0 ▶sub size { -s $_[0]->[PATH] }
Subroutine size: not called CC=1 SCAR=6.9
Pod documented
2015  
2016 my %formats = (
2017 28 'ls' => [ 1024, log(1024), [ "", map { " $_" } qw/K M G T/ ] ],
2018 28 'iec' => [ 1024, log(1024), [ "", map { " $_" } qw/KiB MiB GiB TiB/ ] ],
2019 7 'si' => [ 1000, log(1000), [ "", map { " $_" } qw/kB MB GB TB/ ] ],
2020 );
2021  
2022 0 ▶sub _formats { return $formats{$_[0]} }
Subroutine _formats: not called CC=1 SCAR=6.9
2023  
2024 0 ▶sub size_human {
Subroutine size_human: not called CC=4 SCAR=30.0
Pod documented
2025 0 my $self = shift;
2026 0 my $args = _get_args( shift, qw/format/ );
2027 0 ▶ my $format = defined $args->{format} ? $args->{format} : "ls";
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'format'} ? : 0 0 0
2028 0 ▶ my $fmt_opts = $formats{$format}
Branch: 1 branch
BranchTrueFalseTotal
my $fmt_opts = $formats{$format} or Carp::croak("Invalid format '${format}' for size_human()") 0 0 0
2029 or Carp::croak("Invalid format '$format' for size_human()");
2030 0 my $size = -s $self->[PATH];
2031 0 ▶ return defined $size ? _human_size( $size, @$fmt_opts ) : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $size ? : 0 0 0
2032 }
2033  
2034 0 ▶sub _ceil {
Subroutine _ceil: not called CC=2 SCAR=17.9
2035 0 ▶ return $_[0] == int($_[0]) ? $_[0] : int($_[0]+1);
Branch: 1 branch
BranchTrueFalseTotal
$_[0] == int $_[0] ? : 0 0 0
2036 }
2037  
2038 0 ▶sub _human_size {
Subroutine _human_size: not called CC=7 SCAR=40.3
2039 0 my ( $size, $base, $log_base, $suffixes ) = @_;
2040 0 ▶ return "0" if $size == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $size == 0 0 0 0
2041  
2042 0 my $mag = int( log($size) / $log_base );
2043 0 $size /= $base**$mag;
2044 0 ▶ $size =
Branch: 2 branches
BranchTrueFalseTotal
$mag == 0 ? : 0 0 0
length int $size == 1 ? : 0 0 0
2045 $mag == 0 ? $size
2046 : length( int($size) ) == 1 ? _ceil( $size * 10 ) / 10
2047 : _ceil($size);
2048 0 ▶ if ( $size >= $base ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size >= $base) 0 0 0
2049 0 $size /= $base;
2050 0 $mag++;
2051 }
2052  
2053 0 ▶ my $fmt = ( $mag == 0 || length( int($size) ) > 1 ) ? "%.0f%s" : "%.1f%s";
Branch: 1 branch
BranchTrueFalseTotal
$mag == 0 || length int $size > 1 ? : 0 0 0
Condition0%0/3
$mag == 0 or length int $size > 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$mag == 0 or length int $size > 1
$mag == 0 length int $size > 1
Truth table0%0/3
A or B
A= $mag == 0B= length int $size > 1
ABresult
1X1
011
000
2054 0 return sprintf( $fmt, $size, $suffixes->[$mag] );
2055 }
2056  
2057 #pod =method slurp, slurp_raw, slurp_utf8
2058 #pod
2059 #pod $data = path("foo.txt")->slurp;
2060 #pod $data = path("foo.txt")->slurp( {binmode => ":raw"} );
2061 #pod $data = path("foo.txt")->slurp_raw;
2062 #pod $data = path("foo.txt")->slurp_utf8;
2063 #pod
2064 #pod Reads file contents into a scalar. Takes an optional hash reference which may
2065 #pod be used to pass options. The only available option is C<binmode>, which is
2066 #pod passed to C<binmode()> on the handle used for reading.
2067 #pod
2068 #pod C<slurp_raw> is like C<slurp> with a C<binmode> of C<:unix> for
2069 #pod a fast, unbuffered, raw read.
2070 #pod
2071 #pod C<slurp_utf8> is like C<slurp> with a C<binmode> of
2072 #pod C<:unix:encoding(UTF-8)> (or C<:unix:utf8_strict> with
2073 #pod L<PerlIO::utf8_strict>). If L<Unicode::UTF8> 0.58+ is installed, a
2074 #pod unbuffered, raw slurp will be done instead and the result decoded with
2075 #pod C<Unicode::UTF8>. This is just as strict and is roughly an order of
2076 #pod magnitude faster than using C<:encoding(UTF-8)>.
2077 #pod
2078 #pod B<Note>: C<slurp> and friends lock the filehandle before slurping. If
2079 #pod you plan to slurp from a file created with L<File::Temp>, be sure to
2080 #pod close other handles or open without locking to avoid a deadlock:
2081 #pod
2082 #pod my $tempfile = File::Temp->new(EXLOCK => 0);
2083 #pod my $guts = path($tempfile)->slurp;
2084 #pod
2085 #pod See also L</lines> if you want to slurp a file into a line array.
2086 #pod
2087 #pod Current API available since 0.004.
2088 #pod
2089 #pod =cut
2090  
2091 71 ▶sub slurp {
Subroutine slurp: called CC=8 SCAR=27.2
Pod documented
2092 71 my $self = shift;
2093 71 my $args = _get_args( shift, qw/binmode/ );
2094 71 my $binmode = $args->{binmode};
2095 71 ▶ $binmode = ( ( caller(0) )[10] || {} )->{'open<'} unless defined $binmode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $binmode 0 71 71
Condition0%0/2
(caller 0)[10] || {}
l!l
00
MC/DC0%0/1
(caller 0)[10] || {}
(caller 0)[10]
Truth table0%0/2
A
A= (caller 0)[10]
Aresult
11
00
2096 71 my $fh = $self->filehandle( { locked => 1 }, "<", $binmode );
2097 16 ▶ if ( ( defined($binmode) ? $binmode : "" ) eq ":unix"
Branch: 2 branches
BranchTrueFalseTotal
if ((defined $binmode ? $binmode : '') eq ':unix' and my $size = -s $fh) { } 16 0 16
defined $binmode ? : 16 0 16
Condition33%1/3
(defined $binmode ? $binmode : "") eq ":unix" and my $size = -s $fh
!ll&&!rl&&r
0016
MC/DC0%0/2
(defined $binmode ? $binmode : "") eq ":unix" and my $size = -s $fh
(defined $binmode ? $binmode : "") eq ":unix" my $size = -s $fh
Truth table33%1/3
A and B
A= (defined $binmode ? $binmode : "") eq ":unix"B= my $size = -s $fh
ABresult
0X0
100
111
2098 and my $size = -s $fh )
2099 {
2100 16 my $buf;
2101 16 my $rc = read $fh, $buf, $size; # File::Slurp in a nutshell
2102 16 ▶ $self->_throw('read') unless defined $rc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rc 0 16 16
2103 16 return $buf;
2104 }
2105 else {
2106 0 local $/;
2107 0 my $buf = scalar <$fh>;
2108 0 ▶ $self->_throw('read') unless defined $buf;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $buf 0 0 0
2109 0 return $buf;
2110 }
2111 }
2112  
2113 71 ▶sub slurp_raw { $_[1] = { binmode => ":unix" }; goto &slurp }
Subroutine slurp_raw: called CC=1 SCAR=0.0
Pod documented
2114  
2115 0 ▶sub slurp_utf8 {
Subroutine slurp_utf8: not called CC=5 SCAR=34.0
Pod documented
2116 0 ▶ if ( defined($HAS_UU) ? $HAS_UU : ( $HAS_UU = _check_UU() ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $HAS_UU ? $HAS_UU : ($HAS_UU = _check_UU())) { } 0 0 0
elsif (defined $HAS_PU ? $HAS_PU : ($HAS_PU = _check_PU())) { } 0 0 0
defined $HAS_UU ? : 0 0 0
2117 0 return Unicode::UTF8::decode_utf8( slurp( $_[0], { binmode => ":unix" } ) );
2118 }
2119 ▶ elsif ( defined($HAS_PU) ? $HAS_PU : ( $HAS_PU = _check_PU() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
defined $HAS_PU ? : 0 0 0
2120 0 $_[1] = { binmode => ":unix:utf8_strict" };
2121 0 goto &slurp;
2122 }
2123 else {
2124 0 $_[1] = { binmode => ":unix:encoding(UTF-8)" };
2125 0 goto &slurp;
2126 }
2127 }
2128  
2129 #pod =method spew, spew_raw, spew_utf8
2130 #pod
2131 #pod path("foo.txt")->spew(@data);
2132 #pod path("foo.txt")->spew(\@data);
2133 #pod path("foo.txt")->spew({binmode => ":raw"}, @data);
2134 #pod path("foo.txt")->spew_raw(@data);
2135 #pod path("foo.txt")->spew_utf8(@data);
2136 #pod
2137 #pod Writes data to a file atomically. The file is written to a temporary file in
2138 #pod the same directory, then renamed over the original. An optional hash reference
2139 #pod may be used to pass options. The only option is C<binmode>, which is passed to
2140 #pod C<binmode()> on the handle used for writing.
2141 #pod
2142 #pod C<spew_raw> is like C<spew> with a C<binmode> of C<:unix> for a fast,
2143 #pod unbuffered, raw write.
2144 #pod
2145 #pod C<spew_utf8> is like C<spew> with a C<binmode> of C<:unix:encoding(UTF-8)>
2146 #pod (or C<:unix:utf8_strict> with L<PerlIO::utf8_strict>). If L<Unicode::UTF8>
2147 #pod 0.58+ is installed, a raw, unbuffered spew will be done instead on the data
2148 #pod encoded with C<Unicode::UTF8>.
2149 #pod
2150 #pod B<NOTE>: because the file is written to a temporary file and then renamed, the
2151 #pod new file will wind up with permissions based on your current umask. This is a
2152 #pod feature to protect you from a race condition that would otherwise give
2153 #pod different permissions than you might expect. If you really want to keep the
2154 #pod original mode flags, use L</append> with the C<truncate> option.
2155 #pod
2156 #pod Current API available since 0.011.
2157 #pod
2158 #pod =cut
2159  
2160 0 ▶sub spew {
Subroutine spew: not called CC=10 SCAR=47.0
Pod documented
2161 0 my ( $self, @data ) = @_;
2162 0 ▶ my $args = ( @data && ref $data[0] eq 'HASH' ) ? shift @data : {};
Branch: 1 branch
BranchTrueFalseTotal
@data && ref $data[0] eq 'HASH' ? : 0 0 0
Condition0%0/3
@data and ref $data[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@data and ref $data[0] eq "HASH"
@data ref $data[0] eq "HASH"
Truth table0%0/3
A and B
A= @dataB= ref $data[0] eq "HASH"
ABresult
0X0
100
111
2163 0 $args = _get_args( $args, qw/binmode/ );
2164 0 my $binmode = $args->{binmode};
2165 # get default binmode from caller's lexical scope (see "perldoc open")
2166 0 ▶ $binmode = ( ( caller(0) )[10] || {} )->{'open>'} unless defined $binmode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $binmode 0 0 0
Condition0%0/2
(caller 0)[10] || {}
l!l
00
MC/DC0%0/1
(caller 0)[10] || {}
(caller 0)[10]
Truth table0%0/2
A
A= (caller 0)[10]
Aresult
11
00
2167  
2168 # writing needs to follow the link and create the tempfile in the same
2169 # dir for later atomic rename
2170 0 my $resolved_path = $self->_resolve_symlinks;
2171 0 my $temp = $resolved_path->_replacement_path;
2172  
2173 0 my $fh;
2174 0 my $ok = eval { $fh = $temp->filehandle( { exclusive => 1, locked => 1 }, ">", $binmode ); 1 };
2175 0 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
2176 0 ▶ my $msg = ref($@) eq 'Path::Tiny::Error'
Branch: 1 branch
BranchTrueFalseTotal
ref $@ eq 'Path::Tiny::Error' ? : 0 0 0
2177 ? "error opening temp file '$@->{file}' for atomic write: $@->{err}"
2178 : "error opening temp file for atomic write: $@";
2179 0 $self->_throw('spew', $self->[PATH], $msg);
2180 }
2181 0 ▶ print( {$fh} map { ref eq 'ARRAY' ? @$_ : $_ } @data) or $self->_throw('print', $temp->[PATH]);
Branch: 2 branches
BranchTrueFalseTotal
print {$fh;} map({ref $_ eq "ARRAY" ? @$_ : $_;} @data) or $self->_throw("print", $temp->[0]) 0 0 0
ref $_ eq 'ARRAY' ? : 0 0 0
2182 0 ▶ close $fh or $self->_throw( 'close', $temp->[PATH] );
Branch: 1 branch
BranchTrueFalseTotal
close $fh or $self->_throw("close", $temp->[0]) 0 0 0
2183  
2184 0 return $temp->move($resolved_path);
2185 }
2186  
2187 0 ▶sub spew_raw { splice @_, 1, 0, { binmode => ":unix" }; goto &spew }
Subroutine spew_raw: not called CC=1 SCAR=6.9
Pod documented
2188  
2189 0 ▶sub spew_utf8 {
Subroutine spew_utf8: not called CC=6 SCAR=37.4
Pod documented
2190 0 ▶ if ( defined($HAS_UU) ? $HAS_UU : ( $HAS_UU = _check_UU() ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $HAS_UU ? $HAS_UU : ($HAS_UU = _check_UU())) { } 0 0 0
elsif (defined $HAS_PU ? $HAS_PU : ($HAS_PU = _check_PU())) { } 0 0 0
defined $HAS_UU ? : 0 0 0
2191 0 my $self = shift;
2192 spew(
2193 $self,
2194 { binmode => ":unix" },
2195 0 ▶ map { Unicode::UTF8::encode_utf8($_) } map { ref eq 'ARRAY' ? @$_ : $_ } @_
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 0 0 0
2196 );
2197 }
2198 ▶ elsif ( defined($HAS_PU) ? $HAS_PU : ( $HAS_PU = _check_PU() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
defined $HAS_PU ? : 0 0 0
2199 0 splice @_, 1, 0, { binmode => ":unix:utf8_strict" };
2200 0 goto &spew;
2201 }
2202 else {
2203 0 splice @_, 1, 0, { binmode => ":unix:encoding(UTF-8)" };
2204 0 goto &spew;
2205 }
2206 }
2207  
2208 #pod =method stat, lstat
2209 #pod
2210 #pod $stat = path("foo.txt")->stat;
2211 #pod $stat = path("/some/symlink")->lstat;
2212 #pod
2213 #pod Like calling C<stat> or C<lstat> from L<File::stat>.
2214 #pod
2215 #pod Current API available since 0.001.
2216 #pod
2217 #pod =cut
2218  
2219 # XXX break out individual stat() components as subs?
2220 0 ▶sub stat {
Subroutine stat: not called CC=2 SCAR=17.9
Pod documented
2221 0 my $self = shift;
2222 0 require File::stat;
2223 0 ▶ return File::stat::stat( $self->[PATH] ) || $self->_throw('stat');
Condition0%0/3
File::stat::stat($self->[0]) || $self->_throw('stat')
l!l&&r!l&&!r
000
MC/DC0%0/2
File::stat::stat($self->[0]) || $self->_throw('stat')
File::stat::stat($self->[0]) $self->_throw('stat')
Truth table0%0/3
A || B
A= File::stat::stat($self->[0])B= $self->_throw('stat')
ABresult
1X1
011
000
2224 }
2225  
2226 0 ▶sub lstat {
Subroutine lstat: not called CC=2 SCAR=17.9
Pod documented
2227 0 my $self = shift;
2228 0 require File::stat;
2229 0 ▶ return File::stat::lstat( $self->[PATH] ) || $self->_throw('lstat');
Condition0%0/3
File::stat::lstat($self->[0]) || $self->_throw('lstat')
l!l&&r!l&&!r
000
MC/DC0%0/2
File::stat::lstat($self->[0]) || $self->_throw('lstat')
File::stat::lstat($self->[0]) $self->_throw('lstat')
Truth table0%0/3
A || B
A= File::stat::lstat($self->[0])B= $self->_throw('lstat')
ABresult
1X1
011
000
2230 }
2231  
2232 #pod =method stringify
2233 #pod
2234 #pod $path = path("foo.txt");
2235 #pod say $path->stringify; # same as "$path"
2236 #pod
2237 #pod Returns a string representation of the path. Unlike C<canonpath>, this method
2238 #pod returns the path standardized with Unix-style C</> directory separators.
2239 #pod
2240 #pod Current API available since 0.001.
2241 #pod
2242 #pod =cut
2243  
2244 26 ▶sub stringify { $_[0]->[PATH] =~ /^~/ ? './' . $_[0]->[PATH] : $_[0]->[PATH] }
Subroutine stringify: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0][0] =~ /^~/ ? : 0 26 26
2245  
2246 #pod =method subsumes
2247 #pod
2248 #pod path("foo/bar")->subsumes("foo/bar/baz"); # true
2249 #pod path("/foo/bar")->subsumes("/foo/baz"); # false
2250 #pod
2251 #pod Returns true if the first path is a prefix of the second path at a directory
2252 #pod boundary.
2253 #pod
2254 #pod This B<does not> resolve parent directory entries (C<..>) or symlinks:
2255 #pod
2256 #pod path("foo/bar")->subsumes("foo/bar/../baz"); # true
2257 #pod
2258 #pod If such things are important to you, ensure that both paths are resolved to
2259 #pod the filesystem with C<realpath>:
2260 #pod
2261 #pod my $p1 = path("foo/bar")->realpath;
2262 #pod my $p2 = path("foo/bar/../baz")->realpath;
2263 #pod if ( $p1->subsumes($p2) ) { ... }
2264 #pod
2265 #pod Current API available since 0.048.
2266 #pod
2267 #pod =cut
2268  
2269 0 ▶sub subsumes {
Subroutine subsumes: not called CC=12 SCAR=50.5
Pod documented
2270 0 my $self = shift;
2271 0 ▶ Carp::croak("subsumes() requires a defined, positive-length argument")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 0 0
2272 unless defined $_[0];
2273 0 my $other = _path(shift);
2274  
2275 # normalize absolute vs relative
2276 0 ▶ if ( $self->is_absolute && !$other->is_absolute ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_absolute and not $other->is_absolute) { } 0 0 0
elsif ($other->is_absolute and not $self->is_absolute) { } 0 0 0
Condition0%0/3
$self->is_absolute and not $other->is_absolute
!ll&&!rl&&r
000
MC/DC0%0/2
$self->is_absolute and not $other->is_absolute
$self->is_absolute not $other->is_absolute
Truth table0%0/3
A and not(B)
A= $self->is_absoluteB= not $other->is_absolute
ABresult
0X0
100
111
2277 0 $other = $other->absolute;
2278 }
2279 ▶ elsif ( $other->is_absolute && !$self->is_absolute ) {
Condition0%0/3
$other->is_absolute and not $self->is_absolute
!ll&&!rl&&r
000
MC/DC0%0/2
$other->is_absolute and not $self->is_absolute
$other->is_absolute not $self->is_absolute
Truth table0%0/3
A and not(B)
A= $other->is_absoluteB= not $self->is_absolute
ABresult
0X0
100
111
2280 0 $self = $self->absolute;
2281 }
2282  
2283 # normalize volume vs non-volume; do this after absolute path
2284 # adjustments above since that might add volumes already
2285 0 ▶ if ( length $self->volume && !length $other->volume ) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $self->volume and not length $other->volume) { } 0 0 0
elsif (length $other->volume and not length $self->volume) { } 0 0 0
Condition0%0/3
length $self->volume and not length $other->volume
!ll&&!rl&&r
000
MC/DC0%0/2
length $self->volume and not length $other->volume
length $self->volume not length $other->volume
Truth table0%0/3
A and not(B)
A= length $self->volumeB= not length $other->volume
ABresult
0X0
100
111
2286 0 $other = $other->absolute;
2287 }
2288 ▶ elsif ( length $other->volume && !length $self->volume ) {
Condition0%0/3
length $other->volume and not length $self->volume
!ll&&!rl&&r
000
MC/DC0%0/2
length $other->volume and not length $self->volume
length $other->volume not length $self->volume
Truth table0%0/3
A and not(B)
A= length $other->volumeB= not length $self->volume
ABresult
0X0
100
111
2289 0 $self = $self->absolute;
2290 }
2291  
2292 0 ▶ if ( $self->[PATH] eq '.' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->[0] eq '.') { } 0 0 0
elsif ($self->is_rootdir) { } 0 0 0
2293 0 return !!1; # cwd subsumes everything relative
2294 }
2295 elsif ( $self->is_rootdir ) {
2296 # a root directory ("/", "c:/") already ends with a separator
2297 0 return $other->[PATH] =~ m{^\Q$self->[PATH]\E};
2298 }
2299 else {
2300 # exact match or prefix breaking at a separator
2301 0 return $other->[PATH] =~ m{^\Q$self->[PATH]\E(?:/|\z)};
2302 }
2303 }
2304  
2305 #pod =method touch
2306 #pod
2307 #pod path("foo.txt")->touch;
2308 #pod path("foo.txt")->touch($epoch_secs);
2309 #pod
2310 #pod Like the Unix C<touch> utility. Creates the file if it doesn't exist, or else
2311 #pod changes the modification and access times to the current time. If the first
2312 #pod argument is the epoch seconds then it will be used.
2313 #pod
2314 #pod Returns the path object so it can be easily chained with other methods:
2315 #pod
2316 #pod # won't die if foo.txt doesn't exist
2317 #pod $content = path("foo.txt")->touch->slurp;
2318 #pod
2319 #pod Current API available since 0.015.
2320 #pod
2321 #pod =cut
2322  
2323 0 ▶sub touch {
Subroutine touch: not called CC=6 SCAR=37.4
Pod documented
2324 0 my ( $self, $epoch ) = @_;
2325 0 ▶ if ( !-e $self->[PATH] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $self->[0]) 0 0 0
2326 0 my $fh = $self->openw;
2327 0 ▶ close $fh or $self->_throw('close');
Branch: 1 branch
BranchTrueFalseTotal
close $fh or $self->_throw("close") 0 0 0
2328 }
2329 0 ▶ if ( defined $epoch ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $epoch) { } 0 0 0
2330 0 ▶ utime $epoch, $epoch, $self->[PATH]
Branch: 1 branch
BranchTrueFalseTotal
utime $epoch, $epoch, $self->[0] or $self->_throw("utime ($epoch)") 0 0 0
2331 or $self->_throw("utime ($epoch)");
2332 }
2333 else {
2334 # literal undef prevents warnings :-(
2335 0 ▶ utime undef, undef, $self->[PATH]
Branch: 1 branch
BranchTrueFalseTotal
utime undef, undef, $self->[0] or $self->_throw("utime ()") 0 0 0
2336 or $self->_throw("utime ()");
2337 }
2338 0 return $self;
2339 }
2340  
2341 #pod =method touchpath
2342 #pod
2343 #pod path("bar/baz/foo.txt")->touchpath;
2344 #pod
2345 #pod Combines C<mkdir> and C<touch>. Creates the parent directory if it doesn't exist,
2346 #pod before touching the file. Returns the path object like C<touch> does.
2347 #pod
2348 #pod If you need to pass options, use C<mkdir> and C<touch> separately:
2349 #pod
2350 #pod path("bar/baz")->mkdir( \%options )->child("foo.txt")->touch($epoch_secs);
2351 #pod
2352 #pod Current API available since 0.022.
2353 #pod
2354 #pod =cut
2355  
2356 0 ▶sub touchpath {
Subroutine touchpath: not called CC=2 SCAR=17.9
Pod documented
2357 0 my ($self) = @_;
2358 0 my $parent = $self->parent;
2359 0 ▶ $parent->mkdir unless $parent->exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent->exists 0 0 0
2360 0 $self->touch;
2361 }
2362  
2363 #pod =method visit
2364 #pod
2365 #pod path("/tmp")->visit( \&callback, \%options );
2366 #pod
2367 #pod Executes a callback for each child of a directory. It returns a hash
2368 #pod reference with any state accumulated during iteration.
2369 #pod
2370 #pod The options are the same as for L</iterator> (which it uses internally):
2371 #pod C<recurse> and C<follow_symlinks>. Both default to false.
2372 #pod
2373 #pod The callback function will receive a C<Path::Tiny> object as the first argument
2374 #pod and a hash reference to accumulate state as the second argument. For example:
2375 #pod
2376 #pod # collect files sizes
2377 #pod my $sizes = path("/tmp")->visit(
2378 #pod sub {
2379 #pod my ($path, $state) = @_;
2380 #pod return if $path->is_dir;
2381 #pod $state->{$path} = -s $path;
2382 #pod },
2383 #pod { recurse => 1 }
2384 #pod );
2385 #pod
2386 #pod For convenience, the C<Path::Tiny> object will also be locally aliased as the
2387 #pod C<$_> global variable:
2388 #pod
2389 #pod # print paths matching /foo/
2390 #pod path("/tmp")->visit( sub { say if /foo/ }, { recurse => 1} );
2391 #pod
2392 #pod If the callback returns a B<reference> to a false scalar value, iteration will
2393 #pod terminate. This is not the same as "pruning" a directory search; this just
2394 #pod stops all iteration and returns the state hash reference.
2395 #pod
2396 #pod # find up to 10 files larger than 100K
2397 #pod my $files = path("/tmp")->visit(
2398 #pod sub {
2399 #pod my ($path, $state) = @_;
2400 #pod $state->{$path}++ if -s $path > 102400
2401 #pod return \0 if keys %$state == 10;
2402 #pod },
2403 #pod { recurse => 1 }
2404 #pod );
2405 #pod
2406 #pod If you want more flexible iteration, use a module like L<Path::Iterator::Rule>.
2407 #pod
2408 #pod Current API available since 0.062.
2409 #pod
2410 #pod =cut
2411  
2412 0 ▶sub visit {
Subroutine visit: not called CC=6 SCAR=37.4
Pod documented
2413 0 my $self = shift;
2414 0 my $cb = shift;
2415 0 my $args = _get_args( shift, qw/recurse follow_symlinks/ );
2416 0 ▶ Carp::croak("Callback for visit() must be a code reference")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cb and ref $cb eq "CODE" 0 0 0
Condition0%0/3
defined $cb and ref $cb eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $cb and ref $cb eq "CODE"
defined $cb ref $cb eq "CODE"
Truth table0%0/3
A and B
A= defined $cbB= ref $cb eq "CODE"
ABresult
0X0
100
111
2417 unless defined($cb) && ref($cb) eq 'CODE';
2418 0 my $next = $self->iterator($args);
2419 0 my $state = {};
2420 0 ▶ while ( my $file = $next->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = &$next()) 0 0 0
2421 0 local $_ = $file;
2422 0 my $r = $cb->( $file, $state );
2423 0 ▶ last if ref($r) eq 'SCALAR' && !$$r;
Branch: 1 branch
BranchTrueFalseTotal
if ref $r eq "SCALAR" and not $$r 0 0 0
Condition0%0/3
ref $r eq "SCALAR" and not $$r
!ll&&!rl&&r
000
MC/DC0%0/2
ref $r eq "SCALAR" and not $$r
ref $r eq "SCALAR" not $$r
Truth table0%0/3
A and not(B)
A= ref $r eq "SCALAR"B= not $$r
ABresult
0X0
100
111
2424 }
2425 0 return $state;
2426 }
2427  
2428 #pod =method volume
2429 #pod
2430 #pod $vol = path("/tmp/foo.txt")->volume; # ""
2431 #pod $vol = path("C:/tmp/foo.txt")->volume; # "C:"
2432 #pod
2433 #pod Returns the volume portion of the path. This is equivalent
2434 #pod to what L<File::Spec> would give from C<splitpath> and thus
2435 #pod usually is the empty string on Unix-like operating systems or the
2436 #pod drive letter for an absolute path on C<MSWin32>.
2437 #pod
2438 #pod Current API available since 0.001.
2439 #pod
2440 #pod =cut
2441  
2442 0 ▶sub volume {
Subroutine volume: not called CC=2 SCAR=17.9
Pod documented
2443 0 my ($self) = @_;
2444 0 ▶ $self->_splitpath unless defined $self->[VOL];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->[2] 0 0 0
2445 0 return $self->[VOL];
2446 }
2447  
2448 package Path::Tiny::Error;
2449  
2450 7 our @CARP_NOT = qw/Path::Tiny/;
2451  
2452 0 ▶use overload ( q{""} => sub { (shift)->{msg} }, fallback => 1 );
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=0.2
2453  
2454 55 ▶sub throw {
Subroutine throw: called CC=1 SCAR=0.0
2455 55 my ( $class, $op, $file, $err ) = @_;
2456 55 chomp( my $trace = Carp::shortmess );
2457 55 my $msg = "Error $op on '$file': $err$trace\n";
2458 55 die bless { op => $op, file => $file, err => $err, msg => $msg }, $class;
2459 }
2460  
2461 7 1;
2462  
2463  
2464 # vim: ts=4 sts=4 sw=4 et:
2465  
2466 __END__
« /usr/local/lib/perl5/5.44.0/x86_64-linux/Storable.pm ↑ Summary lib/Skilletor/ClaudeSettings.pm »