POSIX/Path.pm

filter: Statementstmt 95.8% 69 / 72 Branchbran 93.3% 28 / 30 Conditioncond 90.4% 19 / 21 MC/DCmcdc 85.7% 12 / 14 Subroutinesub 94.1% 16 / 17 Podpod 91.6% 11 / 12 totaltotal 93.3% 155 / 166 CC 23 SCAR 15.2
CC
23
Cov
94%
new
10.0
is_absolute
6.0
dirname
5.0
CRAP 4.6
« POSIX/Userland/Tar/Header.pm ↑ Summary POSIX/Mem/Bucket.pm »
1 # Copyright (c) 2014, cPanel, Inc.
2 # All rights reserved.
3 # http://cpanel.net/
4 #
5 # This is free software; you can redistribute it and/or modify it under the same
6 # terms as Perl itself. See the LICENSE file for further details.
7  
8 package Filesys::POSIX::Path;
9  
10 26 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 26 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 26 ▶use Filesys::POSIX::Error qw(throw);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 Filesys::POSIX::Path - Pathname manipulation utility class
18  
19 =head1 SYNOPSIS
20  
21 use Filesys::POSIX::Path;
22  
23 my $path = Filesys::POSIX::Path->new('/foo/bar/baz');
24  
25 printf("%s\n", $path->basename); # outputs 'baz'
26 printf("%s\n", $path->dirname); # outputs '/foo/bar'
27  
28 # outputs '/foo/bar/../baz'
29 printf("%s\n", $path->full('/foo/./././bar/../baz'));
30  
31 =head1 DESCRIPTION
32  
33 This module provides an object-oriented approach to path cleanup and
34 introspection.
35  
36 =head1 CREATING AN OBJECT
37  
38 =over
39  
40 =item C<Filesys::POSIX::Path-E<gt>new($path)>
41  
42 Creates a new path object.
43  
44 The path is split on the forward slash (/) character into tokens; empty and
45 redundant tokens are discarded. Enough context is kept to help the methods
46 implemented in this module determine the nature of the path; if it is relative
47 to root, prefixed with './', or relative to the "current working directory".
48 An C<ARRAY> reference blessed into this package's namespace is returned upon
49 success. An EINVAL is thrown if the path provided is empty.
50  
51 =back
52  
53 =cut
54  
55 986 ▶sub new {
Subroutine new: called CC=10 SCAR=23.0
Pod documented
56 986 my ( $class, $path ) = @_;
57 986 my @components = split( /\//, $path );
58 986 my @ret;
59  
60 986 ▶ if ( @components && _non_empty( $components[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@components and _non_empty($components[0])) 522 464 986
Condition100%3/3
@components and _non_empty($components[0])
!ll&&!rl&&r
36428522
MC/DC100%2/2
@components and _non_empty($components[0])
@components _non_empty($components[0])
Truth table100%3/3
A and B
A= @componentsB= _non_empty($components[0])
ABresult
0X0
100
111
61 522 push @ret, $components[0];
62 }
63  
64 986 ▶ if ( @components > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@components > 1) 619 367 986
65 619 ▶ push @ret, grep { _non_empty($_) && $_ ne '.' } @components[ 1 .. $#components ];
Condition100%3/3
_non_empty($_) && $_ ne '.'
!ll&&!rl&&r
2191565
MC/DC100%2/2
_non_empty($_) && $_ ne '.'
_non_empty($_) $_ ne '.'
Truth table100%3/3
A && B
A= _non_empty($_)B= $_ ne '.'
ABresult
0X0
100
111
66 }
67  
68 986 ▶ throw &Errno::EINVAL unless @components || _non_empty($path);
Branch: 1 branch
BranchTrueFalseTotal
unless @components or _non_empty($path) 1 985 986
Condition100%3/3
@components or _non_empty($path)
l!l&&r!l&&!r
950351
MC/DC100%2/2
@components or _non_empty($path)
@components _non_empty($path)
Truth table100%3/3
A or B
A= @componentsB= _non_empty($path)
ABresult
1X1
011
000
69  
70 985 ▶ my @hier = _non_empty( $components[0] ) ? @ret : ( '', @ret );
Branch: 1 branch
BranchTrueFalseTotal
_non_empty($components[0]) ? : 522 463 985
71  
72 985 ▶ if ( @hier == 1 && !_non_empty( $hier[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@hier == 1 and not _non_empty($hier[0])) 35 950 985
Condition100%3/3
@hier == 1 and not _non_empty($hier[0])
!ll&&!rl&&r
61933135
MC/DC100%2/2
@hier == 1 and not _non_empty($hier[0])
@hier == 1 not _non_empty($hier[0])
Truth table100%3/3
A and not(B)
A= @hier == 1B= not _non_empty($hier[0])
ABresult
0X0
100
111
73 35 @hier = ('/');
74 }
75  
76 985 return bless \@hier, $class;
77 }
78  
79 1309 ▶sub _proxy {
Subroutine _proxy: called CC=2 SCAR=6.9
80 1309 my ( $context, @args ) = @_;
81  
82 1309 ▶ unless ( ref $context eq __PACKAGE__ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $context eq __PACKAGE__) 25 1284 1309
83 25 return $context->new(@args);
84 }
85  
86 1284 return $context;
87 }
88  
89 4223 ▶sub _non_empty {
Subroutine _non_empty: called CC=3 SCAR=11.0
90 4223 my ($string) = @_;
91  
92 4223 ▶ return 0 unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 281 3942 4223
93 3942 ▶ return 0 if $string eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $string eq "" 936 3006 3942
94  
95 3006 return 1;
96 }
97  
98 =head1 PATH INTROSPECTION
99  
100 =over
101  
102 =item C<$path-E<gt>components>
103  
104 Return a list of the components parsed at object construction time.
105  
106 =cut
107  
108 9 ▶sub components {
Subroutine components: called CC=1 SCAR=0.0
Pod documented
109 9 my $self = _proxy(@_);
110  
111 9 return @$self;
112 }
113  
114 =item C<$path-E<gt>full>
115  
116 Returns a string representation of the full path. This is the same as:
117  
118 join('/', @$path);
119  
120 =cut
121  
122 813 ▶sub full {
Subroutine full: called CC=1 SCAR=0.0
Pod documented
123 813 my $self = _proxy(@_);
124 813 my @hier = @$self;
125  
126 813 return join( '/', @$self );
127 }
128  
129 =item C<$path-E<gt>dirname>
130  
131 Returns a string representation of all of the leading path elements, of course
132 save for the final path element.
133  
134 =cut
135  
136 240 ▶sub dirname {
Subroutine dirname: called CC=5 SCAR=16.1
Pod documented
137 240 my $self = _proxy(@_);
138 240 my @hier = @$self;
139  
140 240 ▶ if ( @hier > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@hier > 1) 143 97 240
141 143 my @parts = @hier[ 0 .. $#hier - 1 ];
142  
143 143 ▶ if ( @parts == 1 && !_non_empty( $parts[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts == 1 and not _non_empty($parts[0])) 23 120 143
Condition100%3/3
@parts == 1 and not _non_empty($parts[0])
!ll&&!rl&&r
992123
MC/DC100%2/2
@parts == 1 and not _non_empty($parts[0])
@parts == 1 not _non_empty($parts[0])
Truth table100%3/3
A and not(B)
A= @parts == 1B= not _non_empty($parts[0])
ABresult
0X0
100
111
144 23 return '/';
145 }
146  
147 120 return join( '/', @parts );
148 }
149  
150 97 ▶ return $hier[0] eq '/' ? '/' : '.';
Branch: 1 branch
BranchTrueFalseTotal
$hier[0] eq '/' ? : 1 96 97
151 }
152  
153 =item C<$path-E<gt>basename>
154  
155 =item C<$path-E<gt>basename($ext)>
156  
157 Returns the final path component. If called with an extension, then the method
158 will return the path component with the extension chopped off, if found.
159  
160 =cut
161  
162 247 ▶sub basename {
Subroutine basename: called CC=2 SCAR=6.9
Pod documented
163 247 my ( $self, $ext ) = ( _proxy( @_[ 0 .. 1 ] ), $_[2] );
164 247 my @hier = @$self;
165  
166 247 my $name = $hier[$#hier];
167 247 ▶ $name =~ s/$ext$// if _non_empty($ext);
Branch: 1 branch
BranchTrueFalseTotal
if _non_empty($ext) 1 246 247
168  
169 247 return $name;
170 }
171  
172 =item C<$path-E<gt>shift>
173  
174 Useful for iterating over the components of the path object. Shifts the
175 internal start-of-array pointer by one, and returns the previous first value.
176  
177 =cut
178  
179 1012 ▶sub shift {
Subroutine shift: called CC=1 SCAR=0.0
Pod documented
180 1012 my ($self) = @_;
181 1012 return shift @$self;
182 }
183  
184 =item C<$path-E<gt>push(@parts)>
185  
186 Push new components onto the current path object. Each part will be tokenized
187 on the forward slash (/) character, and useless items will be discarded.
188  
189 =cut
190  
191 10 ▶sub push {
Subroutine push: called CC=2 SCAR=7.0
Pod documented
192 10 my ( $self, @parts ) = @_;
193  
194 10 ▶ return push @$self, grep { $_ && $_ ne '.' } map { split /\// } @parts;
Condition66%2/3
$_ && $_ ne '.'
!ll&&!rl&&r
3010
MC/DC50%1/2
$_ && $_ ne '.'
$_ $_ ne '.'
Truth table66%2/3
A && B
A= $_B= $_ ne '.'
ABresult
0X0
100
111
195 }
196  
197 =item C<$path-E<gt>concat($pathname)>
198  
199 A new C<Filesys::POSIX::Path> object is created based on $pathname, and the
200 current path object's non-empty components are pushed onto that new instance.
201 The new path object is returned.
202  
203 =cut
204  
205 7 ▶sub concat {
Subroutine concat: called CC=3 SCAR=11.0
Pod documented
206 7 my ( $self, $path ) = @_;
207 7 ▶ $path = __PACKAGE__->new($path) unless ref $path eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $path eq __PACKAGE__ 6 1 7
208  
209 7 ▶ $path->push( grep { $_ && $_ ne '.' } $self->components );
Condition66%2/3
$_ && $_ ne '.'
!ll&&!rl&&r
203
MC/DC50%1/2
$_ && $_ ne '.'
$_ $_ ne '.'
Truth table66%2/3
A && B
A= $_B= $_ ne '.'
ABresult
0X0
100
111
210 7 return $path;
211 }
212  
213 =item C<$path-E<gt>concat($pathname)>
214  
215 A new C<Filesys::POSIX::Path> object is created based on C<$pathname>, and the
216 new path object's non-empty components are pushed onto the current path object.
217 The current C<$path> reference is then returned.
218  
219 =cut
220  
221 2 ▶sub append {
Subroutine append: called CC=2 SCAR=6.9
Pod undocumented
222 2 my ( $self, $path ) = @_;
223 2 ▶ $path = __PACKAGE__->new($path) unless ref $path eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $path eq __PACKAGE__ 1 1 2
224  
225 2 $self->push( grep { $_ ne '.' } $path->components );
226 2 return $self;
227 }
228  
229 =item C<$path-E<gt>pop>
230  
231 Pops the final path component off of the path object list, and returns that
232 value.
233  
234 =cut
235  
236 26 ▶sub pop {
Subroutine pop: called CC=1 SCAR=0.0
Pod documented
237 26 my ($self) = @_;
238 26 return pop @$self;
239 }
240  
241 =item C<$path-E<gt>count>
242  
243 Returns the number of components in the current path object.
244  
245 =cut
246  
247 1524 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
248 1524 my ($self) = @_;
249 1524 return scalar @$self;
250 }
251  
252 =item C<$path-E<gt>is_absolute>
253  
254 Returns true if the current path object represents an absolute path.
255  
256 =cut
257  
258 0 ▶sub is_absolute {
Subroutine is_absolute: not called CC=2 SCAR=17.9
Pod documented
259 0 my ($self) = @_;
260  
261 0 ▶ return 1 unless _non_empty( $self->[0] );
Branch: 1 branch
BranchTrueFalseTotal
unless _non_empty($self->[0]) 0 0 0
262 0 return 0;
263 }
264  
265 =back
266  
267 =cut
268  
269 26 1;
270  
271 __END__
« POSIX/Userland/Tar/Header.pm ↑ Summary POSIX/Mem/Bucket.pm »