xref: /openbsd-src/gnu/usr.bin/perl/dist/PathTools/lib/File/Spec.pm (revision 3d61058aa5c692477b6d18acfbbdb653a9930ff9)
16fb12b70Safresh1package File::Spec;
26fb12b70Safresh1
36fb12b70Safresh1use strict;
46fb12b70Safresh1
5*3d61058aSafresh1# Keep $VERSION consistent in all *.pm files in this distribution, including
6*3d61058aSafresh1# Cwd.pm.
7*3d61058aSafresh1our $VERSION = '3.91';
8b8851fccSafresh1$VERSION =~ tr/_//d;
96fb12b70Safresh1
109f11ffb7Safresh1my %module = (
116fb12b70Safresh1	      MSWin32 => 'Win32',
126fb12b70Safresh1	      os2     => 'OS2',
136fb12b70Safresh1	      VMS     => 'VMS',
146fb12b70Safresh1	      NetWare => 'Win32', # Yes, File::Spec::Win32 works on NetWare.
156fb12b70Safresh1	      symbian => 'Win32', # Yes, File::Spec::Win32 works on symbian.
166fb12b70Safresh1	      dos     => 'OS2',   # Yes, File::Spec::OS2 works on DJGPP.
17b8851fccSafresh1	      cygwin  => 'Cygwin',
18b8851fccSafresh1	      amigaos => 'AmigaOS');
196fb12b70Safresh1
206fb12b70Safresh1
216fb12b70Safresh1my $module = $module{$^O} || 'Unix';
226fb12b70Safresh1
236fb12b70Safresh1require "File/Spec/$module.pm";
249f11ffb7Safresh1our @ISA = ("File::Spec::$module");
256fb12b70Safresh1
266fb12b70Safresh11;
276fb12b70Safresh1
286fb12b70Safresh1__END__
296fb12b70Safresh1
306fb12b70Safresh1=head1 NAME
316fb12b70Safresh1
326fb12b70Safresh1File::Spec - portably perform operations on file names
336fb12b70Safresh1
346fb12b70Safresh1=head1 SYNOPSIS
356fb12b70Safresh1
366fb12b70Safresh1	use File::Spec;
376fb12b70Safresh1
38e0680481Safresh1	my $x = File::Spec->catfile('a', 'b', 'c');
396fb12b70Safresh1
406fb12b70Safresh1which returns 'a/b/c' under Unix. Or:
416fb12b70Safresh1
426fb12b70Safresh1	use File::Spec::Functions;
436fb12b70Safresh1
44e0680481Safresh1	my $x = catfile('a', 'b', 'c');
456fb12b70Safresh1
466fb12b70Safresh1=head1 DESCRIPTION
476fb12b70Safresh1
486fb12b70Safresh1This module is designed to support operations commonly performed on file
496fb12b70Safresh1specifications (usually called "file names", but not to be confused with the
506fb12b70Safresh1contents of a file, or Perl's file handles), such as concatenating several
516fb12b70Safresh1directory and file names into a single path, or determining whether a path
526fb12b70Safresh1is rooted. It is based on code directly taken from MakeMaker 5.17, code
536fb12b70Safresh1written by Andreas KE<ouml>nig, Andy Dougherty, Charles Bailey, Ilya
546fb12b70Safresh1Zakharevich, Paul Schinder, and others.
556fb12b70Safresh1
566fb12b70Safresh1Since these functions are different for most operating systems, each set of
576fb12b70Safresh1OS specific routines is available in a separate module, including:
586fb12b70Safresh1
596fb12b70Safresh1	File::Spec::Unix
606fb12b70Safresh1	File::Spec::Mac
616fb12b70Safresh1	File::Spec::OS2
626fb12b70Safresh1	File::Spec::Win32
636fb12b70Safresh1	File::Spec::VMS
646fb12b70Safresh1
656fb12b70Safresh1The module appropriate for the current OS is automatically loaded by
666fb12b70Safresh1File::Spec. Since some modules (like VMS) make use of facilities available
676fb12b70Safresh1only under that OS, it may not be possible to load all modules under all
686fb12b70Safresh1operating systems.
696fb12b70Safresh1
706fb12b70Safresh1Since File::Spec is object oriented, subroutines should not be called directly,
716fb12b70Safresh1as in:
726fb12b70Safresh1
736fb12b70Safresh1	File::Spec::catfile('a','b');
746fb12b70Safresh1
756fb12b70Safresh1but rather as class methods:
766fb12b70Safresh1
776fb12b70Safresh1	File::Spec->catfile('a','b');
786fb12b70Safresh1
796fb12b70Safresh1For simple uses, L<File::Spec::Functions> provides convenient functional
806fb12b70Safresh1forms of these methods.
816fb12b70Safresh1
826fb12b70Safresh1=head1 METHODS
836fb12b70Safresh1
846fb12b70Safresh1=over 2
856fb12b70Safresh1
866fb12b70Safresh1=item canonpath
876fb12b70Safresh1X<canonpath>
886fb12b70Safresh1
896fb12b70Safresh1No physical check on the filesystem, but a logical cleanup of a
906fb12b70Safresh1path.
916fb12b70Safresh1
926fb12b70Safresh1    $cpath = File::Spec->canonpath( $path ) ;
936fb12b70Safresh1
946fb12b70Safresh1Note that this does *not* collapse F<x/../y> sections into F<y>.  This
956fb12b70Safresh1is by design.  If F</foo> on your system is a symlink to F</bar/baz>,
966fb12b70Safresh1then F</foo/../quux> is actually F</bar/quux>, not F</quux> as a naive
976fb12b70Safresh1F<../>-removal would give you.  If you want to do this kind of
986fb12b70Safresh1processing, you probably want C<Cwd>'s C<realpath()> function to
996fb12b70Safresh1actually traverse the filesystem cleaning up paths like this.
1006fb12b70Safresh1
1016fb12b70Safresh1=item catdir
1026fb12b70Safresh1X<catdir>
1036fb12b70Safresh1
1046fb12b70Safresh1Concatenate two or more directory names to form a complete path ending
1056fb12b70Safresh1with a directory. But remove the trailing slash from the resulting
1066fb12b70Safresh1string, because it doesn't look good, isn't necessary and confuses
1076fb12b70Safresh1OS/2. Of course, if this is the root directory, don't cut off the
1086fb12b70Safresh1trailing slash :-)
1096fb12b70Safresh1
1106fb12b70Safresh1    $path = File::Spec->catdir( @directories );
1116fb12b70Safresh1
1126fb12b70Safresh1=item catfile
1136fb12b70Safresh1X<catfile>
1146fb12b70Safresh1
1156fb12b70Safresh1Concatenate one or more directory names and a filename to form a
1166fb12b70Safresh1complete path ending with a filename
1176fb12b70Safresh1
1186fb12b70Safresh1    $path = File::Spec->catfile( @directories, $filename );
1196fb12b70Safresh1
1206fb12b70Safresh1=item curdir
1216fb12b70Safresh1X<curdir>
1226fb12b70Safresh1
1236fb12b70Safresh1Returns a string representation of the current directory.
1246fb12b70Safresh1
1256fb12b70Safresh1    $curdir = File::Spec->curdir();
1266fb12b70Safresh1
1276fb12b70Safresh1=item devnull
1286fb12b70Safresh1X<devnull>
1296fb12b70Safresh1
1306fb12b70Safresh1Returns a string representation of the null device.
1316fb12b70Safresh1
1326fb12b70Safresh1    $devnull = File::Spec->devnull();
1336fb12b70Safresh1
1346fb12b70Safresh1=item rootdir
1356fb12b70Safresh1X<rootdir>
1366fb12b70Safresh1
1376fb12b70Safresh1Returns a string representation of the root directory.
1386fb12b70Safresh1
1396fb12b70Safresh1    $rootdir = File::Spec->rootdir();
1406fb12b70Safresh1
1416fb12b70Safresh1=item tmpdir
1426fb12b70Safresh1X<tmpdir>
1436fb12b70Safresh1
1446fb12b70Safresh1Returns a string representation of the first writable directory from a
1456fb12b70Safresh1list of possible temporary directories.  Returns the current directory
1466fb12b70Safresh1if no writable temporary directories are found.  The list of directories
1476fb12b70Safresh1checked depends on the platform; e.g. File::Spec::Unix checks C<$ENV{TMPDIR}>
1486fb12b70Safresh1(unless taint is on) and F</tmp>.
1496fb12b70Safresh1
1506fb12b70Safresh1    $tmpdir = File::Spec->tmpdir();
1516fb12b70Safresh1
1526fb12b70Safresh1=item updir
1536fb12b70Safresh1X<updir>
1546fb12b70Safresh1
1556fb12b70Safresh1Returns a string representation of the parent directory.
1566fb12b70Safresh1
1576fb12b70Safresh1    $updir = File::Spec->updir();
1586fb12b70Safresh1
1596fb12b70Safresh1=item no_upwards
1606fb12b70Safresh1
1619f11ffb7Safresh1Given a list of files in a directory (such as from C<readdir()>),
1629f11ffb7Safresh1strip out C<'.'> and C<'..'>.
1636fb12b70Safresh1
1649f11ffb7Safresh1B<SECURITY NOTE:> This does NOT filter paths containing C<'..'>, like
1659f11ffb7Safresh1C<'../../../../etc/passwd'>, only literal matches to C<'.'> and C<'..'>.
1669f11ffb7Safresh1
1679f11ffb7Safresh1    @paths = File::Spec->no_upwards( readdir $dirhandle );
1686fb12b70Safresh1
1696fb12b70Safresh1=item case_tolerant
1706fb12b70Safresh1
1716fb12b70Safresh1Returns a true or false value indicating, respectively, that alphabetic
1726fb12b70Safresh1case is not or is significant when comparing file specifications.
1736fb12b70Safresh1Cygwin and Win32 accept an optional drive argument.
1746fb12b70Safresh1
1756fb12b70Safresh1    $is_case_tolerant = File::Spec->case_tolerant();
1766fb12b70Safresh1
1776fb12b70Safresh1=item file_name_is_absolute
1786fb12b70Safresh1
1796fb12b70Safresh1Takes as its argument a path, and returns true if it is an absolute path.
1806fb12b70Safresh1
1816fb12b70Safresh1    $is_absolute = File::Spec->file_name_is_absolute( $path );
1826fb12b70Safresh1
1836fb12b70Safresh1This does not consult the local filesystem on Unix, Win32, OS/2, or
1846fb12b70Safresh1Mac OS (Classic).  It does consult the working environment for VMS
1856fb12b70Safresh1(see L<File::Spec::VMS/file_name_is_absolute>).
1866fb12b70Safresh1
1876fb12b70Safresh1=item path
1886fb12b70Safresh1X<path>
1896fb12b70Safresh1
1906fb12b70Safresh1Takes no argument.  Returns the environment variable C<PATH> (or the local
1916fb12b70Safresh1platform's equivalent) as a list.
1926fb12b70Safresh1
1936fb12b70Safresh1    @PATH = File::Spec->path();
1946fb12b70Safresh1
1956fb12b70Safresh1=item join
1966fb12b70Safresh1X<join, path>
1976fb12b70Safresh1
1986fb12b70Safresh1join is the same as catfile.
1996fb12b70Safresh1
2006fb12b70Safresh1=item splitpath
2016fb12b70Safresh1X<splitpath> X<split, path>
2026fb12b70Safresh1
2036fb12b70Safresh1Splits a path in to volume, directory, and filename portions. On systems
2046fb12b70Safresh1with no concept of volume, returns '' for volume.
2056fb12b70Safresh1
2066fb12b70Safresh1    ($volume,$directories,$file) =
2076fb12b70Safresh1                       File::Spec->splitpath( $path );
2086fb12b70Safresh1    ($volume,$directories,$file) =
2096fb12b70Safresh1                       File::Spec->splitpath( $path, $no_file );
2106fb12b70Safresh1
2116fb12b70Safresh1For systems with no syntax differentiating filenames from directories,
2126fb12b70Safresh1assumes that the last file is a path unless C<$no_file> is true or a
2136fb12b70Safresh1trailing separator or F</.> or F</..> is present. On Unix, this means that C<$no_file>
2146fb12b70Safresh1true makes this return ( '', $path, '' ).
2156fb12b70Safresh1
2166fb12b70Safresh1The directory portion may or may not be returned with a trailing '/'.
2176fb12b70Safresh1
2186fb12b70Safresh1The results can be passed to L</catpath()> to get back a path equivalent to
2196fb12b70Safresh1(usually identical to) the original path.
2206fb12b70Safresh1
2216fb12b70Safresh1=item splitdir
2226fb12b70Safresh1X<splitdir> X<split, dir>
2236fb12b70Safresh1
2246fb12b70Safresh1The opposite of L</catdir>.
2256fb12b70Safresh1
2266fb12b70Safresh1    @dirs = File::Spec->splitdir( $directories );
2276fb12b70Safresh1
2286fb12b70Safresh1C<$directories> must be only the directory portion of the path on systems
2296fb12b70Safresh1that have the concept of a volume or that have path syntax that differentiates
2306fb12b70Safresh1files from directories.
2316fb12b70Safresh1
2326fb12b70Safresh1Unlike just splitting the directories on the separator, empty
2336fb12b70Safresh1directory names (C<''>) can be returned, because these are significant
2346fb12b70Safresh1on some OSes.
2356fb12b70Safresh1
2366fb12b70Safresh1=item catpath()
2376fb12b70Safresh1
2386fb12b70Safresh1Takes volume, directory and file portions and returns an entire path. Under
2396fb12b70Safresh1Unix, C<$volume> is ignored, and directory and file are concatenated.  A '/' is
2406fb12b70Safresh1inserted if need be.  On other OSes, C<$volume> is significant.
2416fb12b70Safresh1
2426fb12b70Safresh1    $full_path = File::Spec->catpath( $volume, $directory, $file );
2436fb12b70Safresh1
2446fb12b70Safresh1=item abs2rel
2456fb12b70Safresh1X<abs2rel> X<absolute, path> X<relative, path>
2466fb12b70Safresh1
2476fb12b70Safresh1Takes a destination path and an optional base path returns a relative path
2486fb12b70Safresh1from the base path to the destination path:
2496fb12b70Safresh1
2506fb12b70Safresh1    $rel_path = File::Spec->abs2rel( $path ) ;
2516fb12b70Safresh1    $rel_path = File::Spec->abs2rel( $path, $base ) ;
2526fb12b70Safresh1
2536fb12b70Safresh1If C<$base> is not present or '', then L<Cwd::cwd()|Cwd> is used. If C<$base> is
2546fb12b70Safresh1relative, then it is converted to absolute form using
2556fb12b70Safresh1L</rel2abs()>. This means that it is taken to be relative to
2566fb12b70Safresh1L<Cwd::cwd()|Cwd>.
2576fb12b70Safresh1
2586fb12b70Safresh1On systems with the concept of volume, if C<$path> and C<$base> appear to be
2596fb12b70Safresh1on two different volumes, we will not attempt to resolve the two
2606fb12b70Safresh1paths, and we will instead simply return C<$path>.  Note that previous
2616fb12b70Safresh1versions of this module ignored the volume of C<$base>, which resulted in
2626fb12b70Safresh1garbage results part of the time.
2636fb12b70Safresh1
2646fb12b70Safresh1On systems that have a grammar that indicates filenames, this ignores the
2656fb12b70Safresh1C<$base> filename as well. Otherwise all path components are assumed to be
2666fb12b70Safresh1directories.
2676fb12b70Safresh1
2686fb12b70Safresh1If C<$path> is relative, it is converted to absolute form using L</rel2abs()>.
2696fb12b70Safresh1This means that it is taken to be relative to L<Cwd::cwd()|Cwd>.
2706fb12b70Safresh1
2716fb12b70Safresh1No checks against the filesystem are made.  On VMS, there is
2726fb12b70Safresh1interaction with the working environment, as logicals and
2736fb12b70Safresh1macros are expanded.
2746fb12b70Safresh1
2756fb12b70Safresh1Based on code written by Shigio Yamaguchi.
2766fb12b70Safresh1
2776fb12b70Safresh1=item rel2abs()
2786fb12b70Safresh1X<rel2abs> X<absolute, path> X<relative, path>
2796fb12b70Safresh1
2806fb12b70Safresh1Converts a relative path to an absolute path.
2816fb12b70Safresh1
2826fb12b70Safresh1    $abs_path = File::Spec->rel2abs( $path ) ;
2836fb12b70Safresh1    $abs_path = File::Spec->rel2abs( $path, $base ) ;
2846fb12b70Safresh1
2856fb12b70Safresh1If C<$base> is not present or '', then L<Cwd::cwd()|Cwd> is used. If C<$base> is relative,
2866fb12b70Safresh1then it is converted to absolute form using L</rel2abs()>. This means that it
2876fb12b70Safresh1is taken to be relative to L<Cwd::cwd()|Cwd>.
2886fb12b70Safresh1
2896fb12b70Safresh1On systems with the concept of volume, if C<$path> and C<$base> appear to be
2906fb12b70Safresh1on two different volumes, we will not attempt to resolve the two
2916fb12b70Safresh1paths, and we will instead simply return C<$path>.  Note that previous
2926fb12b70Safresh1versions of this module ignored the volume of C<$base>, which resulted in
2936fb12b70Safresh1garbage results part of the time.
2946fb12b70Safresh1
2956fb12b70Safresh1On systems that have a grammar that indicates filenames, this ignores the
2966fb12b70Safresh1C<$base> filename as well. Otherwise all path components are assumed to be
2976fb12b70Safresh1directories.
2986fb12b70Safresh1
2996fb12b70Safresh1If C<$path> is absolute, it is cleaned up and returned using L</canonpath>.
3006fb12b70Safresh1
3016fb12b70Safresh1No checks against the filesystem are made.  On VMS, there is
3026fb12b70Safresh1interaction with the working environment, as logicals and
3036fb12b70Safresh1macros are expanded.
3046fb12b70Safresh1
3056fb12b70Safresh1Based on code written by Shigio Yamaguchi.
3066fb12b70Safresh1
3076fb12b70Safresh1=back
3086fb12b70Safresh1
3096fb12b70Safresh1For further information, please see L<File::Spec::Unix>,
3106fb12b70Safresh1L<File::Spec::Mac>, L<File::Spec::OS2>, L<File::Spec::Win32>, or
3116fb12b70Safresh1L<File::Spec::VMS>.
3126fb12b70Safresh1
3136fb12b70Safresh1=head1 SEE ALSO
3146fb12b70Safresh1
3156fb12b70Safresh1L<File::Spec::Unix>, L<File::Spec::Mac>, L<File::Spec::OS2>,
3166fb12b70Safresh1L<File::Spec::Win32>, L<File::Spec::VMS>, L<File::Spec::Functions>,
3176fb12b70Safresh1L<ExtUtils::MakeMaker>
3186fb12b70Safresh1
3196fb12b70Safresh1=head1 AUTHOR
3206fb12b70Safresh1
321eac174f2Safresh1Maintained by perl5-porters <F<perl5-porters@perl.org>>.
3226fb12b70Safresh1
3236fb12b70Safresh1The vast majority of the code was written by
3246fb12b70Safresh1Kenneth Albanowski C<< <kjahds@kjahds.com> >>,
3256fb12b70Safresh1Andy Dougherty C<< <doughera@lafayette.edu> >>,
3266fb12b70Safresh1Andreas KE<ouml>nig C<< <A.Koenig@franz.ww.TU-Berlin.DE> >>,
3276fb12b70Safresh1Tim Bunce C<< <Tim.Bunce@ig.co.uk> >>.
3286fb12b70Safresh1VMS support by Charles Bailey C<< <bailey@newman.upenn.edu> >>.
3296fb12b70Safresh1OS/2 support by Ilya Zakharevich C<< <ilya@math.ohio-state.edu> >>.
3306fb12b70Safresh1Mac support by Paul Schinder C<< <schinder@pobox.com> >>, and
3316fb12b70Safresh1Thomas Wegner C<< <wegner_thomas@yahoo.com> >>.
3326fb12b70Safresh1abs2rel() and rel2abs() written by Shigio Yamaguchi C<< <shigio@tamacom.com> >>,
3336fb12b70Safresh1modified by Barrie Slaymaker C<< <barries@slaysys.com> >>.
3346fb12b70Safresh1splitpath(), splitdir(), catpath() and catdir() by Barrie Slaymaker.
3356fb12b70Safresh1
3366fb12b70Safresh1=head1 COPYRIGHT
3376fb12b70Safresh1
3386fb12b70Safresh1Copyright (c) 2004-2013 by the Perl 5 Porters.  All rights reserved.
3396fb12b70Safresh1
3406fb12b70Safresh1This program is free software; you can redistribute it and/or modify
3416fb12b70Safresh1it under the same terms as Perl itself.
3426fb12b70Safresh1
3436fb12b70Safresh1=cut
344