source: trunk/essentials/dev-lang/perl/lib/File/Basename.pm@ 3299

Last change on this file since 3299 was 3181, checked in by bird, 19 years ago

perl 5.8.8

File size: 11.0 KB
Line 
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# A bit of juggling to insure that C<use re 'taint';> always works, since
41# File::Basename is used during the Perl build, when the re extension may
42# not be available.
43BEGIN {
44 unless (eval { require re; })
45 { eval ' sub re::import { $^H |= 0x00100000; } ' } # HINT_RE_TAINT
46 import re 'taint';
47}
48
49
50use strict;
51use 5.006;
52use warnings;
53our(@ISA, @EXPORT, $VERSION, $Fileparse_fstype, $Fileparse_igncase);
54require Exporter;
55@ISA = qw(Exporter);
56@EXPORT = qw(fileparse fileparse_set_fstype basename dirname);
57$VERSION = "2.74";
58
59fileparse_set_fstype($^O);
60
61
62=over 4
63
64=item C<fileparse>
65
66 my($filename, $directories, $suffix) = fileparse($path);
67 my($filename, $directories, $suffix) = fileparse($path, @suffixes);
68 my $filename = fileparse($path, @suffixes);
69
70The C<fileparse()> routine divides a file path into its $directories, $filename
71and (optionally) the filename $suffix.
72
73$directories contains everything up to and including the last
74directory separator in the $path including the volume (if applicable).
75The remainder of the $path is the $filename.
76
77 # On Unix returns ("baz", "/foo/bar/", "")
78 fileparse("/foo/bar/baz");
79
80 # On Windows returns ("baz", "C:\foo\bar\", "")
81 fileparse("C:\foo\bar\baz");
82
83 # On Unix returns ("", "/foo/bar/baz/", "")
84 fileparse("/foo/bar/baz/");
85
86If @suffixes are given each element is a pattern (either a string or a
87C<qr//>) matched against the end of the $filename. The matching
88portion is removed and becomes the $suffix.