home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 2004 July / APC0407D2.iso / workshop / apache / files / ActivePerl-5.6.1.638-MSWin32-x86.msi / _af8eb328ac3302319cf0fc5254d9c89a < prev    next >
Encoding:
Text File  |  2004-04-13  |  8.4 KB  |  274 lines

  1. package URI::file;
  2.  
  3. use strict;
  4. use vars qw(@ISA $VERSION);
  5.  
  6. require URI::_generic;
  7. @ISA = qw(URI::_generic);
  8. $VERSION = sprintf("%d.%02d", q$Revision: 4.13 $ =~ /(\d+)\.(\d+)/);
  9.  
  10. use URI::Escape qw(uri_unescape);
  11.  
  12. use URI::Escape qw(uri_unescape);
  13.  
  14. # Map from $^O values to implementation classes.  The Unix
  15. # class is the default.
  16. my %os_class = (
  17.      os2     => "OS2",
  18.      mac     => "Mac",
  19.      MacOS   => "Mac",
  20.      MSWin32 => "Win32",
  21.      win32   => "Win32",
  22.      msdos   => "FAT",
  23.      dos     => "FAT",
  24.      qnx     => "QNX",
  25. );
  26.  
  27. sub os_class
  28. {
  29.     my($OS) = shift || $^O;
  30.  
  31.     my $class = "URI::file::" . ($os_class{$OS} || "Unix");
  32.     no strict 'refs';
  33.     unless (%{"$class\::"}) {
  34.     eval "require $class";
  35.     die $@ if $@;
  36.     }
  37.     $class;
  38. }
  39.  
  40. sub path { shift->path_query(@_) }
  41. sub host { uri_unescape(shift->authority(@_)) }
  42.  
  43. sub new
  44. {
  45.     my($class, $path, $os) = @_;
  46.     os_class($os)->new($path);
  47. }
  48.  
  49. sub new_abs
  50. {
  51.     my $class = shift;
  52.     my $file = $class->new(shift);
  53.     return $file->abs($class->cwd) unless $$file =~ /^file:/;
  54.     $file;
  55. }
  56.  
  57. sub cwd
  58. {
  59.     my $class = shift;
  60.     require Cwd;
  61.     my $cwd = Cwd::cwd();
  62.     $cwd = VMS::Filespec::unixpath($cwd) if $^O eq 'VMS';
  63.     $cwd = $class->new($cwd);
  64.     $cwd .= "/" unless substr($cwd, -1, 1) eq "/";
  65.     $cwd;
  66. }
  67.  
  68. sub file
  69. {
  70.     my($self, $os) = @_;
  71.     os_class($os)->file($self);
  72. }
  73.  
  74. sub dir
  75. {
  76.     my($self, $os) = @_;
  77.     os_class($os)->dir($self);
  78. }
  79.  
  80. 1;
  81.  
  82. __END__
  83.  
  84. =head1 NAME
  85.  
  86. URI::file - URI that map to local file names
  87.  
  88. =head1 SYNOPSIS
  89.  
  90.  use URI::file;
  91.  
  92.  $u1 = URI->new("file:/foo/bar");
  93.  $u2 = URI->new("foo/bar", "file");
  94.  
  95.  $u3 = URI::file->new($path);
  96.  $u4 = URI::file->new("c:\\windows\\", "win32");
  97.  
  98.  $u1->file;
  99.  $u1->file("mac");
  100.  
  101. =head1 DESCRIPTION
  102.  
  103. The C<URI::file> class supports C<URI> objects belonging to the I<file>
  104. URI scheme.  This scheme allows us to map the conventional file names
  105. found on various computer systems to the URI name space.  An old
  106. specification of the I<file> URI scheme is found in RFC 1738.  Some
  107. older background information is also in RFC 1630. There are no newer
  108. specifications as far as I know.
  109.  
  110. If you want simply to construct I<file> URI objects from URI strings,
  111. use the normal C<URI> constructor.  If you want to construct I<file>
  112. URI objects from the actual file names used by various systems, then
  113. use one of the following C<URI::file> constructors:
  114.  
  115. =over 4
  116.  
  117. =item $u = URI::file->new( $filename, [$os] )
  118.  
  119. Maps a file name to the I<file:> URI name space, creates an URI object
  120. and returns it.  The $filename is interpreted as one belonging to the
  121. indicated operating system ($os), which defaults to the value of the
  122. $^O variable.  The $filename can be either absolute or relative, and
  123. the corresponding type of URI object for $os is returned.
  124.  
  125. =item $u = URI::file->new_abs( $filename, [$os] )
  126.  
  127. Same as URI::file->new, but will make sure that the URI returned
  128. represents an absolute file name.  If the $filename argument is
  129. relative, then the name is resolved relative to the current directory,
  130. i.e. this constructor is really the same as:
  131.  
  132.   URI::file->new($filename)->abs(URI::file->cwd);
  133.  
  134. =item $u = URI::file->cwd
  135.  
  136. Returns a I<file> URI that represents the current working directory.
  137. See L<Cwd>.
  138.  
  139. =back
  140.  
  141. The following methods are supported for I<file> URI (in addition to
  142. the common and generic methods described in L<URI>):
  143.  
  144. =over 4
  145.  
  146. =item $u->file( [$os] )
  147.  
  148. This method return a file name.  It maps from the URI name space
  149. to the file name space of the indicated operating system.
  150.  
  151. It might return C<undef> if the name can not be represented in the
  152. indicated file system.
  153.  
  154. =item $u->dir( [$os] )
  155.  
  156. Some systems use a different form for names of directories than for plain
  157. files.  Use this method if you know you want to use the name for
  158. a directory.
  159.  
  160. =back
  161.  
  162. The C<URI::file> module can be used to map generic file names to names
  163. suitable for the current system.  As such, it can work as a nice
  164. replacement for the C<File::Spec> module.  For instance the following
  165. code will translate the Unix style file name F<Foo/Bar.pm> to a name
  166. suitable for the local system.
  167.  
  168.   $file = URI::file->new("Foo/Bar.pm", "unix")->file;
  169.   die "Can't map filename Foo/Bar.pm for $^O" unless defined $file;
  170.   open(FILE, $file) || die "Can't open '$file': $!";
  171.   # do something with FILE
  172.  
  173. =head1 MAPPING NOTES
  174.  
  175. Most computer systems today have hierarchically organized file systems.
  176. Mapping the names used in these systems to the generic URI syntax
  177. allows us to work with relative file URIs that behave as they should
  178. when resolved using the generic algorithm for URIs (specified in RFC
  179. 2396).  Mapping a file name to the generic URI syntax involves mapping
  180. the path separator character to "/" and encoding of any reserved
  181. characters that appear in the path segments of the file names.  If
  182. path segments consisting of the strings "." or ".." have a
  183. different meaning than what is specified for generic URIs, then these
  184. must be encoded as well.
  185.  
  186. If the file system has device, volume or drive specifications as
  187. the root of the name space, then it makes sense to map them to the
  188. authority field of the generic URI syntax.  This makes sure that
  189. relative URI can not be resolved "above" them , i.e. generally how
  190. relative file names work in those systems.
  191.  
  192. Another common use of the authority field is to encode the host that
  193. this file name is valid on.  The host name "localhost" is special and
  194. generally have the same meaning as an missing or empty authority
  195. field.  This use will be in conflict with using it as a device
  196. specification, but can often be resolved for device specifications
  197. having characters not legal in plain host names.
  198.  
  199. File name to URI mapping in normally not one-to-one.  There are
  200. usually many URI that map to the same file name.  For instance an
  201. authority of "localhost" maps the same as a URI with a missing or empty
  202. authority.
  203.  
  204. Example 1: The Mac use ":" as path separator, but not in the same way
  205. as generic URI. ":foo" is a relative name.  "foo:bar" is an absolute
  206. name.  Also path segments can contain the "/" character as well as be
  207. literal "." or "..".  It means that we will map like this:
  208.  
  209.   Mac                   URI
  210.   ----------            -------------------
  211.   :foo:bar     <==>     foo/bar
  212.   :            <==>     ./
  213.   ::foo:bar    <==>     ../foo/bar
  214.   :::          <==>     ../../
  215.   foo:bar      <==>     file:/foo/bar
  216.   foo:bar:     <==>     file:/foo/bar/
  217.   ..           <==>     %2E%2E
  218.   <undef>      <==      /
  219.   foo/         <==      file:/foo%2F
  220.   ./foo.txt    <==      file:/.%2Ffoo.txt
  221.  
  222. Note that if you want a relative URL, you *must* begin the path with a :.  Any
  223. path that begins with [^:] will be treated as absolute.
  224.  
  225. Example 2: The Unix file system is easy to map as it use the same path
  226. separator as URIs, have a single root, and segments of "." and ".."
  227. have the same meaning.  URIs that have the character "\0" or "/" as
  228. part of any path segment can not be turned into valid Unix file names.
  229.  
  230.   Unix                  URI
  231.   ----------            ------------------
  232.   foo/bar      <==>     foo/bar
  233.   /foo/bar     <==>     file:/foo/bar
  234.   /foo/bar     <==      file://localhost/foo/bar
  235.   file:         ==>     ./file:
  236.   <undef>      <==      file:/fo%00/bar
  237.   /            <==>     file:/
  238.  
  239. =cut
  240.  
  241.  
  242. RFC 1630
  243.  
  244.    [...]
  245.  
  246.    There is clearly a danger of confusion that a link made to a local
  247.    file should be followed by someone on a different system, with
  248.    unexpected and possibly harmful results.  Therefore, the convention
  249.    is that even a "file" URL is provided with a host part.  This allows
  250.    a client on another system to know that it cannot access the file
  251.    system, or perhaps to use some other local mechanism to access the
  252.    file.
  253.  
  254.    The special value "localhost" is used in the host field to indicate
  255.    that the filename should really be used on whatever host one is.
  256.    This for example allows links to be made to files which are
  257.    distribted on many machines, or to "your unix local password file"
  258.    subject of course to consistency across the users of the data.
  259.  
  260.    A void host field is equivalent to "localhost".
  261.  
  262. =head1 SEE ALSO
  263.  
  264. L<URI>, L<File::Spec>, L<perlport>
  265.  
  266. =head1 COPYRIGHT
  267.  
  268. Copyright 1995-1998 Gisle Aas.
  269.  
  270. This library is free software; you can redistribute it and/or
  271. modify it under the same terms as Perl itself.
  272.  
  273. =cut
  274.