File::Basename::Extra - Extension to File::Basename, adds named access to file parts and handling of filename suffixes
version 0.004
# Note: by default no symbols get exported so make sure you export
# the ones you need!
use File::Basename::Extra qw(basename);
# basename and friends
my $file = basename('/foo/bar/file.txt'); # "file.txt"
my $fileext = basename_suffix('/foo/bar/file.txt'); # ".txt"
my $filenoext = basename_nosuffix('/foo/bar/file.txt'); # "file"
# dirname
my $dir = dirname('/foo/bar/file.txt'); # "/foo/bar/"
# fileparse
my ($filename, $dirs, $suffix) = fileparse('/foo/bar/file.txt', qr/\.[^.]*/);
# ("file", "/foo/bar/", ".txt")
# pathname
my $path = pathname('/foo/bar/file.txt'); # "/foo/bar/"
# fullname and friends
my $full = fullname('/foo/bar/file.txt'); # "/foo/bar/file.txt"
my $fullext = fullname_suffix('/foo/bar/file.txt'); # ".txt"
my $fullnoext = fullname_nosuffix('/foo/bar/file.txt'); # "/foo/bar/file"
# getting/setting the default suffix patterns
my @patterns = default_suffix_patterns(); # Returns the currently active patterns
# setting the default suffix patterns
my @previous = default_suffix_patterns(qr/[._]bar/, '\.baz');
# Now only .bar, _bar, and .baz are matched suffixes
This module provides functionalty for handling file name suffixes (aka file name extensions).
These functions are exactly the same as the corresponding ones from File::Basename except that they aren't exported by default.
Returns the file name suffix part of the given filepath. The default
suffix patterns are used if none are provided. Behaves the same as
basename
, i.e., it uses the last last level of a filepath as
filename, even if the last level is clearly directory.
Also, like basename
, files that consist of only a matched suffix
are treated as if they do not have a suffix. So, using the default
suffix pattern, basename_suffix('/Users/home/.profile')
would
return an empty string.
Note: Like the original basename
function from File::Basename,
suffix patterns are automatically escaped so pattern .bar
only
matches .bar
and not e.g., _bar
(this is not done for the
default suffix patterns, nor for patterns provided to the non-basename
family functions of this module!).
Acts basically the same as the original basename
function, except
that the default suffix patterns are used to strip the name of its
suffixes when none are provided.
Also, like basename
, files that consist of only a matched suffix
are treated as if they do not have a suffix. So, using the default
suffix pattern, basename_nosuffix('/Users/home/.profile')
would
return .profile
.
Note: Like the original basename
function from File::Basename,
suffix patterns are automatically escaped so pattern .bar
only
matches .bar
and not e.g., _bar
(this is not done for the
default suffix patterns, nor for patterns provided to the non-basename
family of functions of this module!).
Returns just the filename of the filepath, optionally stripping the
suffix when it matches a provided suffix patterns. Basically the same
as calling fileparse
in scalar context.
Returns the matched suffix of the filename. The default suffix patterns are used when none are provided.
Returns the filename with the the matched suffix stripped. The default suffix patterns are used when none are provided.
Returns the path part of the file. Contrary to dirname
, a filepath
that is clearly a directory, is treated as such (e.g., on Unix,
pathname('/foo/bar/')
returns /foo/bar/
).
Returns the provided filepath, optionally stripping the filename of its matching suffix.
Synonym for filename_suffix.
Returns the full filepath with the the matched suffix stripped. The default suffix patterns are used when none are provided.
The default suffix pattern list (see the fileparse
function in
File::Basename for details) is qr/\.[^.]*/
. Meaning that this
defines the suffix as the part of the filename from (and including)
the last dot. In other words, the part of a filename that is popularly
known as the file extension.
You can alter the suffix matching by proving this function with a different pattern list.
This function returns the pattern list that was effective before optionally changing it.
Please report any bugs or feature requests on the bugtracker website.
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
File::Basename for the suffix matching and platform specific details.
Hayo Baan [email protected]
This software is copyright (c) 2015 by Hayo Baan.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.