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