| 1 | =head1 NAME
|
|---|
| 2 |
|
|---|
| 3 | File::Basename - Parse file paths into directory, filename and suffix.
|
|---|
| 4 |
|
|---|
| 5 | =head1 SYNOPSIS
|
|---|
| 6 |
|
|---|
| 7 | use File::Basename;
|
|---|
| 8 |
|
|---|
| 9 | ($name,$path,$suffix) = fileparse($fullname,@suffixlist);
|
|---|
| 10 | $name = fileparse($fullname,@suffixlist);
|
|---|
| 11 |
|
|---|
| 12 | $basename = basename($fullname,@suffixlist);
|
|---|
| 13 | $dirname = dirname($fullname);
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 | =head1 DESCRIPTION
|
|---|
| 17 |
|
|---|
| 18 | These routines allow you to parse file paths into their directory, filename
|
|---|
| 19 | and suffix.
|
|---|
| 20 |
|
|---|
| 21 | B<NOTE>: C<dirname()> and C<basename()> emulate the behaviours, and
|
|---|
| 22 | quirks, of the shell and C functions of the same name. See each
|
|---|
| 23 | function's documentation for details. If your concern is just parsing
|
|---|
| 24 | paths it is safer to use L<File::Spec>'s C<splitpath()> and
|
|---|
| 25 | C<splitdir()> methods.
|
|---|
| 26 |
|
|---|
| 27 | It is guaranteed that
|
|---|
| 28 |
|
|---|
| 29 | # Where $path_separator is / for Unix, \ for Windows, etc...
|
|---|
| 30 | dirname($path) . $path_separator . basename($path);
|
|---|
| 31 |
|
|---|
| 32 | is equivalent to the original path for all systems but VMS.
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 | =cut
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 | package File::Basename;
|
|---|
| 39 |
|
|---|
| 40 | # A bit of juggling to insure that C<use re 'taint';> always works, since
|
|---|
| 41 | # File::Basename is used during the Perl build, when the re extension may
|
|---|
| 42 | # not be available.
|
|---|
| 43 | BEGIN {
|
|---|
| 44 | unless (eval { require re; })
|
|---|
| 45 | { eval ' sub re::import { $^H |= 0x00100000; } ' } # HINT_RE_TAINT
|
|---|
| 46 | import re 'taint';
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 | use strict;
|
|---|
| 51 | use 5.006;
|
|---|
| 52 | use warnings;
|
|---|
| 53 | our(@ISA, @EXPORT, $VERSION, $Fileparse_fstype, $Fileparse_igncase);
|
|---|
| 54 | require Exporter;
|
|---|
| 55 | @ISA = qw(Exporter);
|
|---|
| 56 | @EXPORT = qw(fileparse fileparse_set_fstype basename dirname);
|
|---|
| 57 | $VERSION = "2.74";
|
|---|
| 58 |
|
|---|
| 59 | fileparse_set_fstype($^O);
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 | =over 4
|
|---|
| 63 |
|
|---|
| 64 | =item C<fileparse>
|
|---|
| 65 |
|
|---|
| 66 | my($filename, $directories, $suffix) = fileparse($path);
|
|---|
| 67 | my($filename, $directories, $suffix) = fileparse($path, @suffixes);
|
|---|
| 68 | my $filename = fileparse($path, @suffixes);
|
|---|
| 69 |
|
|---|
| 70 | The C<fileparse()> routine divides a file path into its $directories, $filename
|
|---|
| 71 | and (optionally) the filename $suffix.
|
|---|
| 72 |
|
|---|
| 73 | $directories contains everything up to and including the last
|
|---|
| 74 | directory separator in the $path including the volume (if applicable).
|
|---|
| 75 | The remainder of the $path is the $filename.
|
|---|
| 76 |
|
|---|
| 77 | # On Unix returns ("baz", "/foo/bar/", "")
|
|---|
| 78 | fileparse("/foo/bar/baz");
|
|---|
| 79 |
|
|---|
| 80 | # On Windows returns ("baz", "C:\foo\bar\", "")
|
|---|
| 81 | fileparse("C:\foo\bar\baz");
|
|---|
| 82 |
|
|---|
| 83 | # On Unix returns ("", "/foo/bar/baz/", "")
|
|---|
| 84 | fileparse("/foo/bar/baz/");
|
|---|
| 85 |
|
|---|
| 86 | If @suffixes are given each element is a pattern (either a string or a
|
|---|
| 87 | C<qr//>) matched against the end of the $filename. The matching
|
|---|
| 88 | portion is removed and becomes the $suffix.
|
|---|
|
|---|