Start anew
[git/jnareb-git.git] / lib / perl5 / 5.6.1 / File / Basename.pm
blobc04f9c415f44f475ef930199ad40c15fdb1198ee
1 package File::Basename;
3 =head1 NAME
5 fileparse - split a pathname into pieces
7 basename - extract just the filename from a path
9 dirname - extract just the directory from a path
11 =head1 SYNOPSIS
13 use File::Basename;
15 ($name,$path,$suffix) = fileparse($fullname,@suffixlist)
16 fileparse_set_fstype($os_string);
17 $basename = basename($fullname,@suffixlist);
18 $dirname = dirname($fullname);
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");
25 =head1 DESCRIPTION
27 These routines allow you to parse file specifications into useful
28 pieces using the syntax of different operating systems.
30 =over 4
32 =item fileparse_set_fstype
34 You select the syntax via the routine fileparse_set_fstype().
36 If the argument passed to it contains one of the substrings
37 "VMS", "MSDOS", "MacOS", "AmigaOS" or "MSWin32", the file specification
38 syntax of that operating system is used in future calls to
39 fileparse(), basename(), and dirname(). If it contains none of
40 these substrings, Unix syntax is used. This pattern matching is
41 case-insensitive. If you've selected VMS syntax, and the file
42 specification you pass to one of these routines contains a "/",
43 they assume you are using Unix emulation and apply the Unix syntax
44 rules instead, for that function call only.
46 If the argument passed to it contains one of the substrings "VMS",
47 "MSDOS", "MacOS", "AmigaOS", "os2", "MSWin32" or "RISCOS", then the pattern
48 matching for suffix removal is performed without regard for case,
49 since those systems are not case-sensitive when opening existing files
50 (though some of them preserve case on file creation).
52 If you haven't called fileparse_set_fstype(), the syntax is chosen
53 by examining the builtin variable C<$^O> according to these rules.
55 =item fileparse
57 The fileparse() routine divides a file specification into three
58 parts: a leading B<path>, a file B<name>, and a B<suffix>. The
59 B<path> contains everything up to and including the last directory
60 separator in the input file specification. The remainder of the input
61 file specification is then divided into B<name> and B<suffix> based on
62 the optional patterns you specify in C<@suffixlist>. Each element of
63 this list is interpreted as a regular expression, and is matched
64 against the end of B<name>. If this succeeds, the matching portion of
65 B<name> is removed and prepended to B<suffix>. By proper use of
66 C<@suffixlist>, you can remove file types or versions for examination.
68 You are guaranteed that if you concatenate B<path>, B<name>, and
69 B<suffix> together in that order, the result will denote the same
70 file as the input file specification.
72 =back
74 =head1 EXAMPLES
76 Using Unix file syntax:
78 ($base,$path,$type) = fileparse('/virgil/aeneid/draft.book7',
79 '\.book\d+');
81 would yield
83 $base eq 'draft'
84 $path eq '/virgil/aeneid/',
85 $type eq '.book7'
87 Similarly, using VMS syntax:
89 ($name,$dir,$type) = fileparse('Doc_Root:[Help]Rhetoric.Rnh',
90 '\..*');
92 would yield
94 $name eq 'Rhetoric'
95 $dir eq 'Doc_Root:[Help]'
96 $type eq '.Rnh'
98 =over 4
100 =item C<basename>
102 The basename() routine returns the first element of the list produced
103 by calling fileparse() with the same arguments, except that it always
104 quotes metacharacters in the given suffixes. It is provided for
105 programmer compatibility with the Unix shell command basename(1).
107 =item C<dirname>
109 The dirname() routine returns the directory portion of the input file
110 specification. When using VMS or MacOS syntax, this is identical to the
111 second element of the list produced by calling fileparse() with the same
112 input file specification. (Under VMS, if there is no directory information
113 in the input file specification, then the current default device and
114 directory are returned.) When using Unix or MSDOS syntax, the return
115 value conforms to the behavior of the Unix shell command dirname(1). This
116 is usually the same as the behavior of fileparse(), but differs in some
117 cases. For example, for the input file specification F<lib/>, fileparse()
118 considers the directory name to be F<lib/>, while dirname() considers the
119 directory name to be F<.>).
121 =back
123 =cut
126 ## use strict;
127 # A bit of juggling to insure that C<use re 'taint';> always works, since
128 # File::Basename is used during the Perl build, when the re extension may
129 # not be available.
130 BEGIN {
131 unless (eval { require re; })
132 { eval ' sub re::import { $^H |= 0x00100000; } ' }
133 import re 'taint';
138 use 5.6.0;
139 use warnings;
140 our(@ISA, @EXPORT, $VERSION, $Fileparse_fstype, $Fileparse_igncase);
141 require Exporter;
142 @ISA = qw(Exporter);
143 @EXPORT = qw(fileparse fileparse_set_fstype basename dirname);
144 $VERSION = "2.7";
147 # fileparse_set_fstype() - specify OS-based rules used in future
148 # calls to routines in this package
150 # Currently recognized values: VMS, MSDOS, MacOS, AmigaOS, os2, RISCOS
151 # Any other name uses Unix-style rules and is case-sensitive
153 sub fileparse_set_fstype {
154 my @old = ($Fileparse_fstype, $Fileparse_igncase);
155 if (@_) {
156 $Fileparse_fstype = $_[0];
157 $Fileparse_igncase = ($_[0] =~ /^(?:MacOS|VMS|AmigaOS|os2|RISCOS|MSWin32|MSDOS)/i);
159 wantarray ? @old : $old[0];
162 # fileparse() - parse file specification
164 # Version 2.4 27-Sep-1996 Charles Bailey bailey@genetics.upenn.edu
167 sub fileparse {
168 my($fullname,@suffices) = @_;
169 my($fstype,$igncase) = ($Fileparse_fstype, $Fileparse_igncase);
170 my($dirpath,$tail,$suffix,$basename);
171 my($taint) = substr($fullname,0,0); # Is $fullname tainted?
173 if ($fstype =~ /^VMS/i) {
174 if ($fullname =~ m#/#) { $fstype = '' } # We're doing Unix emulation
175 else {
176 ($dirpath,$basename) = ($fullname =~ /^(.*[:>\]])?(.*)/s);
177 $dirpath ||= ''; # should always be defined
180 if ($fstype =~ /^MS(DOS|Win32)|epoc/i) {
181 ($dirpath,$basename) = ($fullname =~ /^((?:.*[:\\\/])?)(.*)/s);
182 $dirpath .= '.\\' unless $dirpath =~ /[\\\/]\z/;
184 elsif ($fstype =~ /^MacOS/si) {
185 ($dirpath,$basename) = ($fullname =~ /^(.*:)?(.*)/s);
186 $dirpath = ':' unless $dirpath;
188 elsif ($fstype =~ /^AmigaOS/i) {
189 ($dirpath,$basename) = ($fullname =~ /(.*[:\/])?(.*)/s);
190 $dirpath = './' unless $dirpath;
192 elsif ($fstype !~ /^VMS/i) { # default to Unix
193 ($dirpath,$basename) = ($fullname =~ m#^(.*/)?(.*)#s);
194 if ($^O eq 'VMS' and $fullname =~ m:^(/[^/]+/000000(/|$))(.*):) {
195 # dev:[000000] is top of VMS tree, similar to Unix '/'
196 # so strip it off and treat the rest as "normal"
197 my $devspec = $1;
198 my $remainder = $3;
199 ($dirpath,$basename) = ($remainder =~ m#^(.*/)?(.*)#s);
200 $dirpath = $devspec.$dirpath;
202 $dirpath = './' unless $dirpath;
205 if (@suffices) {
206 $tail = '';
207 foreach $suffix (@suffices) {
208 my $pat = ($igncase ? '(?i)' : '') . "($suffix)\$";
209 if ($basename =~ s/$pat//s) {
210 $taint .= substr($suffix,0,0);
211 $tail = $1 . $tail;
216 $tail .= $taint if defined $tail; # avoid warning if $tail == undef
217 wantarray ? ($basename .= $taint, $dirpath .= $taint, $tail)
218 : $basename .= $taint;
222 # basename() - returns first element of list returned by fileparse()
224 sub basename {
225 my($name) = shift;
226 (fileparse($name, map("\Q$_\E",@_)))[0];
230 # dirname() - returns device and directory portion of file specification
231 # Behavior matches that of Unix dirname(1) exactly for Unix and MSDOS
232 # filespecs except for names ending with a separator, e.g., "/xx/yy/".
233 # This differs from the second element of the list returned
234 # by fileparse() in that the trailing '/' (Unix) or '\' (MSDOS) (and
235 # the last directory name if the filespec ends in a '/' or '\'), is lost.
237 sub dirname {
238 my($basename,$dirname) = fileparse($_[0]);
239 my($fstype) = $Fileparse_fstype;
241 if ($fstype =~ /VMS/i) {
242 if ($_[0] =~ m#/#) { $fstype = '' }
243 else { return $dirname || $ENV{DEFAULT} }
245 if ($fstype =~ /MacOS/i) {
246 if( !length($basename) && $dirname !~ /^[^:]+:\z/) {
247 $dirname =~ s/([^:]):\z/$1/s;
248 ($basename,$dirname) = fileparse $dirname;
250 $dirname .= ":" unless $dirname =~ /:\z/;
252 elsif ($fstype =~ /MSDOS/i) {
253 $dirname =~ s/([^:])[\\\/]*\z/$1/;
254 unless( length($basename) ) {
255 ($basename,$dirname) = fileparse $dirname;
256 $dirname =~ s/([^:])[\\\/]*\z/$1/;
259 elsif ($fstype =~ /MSWin32/i) {
260 $dirname =~ s/([^:])[\\\/]*\z/$1/;
261 unless( length($basename) ) {
262 ($basename,$dirname) = fileparse $dirname;
263 $dirname =~ s/([^:])[\\\/]*\z/$1/;
266 elsif ($fstype =~ /AmigaOS/i) {
267 if ( $dirname =~ /:\z/) { return $dirname }
268 chop $dirname;
269 $dirname =~ s#[^:/]+\z## unless length($basename);
271 else {
272 $dirname =~ s:(.)/*\z:$1:s;
273 unless( length($basename) ) {
274 local($File::Basename::Fileparse_fstype) = $fstype;
275 ($basename,$dirname) = fileparse $dirname;
276 $dirname =~ s:(.)/*\z:$1:s;
280 $dirname;
283 fileparse_set_fstype $^O;