← Index
NYTProf Performance Profile   « block view • line view • sub view »
For bin/hailo
  Run on Thu Oct 21 22:50:37 2010
Reported on Thu Oct 21 22:52:07 2010

Filename/home/hinrik/perl5/perlbrew/perls/perl-5.13.5/lib/5.13.5/File/Basename.pm
StatementsExecuted 57 statements in 1.27ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
11177µs120µsFile::Basename::::fileparse_set_fstypeFile::Basename::fileparse_set_fstype
11134µs38µsFile::Basename::::fileparseFile::Basename::fileparse
91131µs31µsFile::Basename::::CORE:regcompFile::Basename::CORE:regcomp (opcode)
11126µs97µsFile::Basename::::basenameFile::Basename::basename
11122µs33µsFile::Basename::::_strip_trailing_sepFile::Basename::_strip_trailing_sep
11121µs21µsFile::Basename::::BEGIN@51File::Basename::BEGIN@51
11119µs19µsFile::Basename::::BEGIN@42File::Basename::BEGIN@42
102116µs16µsFile::Basename::::CORE:matchFile::Basename::CORE:match (opcode)
11111µs16µsFile::Basename::::BEGIN@50File::Basename::BEGIN@50
11110µs20µsFile::Basename::::BEGIN@52File::Basename::BEGIN@52
11110µs10µsFile::Basename::::BEGIN@357File::Basename::BEGIN@357
1116µs6µsFile::Basename::::CORE:substFile::Basename::CORE:subst (opcode)
2115µs5µsFile::Basename::::CORE:substcontFile::Basename::CORE:substcont (opcode)
0000s0sFile::Basename::::dirnameFile::Basename::dirname
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1=head1 NAME
2
3File::Basename - Parse file paths into directory, filename and suffix.
4
5=head1 SYNOPSIS
6
7 use File::Basename;
8
9 ($name,$path,$suffix) = fileparse($fullname,@suffixlist);
10 $name = fileparse($fullname,@suffixlist);
11
12 $basename = basename($fullname,@suffixlist);
13 $dirname = dirname($fullname);
14
15
16=head1 DESCRIPTION
17
18These routines allow you to parse file paths into their directory, filename
19and suffix.
20
21B<NOTE>: C<dirname()> and C<basename()> emulate the behaviours, and
22quirks, of the shell and C functions of the same name. See each
23function's documentation for details. If your concern is just parsing
24paths it is safer to use L<File::Spec>'s C<splitpath()> and
25C<splitdir()> methods.
26
27It is guaranteed that
28
29 # Where $path_separator is / for Unix, \ for Windows, etc...
30 dirname($path) . $path_separator . basename($path);
31
32is equivalent to the original path for all systems but VMS.
33
34
35=cut
36
37
38package File::Basename;
39
40# File::Basename is used during the Perl build, when the re extension may
41# not be available, but we only actually need it if running under tainting.
42
# spent 19µs within File::Basename::BEGIN@42 which was called: # once (19µs+0s) by Getopt::Long::Descriptive::BEGIN@6 at line 47
BEGIN {
4317µs if (${^TAINT}) {
44 require re;
45 re->import('taint');
46 }
47118µs119µs}
# spent 19µs making 1 call to File::Basename::BEGIN@42
48
49
50224µs220µs
# spent 16µs (11+5) within File::Basename::BEGIN@50 which was called: # once (11µs+5µs) by Getopt::Long::Descriptive::BEGIN@6 at line 50
use strict;
# spent 16µs making 1 call to File::Basename::BEGIN@50 # spent 5µs making 1 call to strict::import
51241µs121µs
# spent 21µs within File::Basename::BEGIN@51 which was called: # once (21µs+0s) by Getopt::Long::Descriptive::BEGIN@6 at line 51
use 5.006;
# spent 21µs making 1 call to File::Basename::BEGIN@51
522915µs229µs
# spent 20µs (10+9) within File::Basename::BEGIN@52 which was called: # once (10µs+9µs) by Getopt::Long::Descriptive::BEGIN@6 at line 52
use warnings;
# spent 20µs making 1 call to File::Basename::BEGIN@52 # spent 9µs making 1 call to warnings::import
5311µsour(@ISA, @EXPORT, $VERSION, $Fileparse_fstype, $Fileparse_igncase);
5412µsrequire Exporter;
5516µs@ISA = qw(Exporter);
5612µs@EXPORT = qw(fileparse fileparse_set_fstype basename dirname);
5711µs$VERSION = "2.79";
58
5914µs1120µsfileparse_set_fstype($^O);
# spent 120µs making 1 call to File::Basename::fileparse_set_fstype
60
61
62=over 4
63
64=item C<fileparse>
65X<fileparse>
66
67 my($filename, $directories, $suffix) = fileparse($path);
68 my($filename, $directories, $suffix) = fileparse($path, @suffixes);
69 my $filename = fileparse($path, @suffixes);
70
71The C<fileparse()> routine divides a file path into its $directories, $filename
72and (optionally) the filename $suffix.
73
74$directories contains everything up to and including the last
75directory separator in the $path including the volume (if applicable).
76The remainder of the $path is the $filename.
77
78 # On Unix returns ("baz", "/foo/bar/", "")
79 fileparse("/foo/bar/baz");
80
81 # On Windows returns ("baz", "C:\foo\bar\", "")
82 fileparse("C:\foo\bar\baz");
83
84 # On Unix returns ("", "/foo/bar/baz/", "")
85 fileparse("/foo/bar/baz/");
86
87If @suffixes are given each element is a pattern (either a string or a
88C<qr//>) matched against the end of the $filename. The matching
89portion is removed and becomes the $suffix.
90
91 # On Unix returns ("baz", "/foo/bar/", ".txt")
92 fileparse("/foo/bar/baz.txt", qr/\.[^.]*/);
93
94If type is non-Unix (see C<fileparse_set_fstype()>) then the pattern
95matching for suffix removal is performed case-insensitively, since
96those systems are not case-sensitive when opening existing files.
97
98You are guaranteed that C<$directories . $filename . $suffix> will
99denote the same location as the original $path.
100
101=cut
102
103
104
# spent 38µs (34+4) within File::Basename::fileparse which was called: # once (34µs+4µs) by File::Basename::basename at line 218
sub fileparse {
1051331µs my($fullname,@suffices) = @_;
106
107 unless (defined $fullname) {
108 require Carp;
109 Carp::croak("fileparse(): need a valid pathname");
110 }
111
112 my $orig_type = '';
113 my($type,$igncase) = ($Fileparse_fstype, $Fileparse_igncase);
114
115 my($taint) = substr($fullname,0,0); # Is $fullname tainted?
116
117 if ($type eq "VMS" and $fullname =~ m{/} ) {
118 # We're doing Unix emulation
119 $orig_type = $type;
120 $type = 'Unix';
121 }
122
123 my($dirpath, $basename);
124
125312µs if (grep { $type eq $_ } qw(MSDOS DOS MSWin32 Epoc)) {
126 ($dirpath,$basename) = ($fullname =~ /^((?:.*[:\\\/])?)(.*)/s);
127 $dirpath .= '.\\' unless $dirpath =~ /[\\\/]\z/;
128 }
129 elsif ($type eq "OS2") {
130 ($dirpath,$basename) = ($fullname =~ m#^((?:.*[:\\/])?)(.*)#s);
131 $dirpath = './' unless $dirpath; # Can't be 0
132 $dirpath .= '/' unless $dirpath =~ m#[\\/]\z#;
133 }
134 elsif ($type eq "AmigaOS") {
135 ($dirpath,$basename) = ($fullname =~ /(.*[:\/])?(.*)/s);
136 $dirpath = './' unless $dirpath;
137 }
138 elsif ($type eq 'VMS' ) {
139 ($dirpath,$basename) = ($fullname =~ /^(.*[:>\]])?(.*)/s);
140 $dirpath ||= ''; # should always be defined
141 }
142 else { # Default to Unix semantics.
14314µs ($dirpath,$basename) = ($fullname =~ m{^(.*/)?(.*)}s);
# spent 4µs making 1 call to File::Basename::CORE:match
144 if ($orig_type eq 'VMS' and $fullname =~ m{^(/[^/]+/000000(/|$))(.*)}) {
145 # dev:[000000] is top of VMS tree, similar to Unix '/'
146 # so strip it off and treat the rest as "normal"
147 my $devspec = $1;
148 my $remainder = $3;
149 ($dirpath,$basename) = ($remainder =~ m{^(.*/)?(.*)}s);
150 $dirpath ||= ''; # should always be defined
151 $dirpath = $devspec.$dirpath;
152 }
153 $dirpath = './' unless $dirpath;
154 }
155
156
157 my $tail = '';
158 my $suffix = '';
159 if (@suffices) {
160 foreach $suffix (@suffices) {
161 my $pat = ($igncase ? '(?i)' : '') . "($suffix)\$";
162 if ($basename =~ s/$pat//s) {
163 $taint .= substr($suffix,0,0);
164 $tail = $1 . $tail;
165 }
166 }
167 }
168
169 # Ensure taint is propgated from the path to its pieces.
170 $tail .= $taint;
171 wantarray ? ($basename .= $taint, $dirpath .= $taint, $tail)
172 : ($basename .= $taint);
173}
174
- -
177=item C<basename>
178X<basename> X<filename>
179
180 my $filename = basename($path);
181 my $filename = basename($path, @suffixes);
182
183This function is provided for compatibility with the Unix shell command
184C<basename(1)>. It does B<NOT> always return the file name portion of a
185path as you might expect. To be safe, if you want the file name portion of
186a path use C<fileparse()>.
187
188C<basename()> returns the last level of a filepath even if the last
189level is clearly directory. In effect, it is acting like C<pop()> for
190paths. This differs from C<fileparse()>'s behaviour.
191
192 # Both return "bar"
193 basename("/foo/bar");
194 basename("/foo/bar/");
195
196@suffixes work as in C<fileparse()> except all regex metacharacters are
197quoted.
198
199 # These two function calls are equivalent.
200 my $filename = basename("/foo/bar/baz.txt", ".txt");
201 my $filename = fileparse("/foo/bar/baz.txt", qr/\Q.txt\E/);
202
203Also note that in order to be compatible with the shell command,
204C<basename()> does not strip off a suffix if it is identical to the
205remaining characters in the filename.
206
207=cut
208
209
210
# spent 97µs (26+71) within File::Basename::basename which was called: # once (26µs+71µs) by Getopt::Long::Descriptive::BEGIN@254 at line 256 of Getopt/Long/Descriptive.pm
sub basename {
211622µs my($path) = shift;
212
213 # From BSD basename(1)
214 # The basename utility deletes any prefix ending with the last slash `/'
215 # character present in string (after first stripping trailing slashes)
216133µs _strip_trailing_sep($path);
# spent 33µs making 1 call to File::Basename::_strip_trailing_sep
217
218138µs my($basename, $dirname, $suffix) = fileparse( $path, map("\Q$_\E",@_) );
# spent 38µs making 1 call to File::Basename::fileparse
219
220 # From BSD basename(1)
221 # The suffix is not stripped if it is identical to the remaining
222 # characters in string.
223 if( length $suffix and !length $basename ) {
224 $basename = $suffix;
225 }
226
227 # Ensure that basename '/' == '/'
228 if( !length $basename ) {
229 $basename = $dirname;
230 }
231
232 return $basename;
233}
234
- -
237=item C<dirname>
238X<dirname>
239
240This function is provided for compatibility with the Unix shell
241command C<dirname(1)> and has inherited some of its quirks. In spite of
242its name it does B<NOT> always return the directory name as you might
243expect. To be safe, if you want the directory name of a path use
244C<fileparse()>.
245
246Only on VMS (where there is no ambiguity between the file and directory
247portions of a path) and AmigaOS (possibly due to an implementation quirk in
248this module) does C<dirname()> work like C<fileparse($path)>, returning just the
249$directories.
250
251 # On VMS and AmigaOS
252 my $directories = dirname($path);
253
254When using Unix or MSDOS syntax this emulates the C<dirname(1)> shell function
255which is subtly different from how C<fileparse()> works. It returns all but
256the last level of a file path even if the last level is clearly a directory.
257In effect, it is not returning the directory portion but simply the path one
258level up acting like C<chop()> for file paths.
259
260Also unlike C<fileparse()>, C<dirname()> does not include a trailing slash on
261its returned path.
262
263 # returns /foo/bar. fileparse() would return /foo/bar/
264 dirname("/foo/bar/baz");
265
266 # also returns /foo/bar despite the fact that baz is clearly a
267 # directory. fileparse() would return /foo/bar/baz/
268 dirname("/foo/bar/baz/");
269
270 # returns '.'. fileparse() would return 'foo/'
271 dirname("foo/");
272
273Under VMS, if there is no directory information in the $path, then the
274current default device and directory is used.
275
276=cut
277
278
279sub dirname {
280 my $path = shift;
281
282 my($type) = $Fileparse_fstype;
283
284 if( $type eq 'VMS' and $path =~ m{/} ) {
285 # Parse as Unix
286 local($File::Basename::Fileparse_fstype) = '';
287 return dirname($path);
288 }
289
290 my($basename, $dirname) = fileparse($path);
291
292 if ($type eq 'VMS') {
293 $dirname ||= $ENV{DEFAULT};
294 }
295 elsif (grep { $type eq $_ } qw(MSDOS DOS MSWin32 OS2)) {
296 _strip_trailing_sep($dirname);
297 unless( length($basename) ) {
298 ($basename,$dirname) = fileparse $dirname;
299 _strip_trailing_sep($dirname);
300 }
301 }
302 elsif ($type eq 'AmigaOS') {
303 if ( $dirname =~ /:\z/) { return $dirname }
304 chop $dirname;
305 $dirname =~ s{[^:/]+\z}{} unless length($basename);
306 }
307 else {
308 _strip_trailing_sep($dirname);
309 unless( length($basename) ) {
310 ($basename,$dirname) = fileparse $dirname;
311 _strip_trailing_sep($dirname);
312 }
313 }
314
315 $dirname;
316}
317
318
319# Strip the trailing path separator.
320
# spent 33µs (22+11) within File::Basename::_strip_trailing_sep which was called: # once (22µs+11µs) by File::Basename::basename at line 216
sub _strip_trailing_sep {
32129µs my $type = $Fileparse_fstype;
322
323125µs if (grep { $type eq $_ } qw(MSDOS DOS MSWin32 OS2)) {
324 $_[0] =~ s/([^:])[\\\/]*\z/$1/;
325 }
326 else {
327311µs $_[0] =~ s{(.)/*\z}{$1}s;
# spent 6µs making 1 call to File::Basename::CORE:subst # spent 5µs making 2 calls to File::Basename::CORE:substcont, avg 3µs/call
328 }
329}
330
331
332=item C<fileparse_set_fstype>
333X<filesystem>
334
335 my $type = fileparse_set_fstype();
336 my $previous_type = fileparse_set_fstype($type);
337
338Normally File::Basename will assume a file path type native to your current
339operating system (ie. /foo/bar style on Unix, \foo\bar on Windows, etc...).
340With this function you can override that assumption.
341
342Valid $types are "VMS", "AmigaOS", "OS2", "RISCOS",
343"MSWin32", "DOS" (also "MSDOS" for backwards bug compatibility),
344"Epoc" and "Unix" (all case-insensitive). If an unrecognized $type is
345given "Unix" will be assumed.
346
347If you've selected VMS syntax, and the file specification you pass to
348one of these routines contains a "/", they assume you are using Unix
349emulation and apply the Unix syntax rules instead, for that function
350call only.
351
352=back
353
354=cut
355
356
357
# spent 10µs within File::Basename::BEGIN@357 which was called: # once (10µs+0s) by Getopt::Long::Descriptive::BEGIN@6 at line 380
BEGIN {
358
359210µsmy @Ignore_Case = qw(VMS AmigaOS OS2 RISCOS MSWin32 MSDOS DOS Epoc);
360my @Types = (@Ignore_Case, qw(Unix));
361
362
# spent 120µs (77+43) within File::Basename::fileparse_set_fstype which was called: # once (77µs+43µs) by Getopt::Long::Descriptive::BEGIN@6 at line 59
sub fileparse_set_fstype {
36339µs my $old = $Fileparse_fstype;
364
36548µs if (@_) {
366 my $new_type = shift;
367
368 $Fileparse_fstype = 'Unix'; # default
369 foreach my $type (@Types) {
3709106µs1843µs $Fileparse_fstype = $type if $new_type =~ /^$type/i;
# spent 31µs making 9 calls to File::Basename::CORE:regcomp, avg 3µs/call # spent 12µs making 9 calls to File::Basename::CORE:match, avg 1µs/call
371 }
372
373 $Fileparse_igncase =
374 (grep $Fileparse_fstype eq $_, @Ignore_Case) ? 1 : 0;
375 }
376
377 return $old;
378}
379
380115µs110µs}
# spent 10µs making 1 call to File::Basename::BEGIN@357
381
382
38316µs1;
384
385
386=head1 SEE ALSO
387
388L<dirname(1)>, L<basename(1)>, L<File::Spec>
 
# spent 16µs within File::Basename::CORE:match which was called 10 times, avg 2µs/call: # 9 times (12µs+0s) by File::Basename::fileparse_set_fstype at line 370, avg 1µs/call # once (4µs+0s) by File::Basename::fileparse at line 143
sub File::Basename::CORE:match; # opcode
# spent 31µs within File::Basename::CORE:regcomp which was called 9 times, avg 3µs/call: # 9 times (31µs+0s) by File::Basename::fileparse_set_fstype at line 370, avg 3µs/call
sub File::Basename::CORE:regcomp; # opcode
# spent 6µs within File::Basename::CORE:subst which was called: # once (6µs+0s) by File::Basename::_strip_trailing_sep at line 327
sub File::Basename::CORE:subst; # opcode
# spent 5µs within File::Basename::CORE:substcont which was called 2 times, avg 3µs/call: # 2 times (5µs+0s) by File::Basename::_strip_trailing_sep at line 327, avg 3µs/call
sub File::Basename::CORE:substcont; # opcode