Debian Codegraph @

upstream/opensuse-tumbleweed/man3/File::Basename.3pm

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man v6.0.2 (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 .\" Required to disable full justification in groff 1.23.0.
57 .if n .ds AD l
58 .\" ========================================================================
59 .\"
60 .IX Title "File::Basename 3pm"
61 .TH File::Basename 3pm 2026-03-09 "perl v5.42.1" "Perl Programmers Reference Guide"
62 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
63 .\" way too many mistakes in technical documents.
64 .if n .ad l
65 .nh
66 .SH NAME
67 File::Basename \- Parse file paths into directory, filename and suffix.
68 .SH SYNOPSIS
69 .IX Header "SYNOPSIS"
70 .Vb 1
71 \& use File::Basename;
72 \&
73 \& my ($name, $path, $suffix) = fileparse($fullname, @suffixlist);
74 \& my $name = fileparse($fullname, @suffixlist);
75 \&
76 \& my $basename = basename($fullname, @suffixlist);
77 \& my $dirname = dirname($fullname);
78 .Ve
79 .SH DESCRIPTION
80 .IX Header "DESCRIPTION"
81 These routines allow you to parse file paths into their directory, filename
82 and suffix.
83 .PP
84 \&\fBNOTE\fR: \f(CWdirname()\fR and \f(CWbasename()\fR emulate the behaviours, and
85 quirks, of the shell and C functions of the same name. See each
86 function\*(Aqs documentation for details. If your concern is just parsing
87 paths it is safer to use File::Spec\*(Aqs \f(CWsplitpath()\fR and
88 \&\f(CWsplitdir()\fR methods.
89 .PP
90 It is guaranteed that
91 .PP
92 .Vb 2
93 \& # Where $path_separator is / for Unix, \e for Windows, etc...
94 \& dirname($path) . $path_separator . basename($path);
95 .Ve
96 .PP
97 is equivalent to the original path for all systems but VMS.
98 .ie n .IP """fileparse""" 4
99 .el .IP \f(CWfileparse\fR 4
100 .IX Xref "fileparse"
101 .IX Item "fileparse"
102 .Vb 3
103 \& my($filename, $dirs, $suffix) = fileparse($path);
104 \& my($filename, $dirs, $suffix) = fileparse($path, @suffixes);
105 \& my $filename = fileparse($path, @suffixes);
106 .Ve
107 .Sp
108 The \f(CWfileparse()\fR routine divides a file path into its \f(CW$dirs\fR, \f(CW$filename\fR
109 and (optionally) the filename \f(CW$suffix\fR.
110 .Sp
111 \&\f(CW$dirs\fR contains everything up to and including the last
112 directory separator in the \f(CW$path\fR including the volume (if applicable).
113 The remainder of the \f(CW$path\fR is the \f(CW$filename\fR.
114 .Sp
115 .Vb 2
116 \& # On Unix returns ("baz", "/foo/bar/", "")
117 \& fileparse("/foo/bar/baz");
118 \&
119 \& # On Windows returns ("baz", \*(AqC:\efoo\ebar\e\*(Aq, "")
120 \& fileparse(\*(AqC:\efoo\ebar\ebaz\*(Aq);
121 \&
122 \& # On Unix returns ("", "/foo/bar/baz/", "")
123 \& fileparse("/foo/bar/baz/");
124 .Ve
125 .Sp
126 If \f(CW@suffixes\fR are given each element is a pattern (either a string or a
127 \&\f(CW\*(C`qr//\*(C'\fR) matched against the end of the \f(CW$filename\fR. The matching
128 portion is removed and becomes the \f(CW$suffix\fR.
129 .Sp
130 .Vb 2
131 \& # On Unix returns ("baz", "/foo/bar/", ".txt")
132 \& fileparse("/foo/bar/baz.txt", qr/\e.[^.]*/);
133 .Ve
134 .Sp
135 If type is non\-Unix (see "fileparse_set_fstype") then the pattern
136 matching for suffix removal is performed case\-insensitively, since
137 those systems are not case\-sensitive when opening existing files.
138 .Sp
139 You are guaranteed that \f(CW\*(C`$dirs . $filename . $suffix\*(C'\fR will
140 denote the same location as the original \f(CW$path\fR.
141 .ie n .IP """basename""" 4
142 .el .IP \f(CWbasename\fR 4
143 .IX Xref "basename filename"
144 .IX Item "basename"
145 .Vb 2
146 \& my $filename = basename($path);
147 \& my $filename = basename($path, @suffixes);
148 .Ve
149 .Sp
150 This function is provided for compatibility with the Unix shell command
151 \&\f(CWbasename(1)\fR. It does \fBNOT\fR always return the file name portion of a
152 path as you might expect. To be safe, if you want the file name portion of
153 a path use \f(CWfileparse()\fR.
154 .Sp
155 \&\f(CWbasename()\fR returns the last level of a filepath even if the last
156 level is clearly directory. In effect, it is acting like \f(CWpop()\fR for
157 paths. This differs from \f(CWfileparse()\fR\*(Aqs behaviour.
158 .Sp
159 .Vb 3
160 \& # Both return "bar"
161 \& basename("/foo/bar");
162 \& basename("/foo/bar/");
163 .Ve
164 .Sp
165 \&\f(CW@suffixes\fR work as in \f(CWfileparse()\fR except all regex metacharacters are
166 quoted.
167 .Sp
168 .Vb 3
169 \& # These two function calls are equivalent.
170 \& my $filename = basename("/foo/bar/baz.txt", ".txt");
171 \& my $filename = fileparse("/foo/bar/baz.txt", qr/\eQ.txt\eE/);
172 .Ve
173 .Sp
174 Also note that in order to be compatible with the shell command,
175 \&\f(CWbasename()\fR does not strip off a suffix if it is identical to the
176 remaining characters in the filename.
177 .ie n .IP """dirname""" 4
178 .el .IP \f(CWdirname\fR 4
179 .IX Xref "dirname"
180 .IX Item "dirname"
181 This function is provided for compatibility with the Unix shell
182 command \f(CWdirname(1)\fR and has inherited some of its quirks. In spite of
183 its name it does \fBNOT\fR always return the directory name as you might
184 expect. To be safe, if you want the directory name of a path use
185 \&\f(CWfileparse()\fR.
186 .Sp
187 Only on VMS (where there is no ambiguity between the file and directory
188 portions of a path) and AmigaOS (possibly due to an implementation quirk in
189 this module) does \f(CWdirname()\fR work like \f(CWfileparse($path)\fR, returning just the
190 \&\f(CW$dirs\fR.
191 .Sp
192 .Vb 2
193 \& # On VMS and AmigaOS
194 \& my $dirs = dirname($path);
195 .Ve
196 .Sp
197 When using Unix or MSDOS syntax this emulates the \f(CWdirname(1)\fR shell function
198 which is subtly different from how \f(CWfileparse()\fR works. It returns all but
199 the last level of a file path even if the last level is clearly a directory.
200 In effect, it is not returning the directory portion but simply the path one
201 level up acting like \f(CWchop()\fR for file paths.
202 .Sp
203 Also unlike \f(CWfileparse()\fR, \f(CWdirname()\fR does not include a trailing slash on
204 its returned path.
205 .Sp
206 .Vb 2
207 \& # returns /foo/bar. fileparse() would return /foo/bar/
208 \& dirname("/foo/bar/baz");
209 \&
210 \& # also returns /foo/bar despite the fact that baz is clearly a
211 \& # directory. fileparse() would return /foo/bar/baz/
212 \& dirname("/foo/bar/baz/");
213 \&
214 \& # returns \*(Aq.\*(Aq. fileparse() would return \*(Aqfoo/\*(Aq
215 \& dirname("foo/");
216 .Ve
217 .Sp
218 Under VMS, if there is no directory information in the \f(CW$path\fR, then the
219 current default device and directory is used.
220 .ie n .IP """fileparse_set_fstype""" 4
221 .el .IP \f(CWfileparse_set_fstype\fR 4
222 .IX Xref "filesystem"
223 .IX Item "fileparse_set_fstype"
224 .Vb 2
225 \& my $type = fileparse_set_fstype();
226 \& my $previous_type = fileparse_set_fstype($type);
227 .Ve
228 .Sp
229 Normally File::Basename will assume a file path type native to your current
230 operating system (ie. /foo/bar style on Unix, \efoo\ebar on Windows, etc...).
231 With this function you can override that assumption.
232 .Sp
233 Valid \f(CW$types\fR are "MacOS", "VMS", "AmigaOS", "OS2", "RISCOS",
234 "MSWin32", "DOS" (also "MSDOS" for backwards bug compatibility),
235 "Epoc" and "Unix" (all case\-insensitive). If an unrecognized \f(CW$type\fR is
236 given "Unix" will be assumed.
237 .Sp
238 If you\*(Aqve selected VMS syntax, and the file specification you pass to
239 one of these routines contains a "/", they assume you are using Unix
240 emulation and apply the Unix syntax rules instead, for that function
241 call only.
242 .SH "SEE ALSO"
243 .IX Header "SEE ALSO"
244 \&\fBdirname\fR\|(1), \fBbasename\fR\|(1), File::Spec