1File::Basename(3pm)    Perl Programmers Reference Guide    File::Basename(3pm)
2
3
4

NAME

6       File::Basename - Parse file paths into directory, filename and suffix.
7

SYNOPSIS

9           use File::Basename;
10
11           ($name,$path,$suffix) = fileparse($fullname,@suffixlist);
12           $name = fileparse($fullname,@suffixlist);
13
14           $basename = basename($fullname,@suffixlist);
15           $dirname  = dirname($fullname);
16

DESCRIPTION

18       These routines allow you to parse file paths into their directory,
19       filename and suffix.
20
21       NOTE: "dirname()" and "basename()" emulate the behaviours, and quirks,
22       of the shell and C functions of the same name.  See each function's
23       documentation for details.  If your concern is just parsing paths it is
24       safer to use File::Spec's "splitpath()" and "splitdir()" methods.
25
26       It is guaranteed that
27
28           # Where $path_separator is / for Unix, \ for Windows, etc...
29           dirname($path) . $path_separator . basename($path);
30
31       is equivalent to the original path for all systems but VMS.
32
33       "fileparse"
34               my($filename, $directories, $suffix) = fileparse($path);
35               my($filename, $directories, $suffix) = fileparse($path, @suffixes);
36               my $filename                         = fileparse($path, @suffixes);
37
38           The "fileparse()" routine divides a file path into its $directo‐
39           ries, $filename and (optionally) the filename $suffix.
40
41           $directories contains everything up to and including the last
42           directory separator in the $path including the volume (if applica‐
43           ble).  The remainder of the $path is the $filename.
44
45                # On Unix returns ("baz", "/foo/bar/", "")
46                fileparse("/foo/bar/baz");
47
48                # On Windows returns ("baz", "C:\foo\bar\", "")
49                fileparse("C:\foo\bar\baz");
50
51                # On Unix returns ("", "/foo/bar/baz/", "")
52                fileparse("/foo/bar/baz/");
53
54           If @suffixes are given each element is a pattern (either a string
55           or a "qr//") matched against the end of the $filename.  The match‐
56           ing portion is removed and becomes the $suffix.
57
58                # On Unix returns ("baz", "/foo/bar", ".txt")
59                fileparse("/foo/bar/baz", qr/\.[^.]*/);
60
61           If type is non-Unix (see "fileparse_set_fstype()") then the pattern
62           matching for suffix removal is performed case-insensitively, since
63           those systems are not case-sensitive when opening existing files.
64
65           You are guaranteed that "$directories . $filename . $suffix" will
66           denote the same location as the original $path.
67
68       "basename"
69               my $filename = basename($path);
70               my $filename = basename($path, @suffixes);
71
72           This function is provided for compatibility with the Unix shell
73           command basename(1).  It does NOT always return the file name por‐
74           tion of a path as you might expect.  To be safe, if you want the
75           file name portion of a path use "fileparse()".
76
77           "basename()" returns the last level of a filepath even if the last
78           level is clearly directory.  In effect, it is acting like "pop()"
79           for paths.  This differs from "fileparse()"'s behaviour.
80
81               # Both return "bar"
82               basename("/foo/bar");
83               basename("/foo/bar/");
84
85           @suffixes work as in "fileparse()" except all regex metacharacters
86           are quoted.
87
88               # These two function calls are equivalent.
89               my $filename = basename("/foo/bar/baz.txt",  ".txt");
90               my $filename = fileparse("/foo/bar/baz.txt", qr/\Q.txt\E/);
91
92           Also note that in order to be compatible with the shell command,
93           "basename()" does not strip off a suffix if it is identical to the
94           remaining characters in the filename.
95
96       "dirname"
97           This function is provided for compatibility with the Unix shell
98           command dirname(1) and has inherited some of its quirks.  In spite
99           of its name it does NOT always return the directory name as you
100           might expect.  To be safe, if you want the directory name of a path
101           use "fileparse()".
102
103           Only on VMS (where there is no ambiguity between the file and
104           directory portions of a path) and AmigaOS (possibly due to an
105           implementation quirk in this module) does "dirname()" work like
106           "fileparse($path)", returning just the $directories.
107
108               # On VMS and AmigaOS
109               my $directories = dirname($path);
110
111           When using Unix or MSDOS syntax this emulates the dirname(1) shell
112           function which is subtly different from how "fileparse()" works.
113           It returns all but the last level of a file path even if the last
114           level is clearly a directory.  In effect, it is not returning the
115           directory portion but simply the path one level up acting like
116           "chop()" for file paths.
117
118           Also unlike "fileparse()", "dirname()" does not include a trailing
119           slash on its returned path.
120
121               # returns /foo/bar.  fileparse() would return /foo/bar/
122               dirname("/foo/bar/baz");
123
124               # also returns /foo/bar despite the fact that baz is clearly a
125               # directory.  fileparse() would return /foo/bar/baz/
126               dirname("/foo/bar/baz/");
127
128               # returns '.'.  fileparse() would return 'foo/'
129               dirname("foo/");
130
131           Under VMS, if there is no directory information in the $path, then
132           the current default device and directory is used.
133
134       "fileparse_set_fstype"
135             my $type = fileparse_set_fstype();
136             my $previous_type = fileparse_set_fstype($type);
137
138           Normally File::Basename will assume a file path type native to your
139           current operating system (ie. /foo/bar style on Unix, \foo\bar on
140           Windows, etc...).  With this function you can override that assump‐
141           tion.
142
143           Valid $types are "MacOS", "VMS", "AmigaOS", "OS2", "RISCOS",
144           "MSWin32", "DOS" (also "MSDOS" for backwards bug compatibility),
145           "Epoc" and "Unix" (all case-insensitive).  If an unrecognized $type
146           is given "Unix" will be assumed.
147
148           If you've selected VMS syntax, and the file specification you pass
149           to one of these routines contains a "/", they assume you are using
150           Unix emulation and apply the Unix syntax rules instead, for that
151           function call only.
152

SEE ALSO

154       dirname(1), basename(1), File::Spec
155
156
157
158perl v5.8.8                       2001-09-21               File::Basename(3pm)
Impressum