Avoid $` $& $' in libraries
[p5sagit/p5-mst-13.2.git] / lib / File / Basename.pm
CommitLineData
a0d0e21e 1package File::Basename;
2
f06db76b 3=head1 NAME
4
f06db76b 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
20 ($name,$path,$suffix) = fileparse("lib/File/Basename.pm","\.pm");
21 fileparse_set_fstype("VMS");
22 $basename = basename("lib/File/Basename.pm",".pm");
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
ee2ff9ea 37"VMS", "MSDOS", "MacOS", or "AmigaOS", the file specification
55497cff 38syntax of that operating system is used in future calls to
39fileparse(), basename(), and dirname(). If it contains none of
40these substrings, UNIX syntax is used. This pattern matching is
f06db76b 41case-insensitive. If you've selected VMS syntax, and the file
42specification you pass to one of these routines contains a "/",
43they assume you are using UNIX emulation and apply the UNIX syntax
44rules instead, for that function call only.
45
ee2ff9ea 46If the argument passed to it contains one of the substrings "VMS",
47"MSDOS", "MacOS", "AmigaOS", "os2", or "RISCOS", then the pattern
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 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
63this list is interpreted as a regular expression, and is matched
64against the end of B<name>. If this succeeds, the matching portion of
65B<name> is removed and prepended to B<suffix>. By proper use of
66C<@suffixlist>, you can remove file types or versions for examination.
67
68You are guaranteed that if you concatenate B<path>, B<name>, and
7e2183d3 69B<suffix> together in that order, the result will denote the same
70file as the input file specification.
f06db76b 71
72=back
73
74=head1 EXAMPLES
75
76Using UNIX file syntax:
77
7e2183d3 78 ($base,$path,$type) = fileparse('/virgil/aeneid/draft.book7',
f06db76b 79 '\.book\d+');
80
81would yield
82
83 $base eq 'draft'
7e2183d3 84 $path eq '/virgil/aeneid/',
f0542300 85 $type eq '.book7'
f06db76b 86
87Similarly, using VMS syntax:
88
89 ($name,$dir,$type) = fileparse('Doc_Root:[Help]Rhetoric.Rnh',
90 '\..*');
91
92would yield
93
94 $name eq 'Rhetoric'
95 $dir eq 'Doc_Root:[Help]'
96 $type eq '.Rnh'
97
98=item C<basename>
99
100The basename() routine returns the first element of the list produced
44a8e56a 101by calling fileparse() with the same arguments, except that it always
102quotes metacharacters in the given suffixes. It is provided for
103programmer compatibility with the UNIX shell command basename(1).
f06db76b 104
105=item C<dirname>
106
107The dirname() routine returns the directory portion of the input file
108specification. When using VMS or MacOS syntax, this is identical to the
109second element of the list produced by calling fileparse() with the same
7e2183d3 110input file specification. (Under VMS, if there is no directory information
111in the input file specification, then the current default device and
112directory are returned.) When using UNIX or MSDOS syntax, the return
f06db76b 113value conforms to the behavior of the UNIX shell command dirname(1). This
114is usually the same as the behavior of fileparse(), but differs in some
115cases. For example, for the input file specification F<lib/>, fileparse()
116considers the directory name to be F<lib/>, while dirname() considers the
117directory name to be F<.>).
118
119=cut
120
f0c6ccdf 121require 5.002;
a0d0e21e 122require Exporter;
123@ISA = qw(Exporter);
748a9306 124@EXPORT = qw(fileparse fileparse_set_fstype basename dirname);
7e2183d3 125#use strict;
ee2ff9ea 126#use vars qw($VERSION $Fileparse_fstype $Fileparse_igncase);
f0542300 127$VERSION = "2.4";
7e2183d3 128
a0d0e21e 129
130# fileparse_set_fstype() - specify OS-based rules used in future
131# calls to routines in this package
132#
ee2ff9ea 133# Currently recognized values: VMS, MSDOS, MacOS, AmigaOS, os2, RISCOS
134# Any other name uses Unix-style rules and is case-sensitive
a0d0e21e 135
136sub fileparse_set_fstype {
ee2ff9ea 137 my @old = ($Fileparse_fstype, $Fileparse_igncase);
44a8e56a 138 if (@_) {
139 $Fileparse_fstype = $_[0];
ee2ff9ea 140 $Fileparse_igncase = ($_[0] =~ /^(?:MacOS|VMS|AmigaOS|os2|RISCOS)/i);
44a8e56a 141 }
142 wantarray ? @old : $old[0];
a0d0e21e 143}
144
145# fileparse() - parse file specification
146#
f0542300 147# Version 2.4 27-Sep-1996 Charles Bailey bailey@genetics.upenn.edu
a0d0e21e 148
149
150sub fileparse {
151 my($fullname,@suffices) = @_;
ee2ff9ea 152 my($fstype,$igncase) = ($Fileparse_fstype, $Fileparse_igncase);
7e2183d3 153 my($dirpath,$tail,$suffix,$basename);
a0d0e21e 154
155 if ($fstype =~ /^VMS/i) {
156 if ($fullname =~ m#/#) { $fstype = '' } # We're doing Unix emulation
157 else {
f0542300 158 ($dirpath,$basename) = ($fullname =~ /^(.*[:>\]])?(.*)/);
a0d0e21e 159 }
160 }
161 if ($fstype =~ /^MSDOS/i) {
f0542300 162 ($dirpath,$basename) = ($fullname =~ /^(.*[:\\\/])?(.*)/);
42568e28 163 $dirpath .= '.\\' unless $dirpath =~ /[\\\/]$/;
a0d0e21e 164 }
7e2183d3 165 elsif ($fstype =~ /^MacOS/i) {
f0542300 166 ($dirpath,$basename) = ($fullname =~ /^(.*:)?(.*)/);
a0d0e21e 167 }
55497cff 168 elsif ($fstype =~ /^AmigaOS/i) {
169 ($dirpath,$basename) = ($fullname =~ /(.*[:\/])?(.*)/);
a3156fc3 170 $dirpath = './' unless $dirpath;
55497cff 171 }
748a9306 172 elsif ($fstype !~ /^VMS/i) { # default to Unix
f0542300 173 ($dirpath,$basename) = ($fullname =~ m#^(.*/)?(.*)#);
f0c6ccdf 174 $dirpath = './' unless $dirpath;
a0d0e21e 175 }
176
177 if (@suffices) {
f06db76b 178 $tail = '';
a0d0e21e 179 foreach $suffix (@suffices) {
ee2ff9ea 180 my $pat = ($igncase ? '(?i)' : '') . "($suffix)\$";
44a8e56a 181 if ($basename =~ s/$pat//) {
182 $tail = $1 . $tail;
a0d0e21e 183 }
184 }
185 }
186
748a9306 187 wantarray ? ($basename,$dirpath,$tail) : $basename;
a0d0e21e 188}
189
190
191# basename() - returns first element of list returned by fileparse()
192
193sub basename {
748a9306 194 my($name) = shift;
195 (fileparse($name, map("\Q$_\E",@_)))[0];
a0d0e21e 196}
7e2183d3 197
a0d0e21e 198
199# dirname() - returns device and directory portion of file specification
200# Behavior matches that of Unix dirname(1) exactly for Unix and MSDOS
748a9306 201# filespecs except for names ending with a separator, e.g., "/xx/yy/".
202# This differs from the second element of the list returned
a0d0e21e 203# by fileparse() in that the trailing '/' (Unix) or '\' (MSDOS) (and
204# the last directory name if the filespec ends in a '/' or '\'), is lost.
205
206sub dirname {
207 my($basename,$dirname) = fileparse($_[0]);
208 my($fstype) = $Fileparse_fstype;
209
210 if ($fstype =~ /VMS/i) {
748a9306 211 if ($_[0] =~ m#/#) { $fstype = '' }
7e2183d3 212 else { return $dirname || $ENV{DEFAULT} }
a0d0e21e 213 }
214 if ($fstype =~ /MacOS/i) { return $dirname }
215 elsif ($fstype =~ /MSDOS/i) {
42568e28 216 $dirname =~ s/([^:])[\\\/]*$/$1/;
217 unless( length($basename) ) {
218 ($basename,$dirname) = fileparse $dirname;
219 $dirname =~ s/([^:])[\\\/]*$/$1/;
220 }
a0d0e21e 221 }
55497cff 222 elsif ($fstype =~ /AmigaOS/i) {
223 if ( $dirname =~ /:$/) { return $dirname }
224 chop $dirname;
225 $dirname =~ s#[^:/]+$## unless length($basename);
226 }
a0d0e21e 227 else {
42568e28 228 $dirname =~ s:(.)/*$:$1:;
229 unless( length($basename) ) {
230 local($File::Basename::Fileparse_fstype) = $fstype;
231 ($basename,$dirname) = fileparse $dirname;
232 $dirname =~ s:(.)/*$:$1:;
233 }
a0d0e21e 234 }
235
236 $dirname;
237}
238
44a8e56a 239fileparse_set_fstype $^O;
a0d0e21e 240
2411;