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 classic (Mac OS 9 and earlier) used ":" as path
110 separator, but not in the same way as a generic URI. ":foo" was a
111 relative name. "foo:bar" was an absolute name. Also, path segments
112 could contain the "/" character as well as the literal "." or "..". So
113 the mapping looks like this:
114
115 Mac classic URI
116 ---------- -------------------
117 :foo:bar <==> foo/bar
118 : <==> ./
119 ::foo:bar <==> ../foo/bar
120 ::: <==> ../../
121 foo:bar <==> file:/foo/bar
122 foo:bar: <==> file:/foo/bar/
123 .. <==> %2E%2E
124 <undef> <== /
125 foo/ <== file:/foo%2F
126 ./foo.txt <== file:/.%2Ffoo.txt
127
128 Note that if you want a relative URL, you *must* begin the path with a
129 :. Any path that begins with [^:] is treated as absolute.
130
131 Example 2: The UNIX file system is easy to map, as it uses the same
132 path separator as URIs, has a single root, and segments of "." and ".."
133 have the same meaning. URIs that have the character "\0" or "/" as
134 part of any path segment can not be turned into valid UNIX file names.
135
136 UNIX URI
137 ---------- ------------------
138 foo/bar <==> foo/bar
139 /foo/bar <==> file:/foo/bar
140 /foo/bar <== file://localhost/foo/bar
141 file: ==> ./file:
142 <undef> <== file:/fo%00/bar
143 / <==> file:/
144
146 The following configuration variables influence how the class and its
147 methods behave:
148
149 %URI::file::OS_CLASS
150 This hash maps OS identifiers to implementation classes. You might
151 want to add or modify this if you want to plug in your own file
152 handler class. Normally the keys should match the $^O values in
153 use.
154
155 If there is no mapping then the "Unix" implementation is used.
156
157 $URI::file::DEFAULT_AUTHORITY
158 This determine what "authority" string to include in absolute file
159 URIs. It defaults to "". If you prefer verbose URIs you might set
160 it to be "localhost".
161
162 Setting this value to "undef" force behaviour compatible to URI
163 v1.31 and earlier. In this mode host names in UNC paths and drive
164 letters are mapped to the authority component on Windows, while we
165 produce authority-less URIs on Unix.
166
168 URI, File::Spec, perlport
169
171 Copyright 1995-1998,2004 Gisle Aas.
172
173 This library is free software; you can redistribute it and/or modify it
174 under the same terms as Perl itself.
175
176
177
178perl v5.26.3 2018-01-09 URI::file(3)