Spec.pm 10 KB

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