2 <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns=
"http://www.w3.org/1999/xhtml">
5 <title>File::Basename - Parse file paths into directory, filename and suffix.
</title>
6 <meta http-equiv=
"content-type" content=
"text/html; charset=utf-8" />
7 <link rev=
"made" href=
"mailto:" />
10 <body style=
"background-color: white">
11 <table border=
"0" width=
"100%" cellspacing=
"0" cellpadding=
"3">
12 <tr><td class=
"block" style=
"background-color: #cccccc" valign=
"middle">
13 <big><strong><span class=
"block"> File::Basename - Parse file paths into directory, filename and suffix.
</span></strong></big>
17 <p><a name=
"__index__"></a></p>
22 <li><a href=
"#name">NAME
</a></li>
23 <li><a href=
"#synopsis">SYNOPSIS
</a></li>
24 <li><a href=
"#description">DESCRIPTION
</a></li>
25 <li><a href=
"#see_also">SEE ALSO
</a></li>
32 <h1><a name=
"name">NAME
</a></h1>
33 <p>File::Basename - Parse file paths into directory, filename and suffix.
</p>
37 <h1><a name=
"synopsis">SYNOPSIS
</a></h1>
39 use File::Basename;
</pre>
41 ($name,$path,$suffix) = fileparse($fullname,@suffixlist);
42 $name = fileparse($fullname,@suffixlist);
</pre>
44 $basename = basename($fullname,@suffixlist);
45 $dirname = dirname($fullname);
</pre>
49 <h1><a name=
"description">DESCRIPTION
</a></h1>
50 <p>These routines allow you to parse file paths into their directory, filename
52 <p><strong>NOTE
</strong>:
<a href=
"#item_dirname"><code>dirname()
</code></a> and
<a href=
"#item_basename"><code>basename()
</code></a> emulate the behaviours, and
53 quirks, of the shell and C functions of the same name. See each
54 function's documentation for details. If your concern is just parsing
55 paths it is safer to use
<a href=
"file://C|\msysgit\mingw\html/lib/File/Spec.html">the File::Spec manpage
</a>'s
<code>splitpath()
</code> and
56 <code>splitdir()
</code> methods.
</p>
57 <p>It is guaranteed that
</p>
59 # Where $path_separator is / for Unix, \ for Windows, etc...
60 dirname($path) . $path_separator . basename($path);
</pre>
61 <p>is equivalent to the original path for all systems but VMS.
</p>
63 <dt><strong><a name=
"item_fileparse"><code>fileparse
</code></a></strong>
67 my($filename, $directories, $suffix) = fileparse($path);
68 my($filename, $directories, $suffix) = fileparse($path, @suffixes);
69 my $filename = fileparse($path, @suffixes);
</pre>
72 <p>The
<a href=
"#item_fileparse"><code>fileparse()
</code></a> routine divides a file path into its $directories, $filename
73 and (optionally) the filename $suffix.
</p>
76 <p>$directories contains everything up to and including the last
77 directory separator in the $path including the volume (if applicable).
78 The remainder of the $path is the $filename.
</p>
82 # On Unix returns (
"baz
",
"/foo/bar/
",
"")
83 fileparse(
"/foo/bar/baz
");
</pre>
87 # On Windows returns (
"baz
",
"C:\foo\bar\
",
"")
88 fileparse(
"C:\foo\bar\baz
");
</pre>
92 # On Unix returns (
"",
"/foo/bar/baz/
",
"")
93 fileparse(
"/foo/bar/baz/
");
</pre>
96 <p>If @suffixes are given each element is a pattern (either a string or a
97 <a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_qr_"><code>qr//
</code></a>) matched against the end of the $filename. The matching
98 portion is removed and becomes the $suffix.
</p>
102 # On Unix returns (
"baz
",
"/foo/bar
",
".txt
")
103 fileparse(
"/foo/bar/baz
", qr/\.[^.]*/);
</pre>
106 <p>If type is non-Unix (see
<a href=
"#item_fileparse_set_fstype"><code>fileparse_set_fstype()
</code></a>) then the pattern
107 matching for suffix removal is performed case-insensitively, since
108 those systems are not case-sensitive when opening existing files.
</p>
111 <p>You are guaranteed that
<code>$directories . $filename . $suffix
</code> will
112 denote the same location as the original $path.
</p>
114 <dt><strong><a name=
"item_basename"><code>basename
</code></a></strong>
118 my $filename = basename($path);
119 my $filename = basename($path, @suffixes);
</pre>
122 <p>This function is provided for compatibility with the Unix shell command
123 <a href=
"#item_basename"><code>basename(
1)
</code></a>. It does
<strong>NOT
</strong> always return the file name portion of a
124 path as you might expect. To be safe, if you want the file name portion of
125 a path use
<a href=
"#item_fileparse"><code>fileparse()
</code></a>.
</p>
128 <p><a href=
"#item_basename"><code>basename()
</code></a> returns the last level of a filepath even if the last
129 level is clearly directory. In effect, it is acting like
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_pop"><code>pop()
</code></a> for
130 paths. This differs from
<a href=
"#item_fileparse"><code>fileparse()
</code></a>'s behaviour.
</p>
134 # Both return
"bar
"
135 basename(
"/foo/bar
");
136 basename(
"/foo/bar/
");
</pre>
139 <p>@suffixes work as in
<a href=
"#item_fileparse"><code>fileparse()
</code></a> except all regex metacharacters are
144 # These two function calls are equivalent.
145 my $filename = basename(
"/foo/bar/baz.txt
",
".txt
");
146 my $filename = fileparse(
"/foo/bar/baz.txt
", qr/\Q.txt\E/);
</pre>
149 <p>Also note that in order to be compatible with the shell command,
150 <a href=
"#item_basename"><code>basename()
</code></a> does not strip off a suffix if it is identical to the
151 remaining characters in the filename.
</p>
153 <dt><strong><a name=
"item_dirname"><code>dirname
</code></a></strong>
156 <p>This function is provided for compatibility with the Unix shell
157 command
<a href=
"#item_dirname"><code>dirname(
1)
</code></a> and has inherited some of its quirks. In spite of
158 its name it does
<strong>NOT
</strong> always return the directory name as you might
159 expect. To be safe, if you want the directory name of a path use
160 <a href=
"#item_fileparse"><code>fileparse()
</code></a>.
</p>
163 <p>Only on VMS (where there is no ambiguity between the file and directory
164 portions of a path) and AmigaOS (possibly due to an implementation quirk in
165 this module) does
<a href=
"#item_dirname"><code>dirname()
</code></a> work like
<a href=
"#item_fileparse"><code>fileparse($path)
</code></a>, returning just the
171 my $directories = dirname($path);
</pre>
174 <p>When using Unix or MSDOS syntax this emulates the
<a href=
"#item_dirname"><code>dirname(
1)
</code></a> shell function
175 which is subtly different from how
<a href=
"#item_fileparse"><code>fileparse()
</code></a> works. It returns all but
176 the last level of a file path even if the last level is clearly a directory.
177 In effect, it is not returning the directory portion but simply the path one
178 level up acting like
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_chop"><code>chop()
</code></a> for file paths.
</p>
181 <p>Also unlike
<a href=
"#item_fileparse"><code>fileparse()
</code></a>,
<a href=
"#item_dirname"><code>dirname()
</code></a> does not include a trailing slash on
182 its returned path.
</p>
186 # returns /foo/bar. fileparse() would return /foo/bar/
187 dirname(
"/foo/bar/baz
");
</pre>
191 # also returns /foo/bar despite the fact that baz is clearly a
192 # directory. fileparse() would return /foo/bar/baz/
193 dirname(
"/foo/bar/baz/
");
</pre>
197 # returns '.'. fileparse() would return 'foo/'
198 dirname(
"foo/
");
</pre>
201 <p>Under VMS, if there is no directory information in the $path, then the
202 current default device and directory is used.
</p>
205 <dt><strong><a name=
"item_fileparse_set_fstype"><code>fileparse_set_fstype
</code></a></strong>
209 my $type = fileparse_set_fstype();
210 my $previous_type = fileparse_set_fstype($type);
</pre>
213 <p>Normally File::Basename will assume a file path type native to your current
214 operating system (ie. /foo/bar style on Unix, \foo\bar on Windows, etc...).
215 With this function you can override that assumption.
</p>
218 <p>Valid $types are ``MacOS'', ``VMS'', ``AmigaOS'', ``OS2'', ``RISCOS'',
219 ``MSWin32'', ``DOS'' (also ``MSDOS'' for backwards bug compatibility),
220 ``Epoc'' and ``Unix'' (all case-insensitive). If an unrecognized $type is
221 given ``Unix'' will be assumed.
</p>
224 <p>If you've selected VMS syntax, and the file specification you pass to
225 one of these routines contains a ``/'', they assume you are using Unix
226 emulation and apply the Unix syntax rules instead, for that function
233 <h1><a name=
"see_also">SEE ALSO
</a></h1>
234 <p><a href=
"#item_dirname">dirname(
1)
</a>,
<a href=
"#item_basename">basename(
1)
</a>,
<a href=
"file://C|\msysgit\mingw\html/lib/File/Spec.html">the File::Spec manpage
</a></p>
235 <table border=
"0" width=
"100%" cellspacing=
"0" cellpadding=
"3">
236 <tr><td class=
"block" style=
"background-color: #cccccc" valign=
"middle">
237 <big><strong><span class=
"block"> File::Basename - Parse file paths into directory, filename and suffix.
</span></strong></big>