xref: /openbsd-src/gnu/usr.bin/perl/dist/PathTools/lib/File/Spec.pm (revision 3d61058aa5c692477b6d18acfbbdb653a9930ff9)
1package File::Spec;
2
3use strict;
4
5# Keep $VERSION consistent in all *.pm files in this distribution, including
6# Cwd.pm.
7our $VERSION = '3.91';
8$VERSION =~ tr/_//d;
9
10my %module = (
11	      MSWin32 => 'Win32',
12	      os2     => 'OS2',
13	      VMS     => 'VMS',
14	      NetWare => 'Win32', # Yes, File::Spec::Win32 works on NetWare.
15	      symbian => 'Win32', # Yes, File::Spec::Win32 works on symbian.
16	      dos     => 'OS2',   # Yes, File::Spec::OS2 works on DJGPP.
17	      cygwin  => 'Cygwin',
18	      amigaos => 'AmigaOS');
19
20
21my $module = $module{$^O} || 'Unix';
22
23require "File/Spec/$module.pm";
24our @ISA = ("File::Spec::$module");
25
261;
27
28__END__
29
30=head1 NAME
31
32File::Spec - portably perform operations on file names
33
34=head1 SYNOPSIS
35
36	use File::Spec;
37
38	my $x = File::Spec->catfile('a', 'b', 'c');
39
40which returns 'a/b/c' under Unix. Or:
41
42	use File::Spec::Functions;
43
44	my $x = catfile('a', 'b', 'c');
45
46=head1 DESCRIPTION
47
48This module is designed to support operations commonly performed on file
49specifications (usually called "file names", but not to be confused with the
50contents of a file, or Perl's file handles), such as concatenating several
51directory and file names into a single path, or determining whether a path
52is rooted. It is based on code directly taken from MakeMaker 5.17, code
53written by Andreas KE<ouml>nig, Andy Dougherty, Charles Bailey, Ilya
54Zakharevich, Paul Schinder, and others.
55
56Since these functions are different for most operating systems, each set of
57OS specific routines is available in a separate module, including:
58
59	File::Spec::Unix
60	File::Spec::Mac
61	File::Spec::OS2
62	File::Spec::Win32
63	File::Spec::VMS
64
65The module appropriate for the current OS is automatically loaded by
66File::Spec. Since some modules (like VMS) make use of facilities available
67only under that OS, it may not be possible to load all modules under all
68operating systems.
69
70Since File::Spec is object oriented, subroutines should not be called directly,
71as in:
72
73	File::Spec::catfile('a','b');
74
75but rather as class methods:
76
77	File::Spec->catfile('a','b');
78
79For simple uses, L<File::Spec::Functions> provides convenient functional
80forms of these methods.
81
82=head1 METHODS
83
84=over 2
85
86=item canonpath
87X<canonpath>
88
89No physical check on the filesystem, but a logical cleanup of a
90path.
91
92    $cpath = File::Spec->canonpath( $path ) ;
93
94Note that this does *not* collapse F<x/../y> sections into F<y>.  This
95is by design.  If F</foo> on your system is a symlink to F</bar/baz>,
96then F</foo/../quux> is actually F</bar/quux>, not F</quux> as a naive
97F<../>-removal would give you.  If you want to do this kind of
98processing, you probably want C<Cwd>'s C<realpath()> function to
99actually traverse the filesystem cleaning up paths like this.
100
101=item catdir
102X<catdir>
103
104Concatenate two or more directory names to form a complete path ending
105with a directory. But remove the trailing slash from the resulting
106string, because it doesn't look good, isn't necessary and confuses
107OS/2. Of course, if this is the root directory, don't cut off the
108trailing slash :-)
109
110    $path = File::Spec->catdir( @directories );
111
112=item catfile
113X<catfile>
114
115Concatenate one or more directory names and a filename to form a
116complete path ending with a filename
117
118    $path = File::Spec->catfile( @directories, $filename );
119
120=item curdir
121X<curdir>
122
123Returns a string representation of the current directory.
124
125    $curdir = File::Spec->curdir();
126
127=item devnull
128X<devnull>
129
130Returns a string representation of the null device.
131
132    $devnull = File::Spec->devnull();
133
134=item rootdir
135X<rootdir>
136
137Returns a string representation of the root directory.
138
139    $rootdir = File::Spec->rootdir();
140
141=item tmpdir
142X<tmpdir>
143
144Returns a string representation of the first writable directory from a
145list of possible temporary directories.  Returns the current directory
146if no writable temporary directories are found.  The list of directories
147checked depends on the platform; e.g. File::Spec::Unix checks C<$ENV{TMPDIR}>
148(unless taint is on) and F</tmp>.
149
150    $tmpdir = File::Spec->tmpdir();
151
152=item updir
153X<updir>
154
155Returns a string representation of the parent directory.
156
157    $updir = File::Spec->updir();
158
159=item no_upwards
160
161Given a list of files in a directory (such as from C<readdir()>),
162strip out C<'.'> and C<'..'>.
163
164B<SECURITY NOTE:> This does NOT filter paths containing C<'..'>, like
165C<'../../../../etc/passwd'>, only literal matches to C<'.'> and C<'..'>.
166
167    @paths = File::Spec->no_upwards( readdir $dirhandle );
168
169=item case_tolerant
170
171Returns a true or false value indicating, respectively, that alphabetic
172case is not or is significant when comparing file specifications.
173Cygwin and Win32 accept an optional drive argument.
174
175    $is_case_tolerant = File::Spec->case_tolerant();
176
177=item file_name_is_absolute
178
179Takes as its argument a path, and returns true if it is an absolute path.
180
181    $is_absolute = File::Spec->file_name_is_absolute( $path );
182
183This does not consult the local filesystem on Unix, Win32, OS/2, or
184Mac OS (Classic).  It does consult the working environment for VMS
185(see L<File::Spec::VMS/file_name_is_absolute>).
186
187=item path
188X<path>
189
190Takes no argument.  Returns the environment variable C<PATH> (or the local
191platform's equivalent) as a list.
192
193    @PATH = File::Spec->path();
194
195=item join
196X<join, path>
197
198join is the same as catfile.
199
200=item splitpath
201X<splitpath> X<split, path>
202
203Splits a path in to volume, directory, and filename portions. On systems
204with no concept of volume, returns '' for volume.
205
206    ($volume,$directories,$file) =
207                       File::Spec->splitpath( $path );
208    ($volume,$directories,$file) =
209                       File::Spec->splitpath( $path, $no_file );
210
211For systems with no syntax differentiating filenames from directories,
212assumes that the last file is a path unless C<$no_file> is true or a
213trailing separator or F</.> or F</..> is present. On Unix, this means that C<$no_file>
214true makes this return ( '', $path, '' ).
215
216The directory portion may or may not be returned with a trailing '/'.
217
218The results can be passed to L</catpath()> to get back a path equivalent to
219(usually identical to) the original path.
220
221=item splitdir
222X<splitdir> X<split, dir>
223
224The opposite of L</catdir>.
225
226    @dirs = File::Spec->splitdir( $directories );
227
228C<$directories> must be only the directory portion of the path on systems
229that have the concept of a volume or that have path syntax that differentiates
230files from directories.
231
232Unlike just splitting the directories on the separator, empty
233directory names (C<''>) can be returned, because these are significant
234on some OSes.
235
236=item catpath()
237
238Takes volume, directory and file portions and returns an entire path. Under
239Unix, C<$volume> is ignored, and directory and file are concatenated.  A '/' is
240inserted if need be.  On other OSes, C<$volume> is significant.
241
242    $full_path = File::Spec->catpath( $volume, $directory, $file );
243
244=item abs2rel
245X<abs2rel> X<absolute, path> X<relative, path>
246
247Takes a destination path and an optional base path returns a relative path
248from the base path to the destination path:
249
250    $rel_path = File::Spec->abs2rel( $path ) ;
251    $rel_path = File::Spec->abs2rel( $path, $base ) ;
252
253If C<$base> is not present or '', then L<Cwd::cwd()|Cwd> is used. If C<$base> is
254relative, then it is converted to absolute form using
255L</rel2abs()>. This means that it is taken to be relative to
256L<Cwd::cwd()|Cwd>.
257
258On systems with the concept of volume, if C<$path> and C<$base> appear to be
259on two different volumes, we will not attempt to resolve the two
260paths, and we will instead simply return C<$path>.  Note that previous
261versions of this module ignored the volume of C<$base>, which resulted in
262garbage results part of the time.
263
264On systems that have a grammar that indicates filenames, this ignores the
265C<$base> filename as well. Otherwise all path components are assumed to be
266directories.
267
268If C<$path> is relative, it is converted to absolute form using L</rel2abs()>.
269This means that it is taken to be relative to L<Cwd::cwd()|Cwd>.
270
271No checks against the filesystem are made.  On VMS, there is
272interaction with the working environment, as logicals and
273macros are expanded.
274
275Based on code written by Shigio Yamaguchi.
276
277=item rel2abs()
278X<rel2abs> X<absolute, path> X<relative, path>
279
280Converts a relative path to an absolute path.
281
282    $abs_path = File::Spec->rel2abs( $path ) ;
283    $abs_path = File::Spec->rel2abs( $path, $base ) ;
284
285If C<$base> is not present or '', then L<Cwd::cwd()|Cwd> is used. If C<$base> is relative,
286then it is converted to absolute form using L</rel2abs()>. This means that it
287is taken to be relative to L<Cwd::cwd()|Cwd>.
288
289On systems with the concept of volume, if C<$path> and C<$base> appear to be
290on two different volumes, we will not attempt to resolve the two
291paths, and we will instead simply return C<$path>.  Note that previous
292versions of this module ignored the volume of C<$base>, which resulted in
293garbage results part of the time.
294
295On systems that have a grammar that indicates filenames, this ignores the
296C<$base> filename as well. Otherwise all path components are assumed to be
297directories.
298
299If C<$path> is absolute, it is cleaned up and returned using L</canonpath>.
300
301No checks against the filesystem are made.  On VMS, there is
302interaction with the working environment, as logicals and
303macros are expanded.
304
305Based on code written by Shigio Yamaguchi.
306
307=back
308
309For further information, please see L<File::Spec::Unix>,
310L<File::Spec::Mac>, L<File::Spec::OS2>, L<File::Spec::Win32>, or
311L<File::Spec::VMS>.
312
313=head1 SEE ALSO
314
315L<File::Spec::Unix>, L<File::Spec::Mac>, L<File::Spec::OS2>,
316L<File::Spec::Win32>, L<File::Spec::VMS>, L<File::Spec::Functions>,
317L<ExtUtils::MakeMaker>
318
319=head1 AUTHOR
320
321Maintained by perl5-porters <F<perl5-porters@perl.org>>.
322
323The vast majority of the code was written by
324Kenneth Albanowski C<< <kjahds@kjahds.com> >>,
325Andy Dougherty C<< <doughera@lafayette.edu> >>,
326Andreas KE<ouml>nig C<< <A.Koenig@franz.ww.TU-Berlin.DE> >>,
327Tim Bunce C<< <Tim.Bunce@ig.co.uk> >>.
328VMS support by Charles Bailey C<< <bailey@newman.upenn.edu> >>.
329OS/2 support by Ilya Zakharevich C<< <ilya@math.ohio-state.edu> >>.
330Mac support by Paul Schinder C<< <schinder@pobox.com> >>, and
331Thomas Wegner C<< <wegner_thomas@yahoo.com> >>.
332abs2rel() and rel2abs() written by Shigio Yamaguchi C<< <shigio@tamacom.com> >>,
333modified by Barrie Slaymaker C<< <barries@slaysys.com> >>.
334splitpath(), splitdir(), catpath() and catdir() by Barrie Slaymaker.
335
336=head1 COPYRIGHT
337
338Copyright (c) 2004-2013 by the Perl 5 Porters.  All rights reserved.
339
340This program is free software; you can redistribute it and/or modify
341it under the same terms as Perl itself.
342
343=cut
344