blib/lib/File/Basename/Extra.pm

filter: Statementstmt 96.0% 49 / 51 Branchbran 93.7% 15 / 16 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 13 / 13 Podpod 100% 9 / 9 totaltotal 96.8% 91 / 94 SCAR 23.0
CC
10
Cov
96%
_basename_fileparse
5.1
filename_suffix
2.0
filename_nosuffix
2.0
CRAP 10.0
↑ Summary
1 package File::Basename::Extra;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: Extension to File::Basename, adds named access to file parts and handling of filename suffixes
6 2 our $VERSION = '0.004'; # VERSION
7  
8 #pod =head1 SYNOPSIS
9 #pod
10 #pod # Note: by default no symbols get exported so make sure you export
11 #pod # the ones you need!
12 #pod use File::Basename::Extra qw(basename);
13 #pod
14 #pod # basename and friends
15 #pod my $file = basename('/foo/bar/file.txt'); # "file.txt"
16 #pod my $fileext = basename_suffix('/foo/bar/file.txt'); # ".txt"
17 #pod my $filenoext = basename_nosuffix('/foo/bar/file.txt'); # "file"
18 #pod
19 #pod # dirname
20 #pod my $dir = dirname('/foo/bar/file.txt'); # "/foo/bar/"
21 #pod
22 #pod # fileparse
23 #pod my ($filename, $dirs, $suffix) = fileparse('/foo/bar/file.txt', qr/\.[^.]*/);
24 #pod # ("file", "/foo/bar/", ".txt")
25 #pod
26 #pod # pathname
27 #pod my $path = pathname('/foo/bar/file.txt'); # "/foo/bar/"
28 #pod
29 #pod # fullname and friends
30 #pod my $full = fullname('/foo/bar/file.txt'); # "/foo/bar/file.txt"
31 #pod my $fullext = fullname_suffix('/foo/bar/file.txt'); # ".txt"
32 #pod my $fullnoext = fullname_nosuffix('/foo/bar/file.txt'); # "/foo/bar/file"
33 #pod
34 #pod # getting/setting the default suffix patterns
35 #pod my @patterns = default_suffix_patterns(); # Returns the currently active patterns
36 #pod
37 #pod # setting the default suffix patterns
38 #pod my @previous = default_suffix_patterns(qr/[._]bar/, '\.baz');
39 #pod # Now only .bar, _bar, and .baz are matched suffixes
40 #pod
41 #pod =head1 DESCRIPTION
42 #pod
43 #pod This module provides functionalty for handling file name suffixes (aka
44 #pod file name extensions).
45 #pod
46 #pod =head1 SEE ALSO
47 #pod
48 #pod L<File::Basename> for the suffix matching and platform specific details.
49 #pod
50 #pod =cut
51  
52 2 ▶use File::Basename 2.74; # For _strip_trailing_sep
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 2 our @ISA = qw(Exporter File::Basename);
55 2 our @EXPORT = ();
56 2 our @EXPORT_OK = (@File::Basename::EXPORT,
57 qw(basename_suffix basename_nosuffix
58 filename filename_suffix filename_nosuffix
59 pathname
60 fullname fullname_suffix fullname_nosuffix
61 default_suffix_patterns));
62  
63 2 my @default_suffix_patterns = (qr/\.[^.]*/);
64  
65 # Special version of the fileparse function, used in the basename versions of the functions
66 sub _basename_fileparse {
67 20 ▶ my $path = shift;
Subroutine _basename_fileparse: called CC=5 SCAR=16.2
68 20 ▶ my @suffix_patterns = @_ ? map { "\Q$_\E" } @_ : @default_suffix_patterns;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 8 12 20
69  
70 # "hidden" function in File::Basename, strips final path separator
71 # (e.g., / or \)
72 20 File::Basename::_strip_trailing_sep($path);
73  
74 20 my($basename, $dirname, $suffix) = fileparse( $path, @suffix_patterns );
75  
76 # The suffix is not stripped if it is identical to the remaining
77 # characters in string.
78 20 ▶ if( length $suffix and !length $basename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $suffix and not length $basename) 6 14 20
Condition100%3/3
length $suffix and not length $basename
!ll&&!rl&&r
686
MC/DC100%2/2
length $suffix and not length $basename
length $suffix not length $basename
Truth table100%3/3
A and not(B)
A= length $suffixB= not length $basename
ABresult
0X0
100
111
79 6 $basename = $suffix;
80 6 $suffix = '';
81 }
82  
83 # Ensure that basename '/' == '/'
84 20 ▶ if( !length $basename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $basename) 0 20 20
85 0 $basename = $dirname;
86 0 $dirname = '';
87 }
88  
89 20 return ($basename, $dirname, $suffix);
90 }
91  
92 #pod =func fileparse FILEPATH
93 #pod
94 #pod =func fileparse FILEPATH PATTERN_LIST
95 #pod
96 #pod =func basename FILEPATH
97 #pod
98 #pod =func basename FILEPATH PATTERN_LIST
99 #pod
100 #pod =func dirname FILEPATH
101 #pod
102 #pod =func fileparse_set_fstype FSTYPE
103 #pod
104 #pod These functions are exactly the same as the corresponding ones from
105 #pod L<File::Basename> except that they aren't exported by default.
106 #pod
107 #pod =func basename_suffix FILEPATH
108 #pod
109 #pod =func basename_suffix FILEPATH PATTERN_LIST
110 #pod
111 #pod Returns the file name suffix part of the given filepath. The default
112 #pod suffix patterns are used if none are provided. Behaves the same as
113 #pod C<basename>, i.e., it uses the last last level of a filepath as
114 #pod filename, even if the last level is clearly directory.
115 #pod
116 #pod Also, like C<basename>, files that consist of only a matched suffix
117 #pod are treated as if they do not have a suffix. So, using the default
118 #pod suffix pattern, C<basename_suffix('/Users/home/.profile')> would
119 #pod return an empty string.
120 #pod
121 #pod Note: Like the original C<basename> function from L<File::Basename>,
122 #pod suffix patterns are automatically escaped so pattern C<.bar> only
123 #pod matches C<.bar> and not e.g., C<_bar> (this is B<not> done for the
124 #pod default suffix patterns, nor for patterns provided to the non-basename
125 #pod family functions of this module!).
126 #pod
127 #pod =cut
128  
129 sub basename_suffix {
130 10 ▶ my (undef, undef, $suffix) = _basename_fileparse(@_);
Subroutine basename_suffix: called CC=1 SCAR=0.0
Pod documented
131 10 return $suffix;
132 }
133  
134 #pod =func basename_nosuffix FILEPATH
135 #pod
136 #pod =func basename_nosuffix FILEPATH PATTERN_LIST
137 #pod
138 #pod Acts basically the same as the original C<basename> function, except
139 #pod that the default suffix patterns are used to strip the name of its
140 #pod suffixes when none are provided.
141 #pod
142 #pod Also, like C<basename>, files that consist of only a matched suffix
143 #pod are treated as if they do not have a suffix. So, using the default
144 #pod suffix pattern, C<basename_nosuffix('/Users/home/.profile')> would
145 #pod return C<.profile>.
146 #pod
147 #pod Note: Like the original C<basename> function from L<File::Basename>,
148 #pod suffix patterns are automatically escaped so pattern C<.bar> only
149 #pod matches C<.bar> and not e.g., C<_bar> (this is B<not> done for the
150 #pod default suffix patterns, nor for patterns provided to the non-basename
151 #pod family of functions of this module!).
152 #pod
153 #pod =cut
154  
155 sub basename_nosuffix {
156 10 ▶ my ($name, undef, undef) = _basename_fileparse(@_);
Subroutine basename_nosuffix: called CC=1 SCAR=0.0
Pod documented
157 10 return $name;
158 }
159  
160 #pod =func filename FILEPATH
161 #pod
162 #pod =func filename FILEPATH PATTERN_LIST
163 #pod
164 #pod Returns just the filename of the filepath, optionally stripping the
165 #pod suffix when it matches a provided suffix patterns. Basically the same
166 #pod as calling C<fileparse> in scalar context.
167 #pod
168 #pod =cut
169  
170 sub filename {
171 5 ▶ my ($filename, undef, undef) = fileparse(@_);
Subroutine filename: called CC=1 SCAR=0.0
Pod documented
172 5 return $filename;
173 }
174  
175 #pod =func filename_suffix FILEPATH
176 #pod
177 #pod =func filename_suffix FILEPATH PATTERN_LIST
178 #pod
179 #pod Returns the matched suffix of the filename. The default suffix
180 #pod patterns are used when none are provided.
181 #pod
182 #pod =cut
183  
184 sub filename_suffix {
185 30 ▶ my $fullname = shift;
Subroutine filename_suffix: called CC=2 SCAR=6.9
Pod documented
186 30 ▶ my (undef, undef, $suffix) = fileparse($fullname, (@_ ? @_ : @default_suffix_patterns));
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 13 17 30
187 30 return $suffix;
188 }
189  
190 #pod =func filename_nosuffix FILEPATH
191 #pod
192 #pod =func filename_nosuffix FILEPATH PATTERN_LIST
193 #pod
194 #pod Returns the filename with the the matched suffix stripped. The default
195 #pod suffix patterns are used when none are provided.
196 #pod
197 #pod =cut
198  
199 sub filename_nosuffix {
200 11 ▶ my $fullname = shift;
Subroutine filename_nosuffix: called CC=2 SCAR=6.9
Pod documented
201 11 ▶ my ($filename, undef, undef) = fileparse($fullname, (@_ ? @_ : @default_suffix_patterns));
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 4 7 11
202 11 return $filename;
203 }
204  
205 #pod =func pathname FILEPATH
206 #pod
207 #pod Returns the path part of the file. Contrary to C<dirname>, a filepath
208 #pod that is clearly a directory, is treated as such (e.g., on Unix,
209 #pod C<pathname('/foo/bar/')> returns C</foo/bar/>).
210 #pod
211 #pod =cut
212  
213 sub pathname {
214 2 ▶ my (undef, $pathname, undef) = fileparse(@_);
Subroutine pathname: called CC=1 SCAR=0.0
Pod documented
215 2 return $pathname;
216 }
217  
218 #pod =func fullname FILEPATH
219 #pod
220 #pod =func fullname FILEPATH PATTERN_LIST
221 #pod
222 #pod Returns the provided filepath, optionally stripping the filename of
223 #pod its matching suffix.
224 #pod
225 #pod =cut
226  
227 sub fullname {
228 4 ▶ my $fullname = shift;
Subroutine fullname: called CC=2 SCAR=6.9
Pod documented
229 4 ▶ return @_ ? fullname_nosuffix($fullname, @_) : $fullname;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 3 4
230 }
231  
232 #pod =func fullname_suffix FILEPATH
233 #pod
234 #pod =func fullname_suffix FILEPATH PATTERN_LIST
235 #pod
236 #pod Synonym for filename_suffix.
237 #pod
238 #pod =cut
239  
240 2 *fullname_suffix = *filename_suffix;
241  
242 #pod =func fullname_nosuffix FILEPATH
243 #pod
244 #pod =func fullname_nosuffix FILEPATH PATTERN_LIST
245 #pod
246 #pod Returns the full filepath with the the matched suffix stripped. The
247 #pod default suffix patterns are used when none are provided.
248 #pod
249 #pod =cut
250  
251 sub fullname_nosuffix {
252 10 ▶ my $fullname = shift;
Subroutine fullname_nosuffix: called CC=2 SCAR=6.9
Pod documented
253 10 my $suffix = filename_suffix($fullname, @_);
254 10 ▶ $fullname =~ s/\Q$suffix\E$// if $suffix;
Branch: 1 branch
BranchTrueFalseTotal
if $suffix 8 2 10
255 10 return $fullname;
256 }
257  
258 #pod =func default_suffix_patterns
259 #pod
260 #pod =func default_suffix_patterns NEW_PATTERN_LIST
261 #pod
262 #pod The default suffix pattern list (see the C<fileparse> function in
263 #pod L<File::Basename> for details) is C<qr/\.[^.]*/>. Meaning that this
264 #pod defines the suffix as the part of the filename from (and including)
265 #pod the last dot. In other words, the part of a filename that is popularly
266 #pod known as the file extension.
267 #pod
268 #pod You can alter the suffix matching by proving this function with a
269 #pod different pattern list.
270 #pod
271 #pod This function returns the pattern list that was effective I<before>
272 #pod optionally changing it.
273 #pod
274 #pod =cut
275  
276 sub default_suffix_patterns {
277 3 ▶ my @org_suffix_patterns = @default_suffix_patterns;
Subroutine default_suffix_patterns: called CC=2 SCAR=6.9
Pod documented
278 3 ▶ @default_suffix_patterns = @_ if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 2 3
279 3 return @org_suffix_patterns;
280 }
281  
282 2 1;
283  
284 __END__
↑ Summary