This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Underline the fact that abs2rel() makes no sense
[perl5.git] / lib / File / Basename.pm
CommitLineData
a0d0e21e
LW
1package File::Basename;
2
f06db76b
AD
3=head1 NAME
4
f06db76b
AD
5fileparse - split a pathname into pieces
6
7basename - extract just the filename from a path
8
9dirname - extract just the directory from a path
10
11=head1 SYNOPSIS
12
13 use File::Basename;
14
15 ($name,$path,$suffix) = fileparse($fullname,@suffixlist)
16 fileparse_set_fstype($os_string);
17 $basename = basename($fullname,@suffixlist);
18 $dirname = dirname($fullname);
19
3b421ef0 20 ($name,$path,$suffix) = fileparse("lib/File/Basename.pm",qr{\.pm});
f06db76b 21 fileparse_set_fstype("VMS");
8003a3f3 22 $basename = basename("lib/File/Basename.pm",".pm");
f06db76b
AD
23 $dirname = dirname("lib/File/Basename.pm");
24
25=head1 DESCRIPTION
26
27These routines allow you to parse file specifications into useful
28pieces using the syntax of different operating systems.
29
30=over 4
31
32=item fileparse_set_fstype
33
34You select the syntax via the routine fileparse_set_fstype().
ee2ff9ea 35
f06db76b 36If the argument passed to it contains one of the substrings
68dc0745 37"VMS", "MSDOS", "MacOS", "AmigaOS" or "MSWin32", the file specification
55497cff 38syntax of that operating system is used in future calls to
39fileparse(), basename(), and dirname(). If it contains none of
c7b9dd21 40these substrings, Unix syntax is used. This pattern matching is
f06db76b
AD
41case-insensitive. If you've selected VMS syntax, and the file
42specification you pass to one of these routines contains a "/",
c7b9dd21 43they assume you are using Unix emulation and apply the Unix syntax
f06db76b
AD
44rules instead, for that function call only.
45
ee2ff9ea 46If the argument passed to it contains one of the substrings "VMS",
68dc0745 47"MSDOS", "MacOS", "AmigaOS", "os2", "MSWin32" or "RISCOS", then the pattern
ee2ff9ea
CS
48matching for suffix removal is performed without regard for case,
49since those systems are not case-sensitive when opening existing files
50(though some of them preserve case on file creation).
51
f06db76b 52If you haven't called fileparse_set_fstype(), the syntax is chosen
f0c6ccdf 53by examining the builtin variable C<$^O> according to these rules.
f06db76b
AD
54
55=item fileparse
56
57The fileparse() routine divides a file specification into three
58parts: a leading B<path>, a file B<name>, and a B<suffix>. The
59B<path> contains everything up to and including the last directory
60separator in the input file specification. The remainder of the input
61file specification is then divided into B<name> and B<suffix> based on
62the optional patterns you specify in C<@suffixlist>. Each element of
3b421ef0
WL
63this list can be a qr-quoted pattern (or a string which is interpreted
64as a regular expression), and is matched
f06db76b
AD
65against the end of B<name>. If this succeeds, the matching portion of
66B<name> is removed and prepended to B<suffix>. By proper use of
67C<@suffixlist>, you can remove file types or versions for examination.
68
69You are guaranteed that if you concatenate B<path>, B<name>, and
7e2183d3 70B<suffix> together in that order, the result will denote the same
71file as the input file specification.
f06db76b
AD
72
73=back
74
75=head1 EXAMPLES
76
c7b9dd21 77Using Unix file syntax:
f06db76b 78
7e2183d3 79 ($base,$path,$type) = fileparse('/virgil/aeneid/draft.book7',
3b421ef0 80 qr{\.book\d+});
f06db76b
AD
81
82would yield
83
84 $base eq 'draft'
7e2183d3 85 $path eq '/virgil/aeneid/',
f0542300 86 $type eq '.book7'
f06db76b
AD
87
88Similarly, using VMS syntax:
89
90 ($name,$dir,$type) = fileparse('Doc_Root:[Help]Rhetoric.Rnh',
3b421ef0 91 qr{\..*});
f06db76b
AD
92
93would yield
94
95 $name eq 'Rhetoric'
96 $dir eq 'Doc_Root:[Help]'
97 $type eq '.Rnh'
98
95e8664e 99=over
2ae324a7 100
f06db76b
AD
101=item C<basename>
102
103The basename() routine returns the first element of the list produced
44a8e56a 104by calling fileparse() with the same arguments, except that it always
105quotes metacharacters in the given suffixes. It is provided for
c7b9dd21 106programmer compatibility with the Unix shell command basename(1).
f06db76b
AD
107
108=item C<dirname>
109
110The dirname() routine returns the directory portion of the input file
111specification. When using VMS or MacOS syntax, this is identical to the
112second element of the list produced by calling fileparse() with the same
7e2183d3 113input file specification. (Under VMS, if there is no directory information
114in the input file specification, then the current default device and
c7b9dd21
GS
115directory are returned.) When using Unix or MSDOS syntax, the return
116value conforms to the behavior of the Unix shell command dirname(1). This
f06db76b
AD
117is usually the same as the behavior of fileparse(), but differs in some
118cases. For example, for the input file specification F<lib/>, fileparse()
119considers the directory name to be F<lib/>, while dirname() considers the
120directory name to be F<.>).
121
2ae324a7 122=back
123
f06db76b
AD
124=cut
125
b3eb6a9b
GS
126
127## use strict;
1f47e8e2 128# A bit of juggling to insure that C<use re 'taint';> always works, since
918c0b2d
CB
129# File::Basename is used during the Perl build, when the re extension may
130# not be available.
131BEGIN {
132 unless (eval { require re; })
9cfe5470 133 { eval ' sub re::import { $^H |= 0x00100000; } ' } # HINT_RE_TAINT
918c0b2d
CB
134 import re 'taint';
135}
136
137
138
3b825e41 139use 5.006;
b395063c 140use warnings;
17f410f9 141our(@ISA, @EXPORT, $VERSION, $Fileparse_fstype, $Fileparse_igncase);
a0d0e21e
LW
142require Exporter;
143@ISA = qw(Exporter);
748a9306 144@EXPORT = qw(fileparse fileparse_set_fstype basename dirname);
2af1ab88 145$VERSION = "2.72";
7e2183d3 146
a0d0e21e
LW
147
148# fileparse_set_fstype() - specify OS-based rules used in future
149# calls to routines in this package
150#
ee2ff9ea
CS
151# Currently recognized values: VMS, MSDOS, MacOS, AmigaOS, os2, RISCOS
152# Any other name uses Unix-style rules and is case-sensitive
a0d0e21e
LW
153
154sub fileparse_set_fstype {
ee2ff9ea 155 my @old = ($Fileparse_fstype, $Fileparse_igncase);
44a8e56a 156 if (@_) {
157 $Fileparse_fstype = $_[0];
39e571d4 158 $Fileparse_igncase = ($_[0] =~ /^(?:MacOS|VMS|AmigaOS|os2|RISCOS|MSWin32|MSDOS)/i);
44a8e56a 159 }
160 wantarray ? @old : $old[0];
a0d0e21e
LW
161}
162
163# fileparse() - parse file specification
164#
f0542300 165# Version 2.4 27-Sep-1996 Charles Bailey bailey@genetics.upenn.edu
a0d0e21e
LW
166
167
168sub fileparse {
169 my($fullname,@suffices) = @_;
978ae421
SB
170 unless (defined $fullname) {
171 require Carp;
6286f723 172 Carp::croak("fileparse(): need a valid pathname");
978ae421 173 }
ee2ff9ea 174 my($fstype,$igncase) = ($Fileparse_fstype, $Fileparse_igncase);
7e2183d3 175 my($dirpath,$tail,$suffix,$basename);
12cbd720 176 my($taint) = substr($fullname,0,0); # Is $fullname tainted?
a0d0e21e
LW
177
178 if ($fstype =~ /^VMS/i) {
179 if ($fullname =~ m#/#) { $fstype = '' } # We're doing Unix emulation
180 else {
c7b9dd21 181 ($dirpath,$basename) = ($fullname =~ /^(.*[:>\]])?(.*)/s);
12cbd720 182 $dirpath ||= ''; # should always be defined
a0d0e21e
LW
183 }
184 }
fa6a1c44 185 if ($fstype =~ /^MS(DOS|Win32)|epoc/i) {
c7b9dd21
GS
186 ($dirpath,$basename) = ($fullname =~ /^((?:.*[:\\\/])?)(.*)/s);
187 $dirpath .= '.\\' unless $dirpath =~ /[\\\/]\z/;
a0d0e21e 188 }
f1e20921
IZ
189 elsif ($fstype =~ /^os2/i) {
190 ($dirpath,$basename) = ($fullname =~ m#^((?:.*[:\\/])?)(.*)#s);
191 $dirpath = './' unless $dirpath; # Can't be 0
192 $dirpath .= '/' unless $dirpath =~ m#[\\/]\z#;
193 }
c7b9dd21
GS
194 elsif ($fstype =~ /^MacOS/si) {
195 ($dirpath,$basename) = ($fullname =~ /^(.*:)?(.*)/s);
95e8664e 196 $dirpath = ':' unless $dirpath;
a0d0e21e 197 }
55497cff 198 elsif ($fstype =~ /^AmigaOS/i) {
c7b9dd21 199 ($dirpath,$basename) = ($fullname =~ /(.*[:\/])?(.*)/s);
a3156fc3 200 $dirpath = './' unless $dirpath;
55497cff 201 }
748a9306 202 elsif ($fstype !~ /^VMS/i) { # default to Unix
c7b9dd21 203 ($dirpath,$basename) = ($fullname =~ m#^(.*/)?(.*)#s);
e3830a4e 204 if ($^O eq 'VMS' and $fullname =~ m:^(/[^/]+/000000(/|$))(.*):) {
491527d0 205 # dev:[000000] is top of VMS tree, similar to Unix '/'
e3830a4e
CB
206 # so strip it off and treat the rest as "normal"
207 my $devspec = $1;
208 my $remainder = $3;
209 ($dirpath,$basename) = ($remainder =~ m#^(.*/)?(.*)#s);
5fa137f1 210 $dirpath ||= ''; # should always be defined
e3830a4e 211 $dirpath = $devspec.$dirpath;
491527d0 212 }
f0c6ccdf 213 $dirpath = './' unless $dirpath;
a0d0e21e
LW
214 }
215
216 if (@suffices) {
f06db76b 217 $tail = '';
a0d0e21e 218 foreach $suffix (@suffices) {
ee2ff9ea 219 my $pat = ($igncase ? '(?i)' : '') . "($suffix)\$";
c7b9dd21 220 if ($basename =~ s/$pat//s) {
12cbd720 221 $taint .= substr($suffix,0,0);
44a8e56a 222 $tail = $1 . $tail;
a0d0e21e
LW
223 }
224 }
225 }
226
12cbd720 227 $tail .= $taint if defined $tail; # avoid warning if $tail == undef
8d6d96c1 228 wantarray ? ($basename .= $taint, $dirpath .= $taint, $tail)
d2ccd3cb 229 : ($basename .= $taint);
a0d0e21e
LW
230}
231
232
233# basename() - returns first element of list returned by fileparse()
234
235sub basename {
748a9306
LW
236 my($name) = shift;
237 (fileparse($name, map("\Q$_\E",@_)))[0];
a0d0e21e 238}
7e2183d3 239
a0d0e21e
LW
240
241# dirname() - returns device and directory portion of file specification
242# Behavior matches that of Unix dirname(1) exactly for Unix and MSDOS
748a9306
LW
243# filespecs except for names ending with a separator, e.g., "/xx/yy/".
244# This differs from the second element of the list returned
a0d0e21e
LW
245# by fileparse() in that the trailing '/' (Unix) or '\' (MSDOS) (and
246# the last directory name if the filespec ends in a '/' or '\'), is lost.
247
248sub dirname {
249 my($basename,$dirname) = fileparse($_[0]);
250 my($fstype) = $Fileparse_fstype;
251
252 if ($fstype =~ /VMS/i) {
748a9306 253 if ($_[0] =~ m#/#) { $fstype = '' }
7e2183d3 254 else { return $dirname || $ENV{DEFAULT} }
a0d0e21e 255 }
084592ab
CN
256 if ($fstype =~ /MacOS/i) {
257 if( !length($basename) && $dirname !~ /^[^:]+:\z/) {
258 $dirname =~ s/([^:]):\z/$1/s;
259 ($basename,$dirname) = fileparse $dirname;
260 }
261 $dirname .= ":" unless $dirname =~ /:\z/;
262 }
f1e20921 263 elsif ($fstype =~ /MS(DOS|Win32)|os2/i) {
c7b9dd21 264 $dirname =~ s/([^:])[\\\/]*\z/$1/;
68dc0745 265 unless( length($basename) ) {
266 ($basename,$dirname) = fileparse $dirname;
c7b9dd21 267 $dirname =~ s/([^:])[\\\/]*\z/$1/;
68dc0745 268 }
269 }
55497cff 270 elsif ($fstype =~ /AmigaOS/i) {
c7b9dd21 271 if ( $dirname =~ /:\z/) { return $dirname }
55497cff 272 chop $dirname;
c7b9dd21 273 $dirname =~ s#[^:/]+\z## unless length($basename);
55497cff 274 }
084592ab 275 else {
c7b9dd21 276 $dirname =~ s:(.)/*\z:$1:s;
42568e28
RB
277 unless( length($basename) ) {
278 local($File::Basename::Fileparse_fstype) = $fstype;
279 ($basename,$dirname) = fileparse $dirname;
c7b9dd21 280 $dirname =~ s:(.)/*\z:$1:s;
42568e28 281 }
a0d0e21e
LW
282 }
283
284 $dirname;
285}
286
44a8e56a 287fileparse_set_fstype $^O;
a0d0e21e
LW
288
2891;