URI::file - URI that map to local file names
use URI::file; $u1 = URI->new("file:/foo/bar"); $u2 = URI->new("foo/bar", "file"); $u3 = URI::file->new($path); $u4 = URI::file->new("c:\\windows\\", "win32"); $u1->file; $u1->file("mac");
URI::file class support
URI objects belonging to the file URI scheme. This scheme allows us to map the conventional file names found on various computer systems to the URI name space. An old specification of the file URI scheme is found in RFC 1738. Some more old background information is also to be found in RFC 1630. Newer specifications are as far as I know missing.
If you want to simply construct file URI objects from URI strings use the normal
URI constructor. If you want to construct file URI objects from the actual file names used by various systems, then use one of the following
- $u = URI::file->new( $filename, [$os] )
Maps a file name to the file: URI name space, creates an URI object and returns it. The $filename is interpreted as one belonging to the indicated operating system; $os, which defaults to the value of the $^O variable. The $filename can be either absolute or relative, and the corresponding type of URI object is returned.
- $u = URI::file->new_abs( $filename, [$os] )
Same as URI::file->new, but will make sure that the URI returned represents an absolute file name. If the $filename argument is relative, then the name is resolved relative to the current directory, i.e. this constructor is really the same as:
- $u = URI::file->cwd
Returns a file URI that represents the current working directory. See Cwd.
The following methods are supported for file URI (in addition to the common and generic methods described in URI):
- $u->file( [$os] )
This method return a file name. It maps back from the URI name space to the file name space of the indicated operating system.
It might return undef if the name can not be represented in the indicated file system.
- $u->dir( [$os] )
Some systems use a different names for directories than for plain files. Use this method if you know you want to use the name for accessing a directory.
URI::file module can be used to map generic file names to names suitable for the current system. As such it can work as a nice replacement for the
File::Spec module. For instance the following code will translate the Unix style file name Foo/Bar.pm to some name suitable for the local system.
$file = URI::file->new("Foo/Bar.pm", "unix")->file; die "Can't map filename Foo/Bar.pm for $^O" unless defined $file; open(FILE, $file) || die "Can't open '$file': $!"; # do something with FILE
Most computer systems today have hierarchical organized file systems. Mapping the names used in these systems to the generic URI syntax allow us to work with relative file URIs that behave as they should when resolved using the generic algorithm for URIs (specified in RFC 2396). Mapping a file name to the generic URI syntax involves mapping the path separator character to "/" and encoding of any reserved character that can appear in the path segments of the file names. If path segments consisting of the strings "." or ".." have a different meaning than what is specified for generic URIs, then these must be encoded as well.
If the file system has device, volume or drive specifications as the root of the name space, then it makes sense to map them to the authority field of the generic URI syntax. This makes sure that relative URI can not be resolved "above" them , i.e. generally how relative file names work in those systems.
Another common use of the authority field is to encode the host that this file name is valid on. The host name "localhost" is special and generally have the same meaning as an missing or empty authority field. This use will be in conflict with using it as a device specification, but can often be resolved by device specifications having characters into legal in plain host names.
File name to URI mapping in normally not one-to-one. There are usually many URI that map to the same file name. For instance an authority of "localhost" maps the same as a URI with missing or empty authority.
Example 1: The Mac use ":" as path separator, but not in the same way as generic URI. ":foo" is a relative name. "foo:bar" is an absolute name. Also path segments can contain the "/" character as well as be literal "." or "..". It means that we will map like this:
Mac URI ---------- ------------------- :foo:bar <==> foo/bar : <==> ./ ::foo:bar <==> ../foo/bar ::: <==> ../../ foo:bar <==> file://foo:/bar foo:bar: <==> file://foo:/bar/ .. <==> %2E%2E <undef> <== / foo: <== file:/foo
Example 2: The Unix file system is easy to map as it use the same path separator as URIs, have a single root, and segments of "." and ".." have the same meaning. URIs that have the character "\0" or "/" as part of any path segment can not be turned into valid Unix file names.
Unix URI ---------- ------------------ foo/bar <==> foo/bar /foo/bar <==> file:/foo/bar /foo/bar <== file://localhost/foo/bar file: ==> ./file: <undef> <== file:/fo%00/bar / <==> file:/
Copyright 1995-1998 Gisle Aas.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1 POD Error
The following errors were encountered while parsing the POD:
- Around line 143:
Deleting unknown formatting code U<>