File::Basename(3perl)

πŸ’– File::Basename β€” bestie edition πŸ’–

okay so this is the File::Basename page from the Perl Programmers Reference Guide, and honestly? it's giving "tiny module, big drama," because these little functions have opinions. let's get into it 🫢


✨ NAME ✨

File::Basename β€” parse file paths into directory, filename and suffix. that's the whole job. it's a little path-chopping icon πŸ‘‘


🌸 SYNOPSIS 🌸

use File::Basename;

my ($name, $path, $suffix) = fileparse($fullname, @suffixlist);
my $name = fileparse($fullname, @suffixlist);

my $basename = basename($fullname, @suffixlist);
my $dirname  = dirname($fullname);

look at that. four whole ways to vibe. 🌈


πŸŽ€ DESCRIPTION πŸŽ€

these routines let you parse file paths into their directory, filename and suffix. that's it, that's the dream πŸ’…

NOTE: dirname() and basename() emulate the behaviours, and quirks, of the shell and C functions of the same name. see each function's documentation for details. if your concern is just parsing paths it is safer to use File::Spec's splitpath() and splitdir() methods.

okay so like β€” quirks. they said quirks. that's the whole vibe of this module, bestie. it's acting like the shell, not like what you want. if you just want to parse a path, go to File::Spec, fr fr.

it is guaranteed that:

# Where $path_separator is / for Unix, \ for Windows, etc...
dirname($path) . $path_separator . basename($path);

is equivalent to the original path for all systems but VMS. no cap 🫑


πŸ¦‹ fileparse

my($filename, $dirs, $suffix) = fileparse($path);
my($filename, $dirs, $suffix) = fileparse($path, @suffixes);
my $filename                  = fileparse($path, @suffixes);

the fileparse() routine divides a file path into its $dirs, $filename and (optionally) the filename $suffix. so cute, so organised.

$dirs contains everything up to and including the last directory separator in the $path including the volume (if applicable). the remainder of the $path is the $filename. πŸ“

# On Unix returns ("baz", "/foo/bar/", "")
fileparse("/foo/bar/baz");

# On Windows returns ("baz", 'C:\foo\bar\', "")
fileparse('C:\foo\bar\baz');

# On Unix returns ("", "/foo/bar/baz/", "")
fileparse("/foo/bar/baz/");

if @suffixes are given each element is a pattern (either a string or a qr//) matched against the end of the $filename. the matching portion is removed and becomes the $suffix. periodt βœ‚οΈ

# On Unix returns ("baz", "/foo/bar/", ".txt")
fileparse("/foo/bar/baz.txt", qr/\.[^.]*/);

if type is non-Unix (see "fileparse_set_fstype") then the pattern matching for suffix removal is performed case-insensitively, since those systems are not case-sensitive when opening existing files. iykyk πŸ‘€

you are guaranteed that "$dirs . $filename . $suffix" will denote the same location as the original $path. πŸ”’


πŸ’œ basename

my $filename = basename($path);
my $filename = basename($path, @suffixes);

this function is provided for compatibility with the Unix shell command basename(1). it does NOT always return the file name portion of a path as you might expect. to be safe, if you want the file name portion of a path use fileparse(). bestie, please, i'm begging you πŸ₯Ί

basename() returns the last level of a filepath even if the last level is clearly directory. in effect, it is acting like pop() for paths. this differs from fileparse()'s behaviour. it's giving "i said what i said" πŸ’€

# Both return "bar"
basename("/foo/bar");
basename("/foo/bar/");

@suffixes work as in fileparse() except all regex metacharacters are quoted. important!!

# These two function calls are equivalent.
my $filename = basename("/foo/bar/baz.txt",  ".txt");
my $filename = fileparse("/foo/bar/baz.txt", qr/\Q.txt\E/);

also note that in order to be compatible with the shell command, basename() does not strip off a suffix if it is identical to the remaining characters in the filename. okay so it's built different and there's a reason 🌸


πŸ‘‘ dirname

this function is provided for compatibility with the Unix shell command dirname(1) and has inherited some of its quirks. in spite of its name it does NOT always return the directory name as you might expect. to be safe, if you want the directory name of a path use fileparse(). ma'am. 🫠

only on VMS (where there is no ambiguity between the file and directory portions of a path) and AmigaOS (possibly due to an implementation quirk in this module) does dirname() work like fileparse($path), returning just the $dirs.

# On VMS and AmigaOS
my $dirs = dirname($path);

when using Unix or MSDOS syntax this emulates the dirname(1) shell function which is subtly different from how fileparse() works. it returns all but the last level of a file path even if the last level is clearly a directory. in effect, it is not returning the directory portion but simply the path one level up acting like chop() for file paths. the audacity, honestly 😭

also unlike fileparse(), dirname() does not include a trailing slash on its returned path.

# returns /foo/bar.  fileparse() would return /foo/bar/
dirname("/foo/bar/baz");

# also returns /foo/bar despite the fact that baz is clearly a
# directory.  fileparse() would return /foo/bar/baz/
dirname("/foo/bar/baz/");

# returns '.'.  fileparse() would return 'foo/'
dirname("foo/");

under VMS, if there is no directory information in the $path, then the current default device and directory is used. noted ✍️


🧚 fileparse_set_fstype

my $type = fileparse_set_fstype();
my $previous_type = fileparse_set_fstype($type);

normally File::Basename will assume a file path type native to your current operating system (ie. /foo/bar style on Unix, \foo\bar on Windows, etc...). with this function you can override that assumption. glow-up unlocked πŸ”₯

valid $types are "MacOS", "VMS", "AmigaOS", "OS2", "RISCOS", "MSWin32", "DOS" (also "MSDOS" for backwards bug compatibility), "Epoc" and "Unix" (all case-insensitive). if an unrecognized $type is given "Unix" will be assumed.

if you've selected VMS syntax, and the file specification you pass to one of these routines contains a /, they assume you are using Unix emulation and apply the Unix syntax rules instead, for that function call only. so specific. obsessed ⭐


🌈 SEE ALSO 🌈


and that's the page, bestie!! πŸ’– this whole thing is from perl v5.44.0, dated 2026-10-17, and it lives in the File::Basename(3perl) section of the Perl Programmers Reference Guide. go forth and parse responsibly, and remember: use fileparse() if you actually want correct answers πŸŽ€βœ¨πŸ¦‹

πŸ“¦ where this page comes from
  • arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms