1URI::file(3) User Contributed Perl Documentation URI::file(3)
2
3
4
6 URI::file - URI that maps to local file names
7
9 use URI::file;
10
11 $u1 = URI->new("file:/foo/bar");
12 $u2 = URI->new("foo/bar", "file");
13
14 $u3 = URI::file->new($path);
15 $u4 = URI::file->new("c:\\windows\\", "win32");
16
17 $u1->file;
18 $u1->file("mac");
19
21 The "URI::file" class supports "URI" objects belonging to the file URI
22 scheme. This scheme allows us to map the conventional file names found
23 on various computer systems to the URI name space. An old
24 specification of the file URI scheme is found in RFC 1738. Some older
25 background information is also in RFC 1630. There are no newer
26 specifications as far as I know.
27
28 If you simply want to construct file URI objects from URI strings, use
29 the normal "URI" constructor. If you want to construct file URI
30 objects from the actual file names used by various systems, then use
31 one of the following "URI::file" constructors:
32
33 $u = URI::file->new( $filename, [$os] )
34 Maps a file name to the file: URI name space, creates a URI object
35 and returns it. The $filename is interpreted as belonging to the
36 indicated operating system ($os), which defaults to the value of
37 the $^O variable. The $filename can be either absolute or
38 relative, and the corresponding type of URI object for $os is
39 returned.
40
41 $u = URI::file->new_abs( $filename, [$os] )
42 Same as URI::file->new, but makes sure that the URI returned
43 represents an absolute file name. If the $filename argument is
44 relative, then the name is resolved relative to the current
45 directory, i.e. this constructor is really the same as:
46
47 URI::file->new($filename)->abs(URI::file->cwd);
48
49 $u = URI::file->cwd
50 Returns a file URI that represents the current working directory.
51 See Cwd.
52
53 The following methods are supported for file URI (in addition to the
54 common and generic methods described in URI):
55
56 $u->file( [$os] )
57 Returns a file name. It maps from the URI name space to the file
58 name space of the indicated operating system.
59
60 It might return "undef" if the name can not be represented in the
61 indicated file system.
62
63 $u->dir( [$os] )
64 Some systems use a different form for names of directories than for
65 plain files. Use this method if you know you want to use the name
66 for a directory.
67
68 The "URI::file" module can be used to map generic file names to names
69 suitable for the current system. As such, it can work as a nice
70 replacement for the "File::Spec" module. For instance, the following
71 code translates the UNIX-style file name Foo/Bar.pm to a name suitable
72 for the local system:
73
74 $file = URI::file->new("Foo/Bar.pm", "unix")->file;
75 die "Can't map filename Foo/Bar.pm for $^O" unless defined $file;
76 open(FILE, $file) || die "Can't open '$file': $!";
77 # do something with FILE
78
80 Most computer systems today have hierarchically organized file systems.
81 Mapping the names used in these systems to the generic URI syntax
82 allows us to work with relative file URIs that behave as they should
83 when resolved using the generic algorithm for URIs (specified in RFC
84 2396). Mapping a file name to the generic URI syntax involves mapping
85 the path separator character to "/" and encoding any reserved
86 characters that appear in the path segments of the file name. If path
87 segments consisting of the strings "." or ".." have a different meaning
88 than what is specified for generic URIs, then these must be encoded as
89 well.
90
91 If the file system has device, volume or drive specifications as the
92 root of the name space, then it makes sense to map them to the
93 authority field of the generic URI syntax. This makes sure that
94 relative URIs can not be resolved "above" them, i.e. generally how
95 relative file names work in those systems.
96
97 Another common use of the authority field is to encode the host on
98 which this file name is valid. The host name "localhost" is special
99 and generally has the same meaning as a missing or empty authority
100 field. This use is in conflict with using it as a device
101 specification, but can often be resolved for device specifications
102 having characters not legal in plain host names.
103
104 File name to URI mapping in normally not one-to-one. There are usually
105 many URIs that map to any given file name. For instance, an authority
106 of "localhost" maps the same as a URI with a missing or empty
107 authority.
108
109 Example 1: The Mac uses ":" as path separator, but not in the same way
110 as a generic URI. ":foo" is a relative name. "foo:bar" is an absolute
111 name. Also, path segments can contain the "/" character as well as the
112 literal "." or "..". So the mapping looks like this:
113
114 Mac URI
115 ---------- -------------------
116 :foo:bar <==> foo/bar
117 : <==> ./
118 ::foo:bar <==> ../foo/bar
119 ::: <==> ../../
120 foo:bar <==> file:/foo/bar
121 foo:bar: <==> file:/foo/bar/
122 .. <==> %2E%2E
123 <undef> <== /
124 foo/ <== file:/foo%2F
125 ./foo.txt <== file:/.%2Ffoo.txt
126
127 Note that if you want a relative URL, you *must* begin the path with a
128 :. Any path that begins with [^:] is treated as absolute.
129
130 Example 2: The UNIX file system is easy to map, as it uses the same
131 path separator as URIs, has a single root, and segments of "." and ".."
132 have the same meaning. URIs that have the character "\0" or "/" as
133 part of any path segment can not be turned into valid UNIX file names.
134
135 UNIX URI
136 ---------- ------------------
137 foo/bar <==> foo/bar
138 /foo/bar <==> file:/foo/bar
139 /foo/bar <== file://localhost/foo/bar
140 file: ==> ./file:
141 <undef> <== file:/fo%00/bar
142 / <==> file:/
143
145 The following configuration variables influence how the class and its
146 methods behave:
147
148 %URI::file::OS_CLASS
149 This hash maps OS identifiers to implementation classes. You might
150 want to add or modify this if you want to plug in your own file
151 handler class. Normally the keys should match the $^O values in
152 use.
153
154 If there is no mapping then the "Unix" implementation is used.
155
156 $URI::file::DEFAULT_AUTHORITY
157 This determine what "authority" string to include in absolute file
158 URIs. It defaults to "". If you prefer verbose URIs you might set
159 it to be "localhost".
160
161 Setting this value to "undef" force behaviour compatible to URI
162 v1.31 and earlier. In this mode host names in UNC paths and drive
163 letters are mapped to the authority component on Windows, while we
164 produce authority-less URIs on Unix.
165
167 URI, File::Spec, perlport
168
170 Copyright 1995-1998,2004 Gisle Aas.
171
172 This library is free software; you can redistribute it and/or modify it
173 under the same terms as Perl itself.
174
175
176
177perl v5.12.0 2009-05-28 URI::file(3)