| 1 |
.\" -*- mode: troff; coding: utf-8 -*- |
| 2 |
.\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) |
| 3 |
.\" |
| 4 |
.\" Standard preamble: |
| 5 |
.\" ======================================================================== |
| 6 |
.de Sp \" Vertical space (when we can't use .PP) |
| 7 |
.if t .sp .5v |
| 8 |
.if n .sp |
| 9 |
.. |
| 10 |
.de Vb \" Begin verbatim text |
| 11 |
.ft CW |
| 12 |
.nf |
| 13 |
.ne \\$1 |
| 14 |
.. |
| 15 |
.de Ve \" End verbatim text |
| 16 |
.ft R |
| 17 |
.fi |
| 18 |
.. |
| 19 |
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. |
| 20 |
.ie n \{\ |
| 21 |
. ds C` "" |
| 22 |
. ds C' "" |
| 23 |
'br\} |
| 24 |
.el\{\ |
| 25 |
. ds C` |
| 26 |
. ds C' |
| 27 |
'br\} |
| 28 |
.\" |
| 29 |
.\" Escape single quotes in literal strings from groff's Unicode transform. |
| 30 |
.ie \n(.g .ds Aq \(aq |
| 31 |
.el .ds Aq ' |
| 32 |
.\" |
| 33 |
.\" If the F register is >0, we'll generate index entries on stderr for |
| 34 |
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index |
| 35 |
.\" entries marked with X<> in POD. Of course, you'll have to process the |
| 36 |
.\" output yourself in some meaningful fashion. |
| 37 |
.\" |
| 38 |
.\" Avoid warning from groff about undefined register 'F'. |
| 39 |
.de IX |
| 40 |
.. |
| 41 |
.nr rF 0 |
| 42 |
.if \n(.g .if rF .nr rF 1 |
| 43 |
.if (\n(rF:(\n(.g==0)) \{\ |
| 44 |
. if \nF \{\ |
| 45 |
. de IX |
| 46 |
. tm Index:\\$1\t\\n%\t"\\$2" |
| 47 |
.. |
| 48 |
. if !\nF==2 \{\ |
| 49 |
. nr % 0 |
| 50 |
. nr F 2 |
| 51 |
. \} |
| 52 |
. \} |
| 53 |
.\} |
| 54 |
.rr rF |
| 55 |
.\" ======================================================================== |
| 56 |
.\" |
| 57 |
.IX Title "File::Basename 3perl" |
| 58 |
.TH File::Basename 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide" |
| 59 |
.\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 60 |
.\" way too many mistakes in technical documents. |
| 61 |
.if n .ad l |
| 62 |
.nh |
| 63 |
.SH NAME |
| 64 |
File::Basename \- Parse file paths into directory, filename and suffix. |
| 65 |
.SH SYNOPSIS |
| 66 |
.IX Header "SYNOPSIS" |
| 67 |
.Vb 1 |
| 68 |
\& use File::Basename; |
| 69 |
\& |
| 70 |
\& my ($name, $path, $suffix) = fileparse($fullname, @suffixlist); |
| 71 |
\& my $name = fileparse($fullname, @suffixlist); |
| 72 |
\& |
| 73 |
\& my $basename = basename($fullname, @suffixlist); |
| 74 |
\& my $dirname = dirname($fullname); |
| 75 |
.Ve |
| 76 |
.SH DESCRIPTION |
| 77 |
.IX Header "DESCRIPTION" |
| 78 |
These routines allow you to parse file paths into their directory, filename |
| 79 |
and suffix. |
| 80 |
.PP |
| 81 |
\&\fBNOTE\fR: \f(CWdirname()\fR and \f(CWbasename()\fR emulate the behaviours, and |
| 82 |
quirks, of the shell and C functions of the same name. See each |
| 83 |
function's documentation for details. If your concern is just parsing |
| 84 |
paths it is safer to use File::Spec's \f(CWsplitpath()\fR and |
| 85 |
\&\f(CWsplitdir()\fR methods. |
| 86 |
.PP |
| 87 |
It is guaranteed that |
| 88 |
.PP |
| 89 |
.Vb 2 |
| 90 |
\& # Where $path_separator is / for Unix, \e for Windows, etc... |
| 91 |
\& dirname($path) . $path_separator . basename($path); |
| 92 |
.Ve |
| 93 |
.PP |
| 94 |
is equivalent to the original path for all systems but VMS. |
| 95 |
.ie n .IP """fileparse""" 4 |
| 96 |
.el .IP \f(CWfileparse\fR 4 |
| 97 |
.IX Xref "fileparse" |
| 98 |
.IX Item "fileparse" |
| 99 |
.Vb 3 |
| 100 |
\& my($filename, $dirs, $suffix) = fileparse($path); |
| 101 |
\& my($filename, $dirs, $suffix) = fileparse($path, @suffixes); |
| 102 |
\& my $filename = fileparse($path, @suffixes); |
| 103 |
.Ve |
| 104 |
.Sp |
| 105 |
The \f(CWfileparse()\fR routine divides a file path into its \f(CW$dirs\fR, \f(CW$filename\fR |
| 106 |
and (optionally) the filename \f(CW$suffix\fR. |
| 107 |
.Sp |
| 108 |
\&\f(CW$dirs\fR contains everything up to and including the last |
| 109 |
directory separator in the \f(CW$path\fR including the volume (if applicable). |
| 110 |
The remainder of the \f(CW$path\fR is the \f(CW$filename\fR. |
| 111 |
.Sp |
| 112 |
.Vb 2 |
| 113 |
\& # On Unix returns ("baz", "/foo/bar/", "") |
| 114 |
\& fileparse("/foo/bar/baz"); |
| 115 |
\& |
| 116 |
\& # On Windows returns ("baz", \*(AqC:\efoo\ebar\e\*(Aq, "") |
| 117 |
\& fileparse(\*(AqC:\efoo\ebar\ebaz\*(Aq); |
| 118 |
\& |
| 119 |
\& # On Unix returns ("", "/foo/bar/baz/", "") |
| 120 |
\& fileparse("/foo/bar/baz/"); |
| 121 |
.Ve |
| 122 |
.Sp |
| 123 |
If \f(CW@suffixes\fR are given each element is a pattern (either a string or a |
| 124 |
\&\f(CW\*(C`qr//\*(C'\fR) matched against the end of the \f(CW$filename\fR. The matching |
| 125 |
portion is removed and becomes the \f(CW$suffix\fR. |
| 126 |
.Sp |
| 127 |
.Vb 2 |
| 128 |
\& # On Unix returns ("baz", "/foo/bar/", ".txt") |
| 129 |
\& fileparse("/foo/bar/baz.txt", qr/\e.[^.]*/); |
| 130 |
.Ve |
| 131 |
.Sp |
| 132 |
If type is non-Unix (see "fileparse_set_fstype") then the pattern |
| 133 |
matching for suffix removal is performed case-insensitively, since |
| 134 |
those systems are not case-sensitive when opening existing files. |
| 135 |
.Sp |
| 136 |
You are guaranteed that \f(CW\*(C`$dirs . $filename . $suffix\*(C'\fR will |
| 137 |
denote the same location as the original \f(CW$path\fR. |
| 138 |
.ie n .IP """basename""" 4 |
| 139 |
.el .IP \f(CWbasename\fR 4 |
| 140 |
.IX Xref "basename filename" |
| 141 |
.IX Item "basename" |
| 142 |
.Vb 2 |
| 143 |
\& my $filename = basename($path); |
| 144 |
\& my $filename = basename($path, @suffixes); |
| 145 |
.Ve |
| 146 |
.Sp |
| 147 |
This function is provided for compatibility with the Unix shell command |
| 148 |
\&\f(CWbasename(1)\fR. It does \fBNOT\fR always return the file name portion of a |
| 149 |
path as you might expect. To be safe, if you want the file name portion of |
| 150 |
a path use \f(CWfileparse()\fR. |
| 151 |
.Sp |
| 152 |
\&\f(CWbasename()\fR returns the last level of a filepath even if the last |
| 153 |
level is clearly directory. In effect, it is acting like \f(CWpop()\fR for |
| 154 |
paths. This differs from \f(CWfileparse()\fR's behaviour. |
| 155 |
.Sp |
| 156 |
.Vb 3 |
| 157 |
\& # Both return "bar" |
| 158 |
\& basename("/foo/bar"); |
| 159 |
\& basename("/foo/bar/"); |
| 160 |
.Ve |
| 161 |
.Sp |
| 162 |
\&\f(CW@suffixes\fR work as in \f(CWfileparse()\fR except all regex metacharacters are |
| 163 |
quoted. |
| 164 |
.Sp |
| 165 |
.Vb 3 |
| 166 |
\& # These two function calls are equivalent. |
| 167 |
\& my $filename = basename("/foo/bar/baz.txt", ".txt"); |
| 168 |
\& my $filename = fileparse("/foo/bar/baz.txt", qr/\eQ.txt\eE/); |
| 169 |
.Ve |
| 170 |
.Sp |
| 171 |
Also note that in order to be compatible with the shell command, |
| 172 |
\&\f(CWbasename()\fR does not strip off a suffix if it is identical to the |
| 173 |
remaining characters in the filename. |
| 174 |
.ie n .IP """dirname""" 4 |
| 175 |
.el .IP \f(CWdirname\fR 4 |
| 176 |
.IX Xref "dirname" |
| 177 |
.IX Item "dirname" |
| 178 |
This function is provided for compatibility with the Unix shell |
| 179 |
command \f(CWdirname(1)\fR and has inherited some of its quirks. In spite of |
| 180 |
its name it does \fBNOT\fR always return the directory name as you might |
| 181 |
expect. To be safe, if you want the directory name of a path use |
| 182 |
\&\f(CWfileparse()\fR. |
| 183 |
.Sp |
| 184 |
Only on VMS (where there is no ambiguity between the file and directory |
| 185 |
portions of a path) and AmigaOS (possibly due to an implementation quirk in |
| 186 |
this module) does \f(CWdirname()\fR work like \f(CWfileparse($path)\fR, returning just the |
| 187 |
\&\f(CW$dirs\fR. |
| 188 |
.Sp |
| 189 |
.Vb 2 |
| 190 |
\& # On VMS and AmigaOS |
| 191 |
\& my $dirs = dirname($path); |
| 192 |
.Ve |
| 193 |
.Sp |
| 194 |
When using Unix or MSDOS syntax this emulates the \f(CWdirname(1)\fR shell function |
| 195 |
which is subtly different from how \f(CWfileparse()\fR works. It returns all but |
| 196 |
the last level of a file path even if the last level is clearly a directory. |
| 197 |
In effect, it is not returning the directory portion but simply the path one |
| 198 |
level up acting like \f(CWchop()\fR for file paths. |
| 199 |
.Sp |
| 200 |
Also unlike \f(CWfileparse()\fR, \f(CWdirname()\fR does not include a trailing slash on |
| 201 |
its returned path. |
| 202 |
.Sp |
| 203 |
.Vb 2 |
| 204 |
\& # returns /foo/bar. fileparse() would return /foo/bar/ |
| 205 |
\& dirname("/foo/bar/baz"); |
| 206 |
\& |
| 207 |
\& # also returns /foo/bar despite the fact that baz is clearly a |
| 208 |
\& # directory. fileparse() would return /foo/bar/baz/ |
| 209 |
\& dirname("/foo/bar/baz/"); |
| 210 |
\& |
| 211 |
\& # returns \*(Aq.\*(Aq. fileparse() would return \*(Aqfoo/\*(Aq |
| 212 |
\& dirname("foo/"); |
| 213 |
.Ve |
| 214 |
.Sp |
| 215 |
Under VMS, if there is no directory information in the \f(CW$path\fR, then the |
| 216 |
current default device and directory is used. |
| 217 |
.ie n .IP """fileparse_set_fstype""" 4 |
| 218 |
.el .IP \f(CWfileparse_set_fstype\fR 4 |
| 219 |
.IX Xref "filesystem" |
| 220 |
.IX Item "fileparse_set_fstype" |
| 221 |
.Vb 2 |
| 222 |
\& my $type = fileparse_set_fstype(); |
| 223 |
\& my $previous_type = fileparse_set_fstype($type); |
| 224 |
.Ve |
| 225 |
.Sp |
| 226 |
Normally File::Basename will assume a file path type native to your current |
| 227 |
operating system (ie. /foo/bar style on Unix, \efoo\ebar on Windows, etc...). |
| 228 |
With this function you can override that assumption. |
| 229 |
.Sp |
| 230 |
Valid \f(CW$types\fR are "MacOS", "VMS", "AmigaOS", "OS2", "RISCOS", |
| 231 |
"MSWin32", "DOS" (also "MSDOS" for backwards bug compatibility), |
| 232 |
"Epoc" and "Unix" (all case-insensitive). If an unrecognized \f(CW$type\fR is |
| 233 |
given "Unix" will be assumed. |
| 234 |
.Sp |
| 235 |
If you've selected VMS syntax, and the file specification you pass to |
| 236 |
one of these routines contains a "/", they assume you are using Unix |
| 237 |
emulation and apply the Unix syntax rules instead, for that function |
| 238 |
call only. |
| 239 |
.SH "SEE ALSO" |
| 240 |
.IX Header "SEE ALSO" |
| 241 |
\&\fBdirname\fR\|(1), \fBbasename\fR\|(1), File::Spec |