1PerlIO(3pm) Perl Programmers Reference Guide PerlIO(3pm)
2
3
4
6 PerlIO - On demand loader for PerlIO layers and root of PerlIO::* name
7 space
8
10 open($fh,"<:crlf", "my.txt"); # support platform-native and CRLF text files
11
12 open($fh,"<","his.jpg"); # portably open a binary file for reading
13 binmode($fh);
14
15 Shell:
16 PERLIO=perlio perl ....
17
19 When an undefined layer 'foo' is encountered in an "open" or "binmode"
20 layer specification then C code performs the equivalent of:
21
22 use PerlIO 'foo';
23
24 The perl code in PerlIO.pm then attempts to locate a layer by doing
25
26 require PerlIO::foo;
27
28 Otherwise the "PerlIO" package is a place holder for additional PerlIO
29 related functions.
30
31 The following layers are currently defined:
32
33 :unix
34 Lowest level layer which provides basic PerlIO operations in terms
35 of UNIX/POSIX numeric file descriptor calls (open(), read(),
36 write(), lseek(), close()).
37
38 :stdio
39 Layer which calls "fread", "fwrite" and "fseek"/"ftell" etc. Note
40 that as this is "real" stdio it will ignore any layers beneath it
41 and go straight to the operating system via the C library as usual.
42
43 :perlio
44 A from scratch implementation of buffering for PerlIO. Provides
45 fast access to the buffer for "sv_gets" which implements perl's
46 readline/<> and in general attempts to minimize data copying.
47
48 ":perlio" will insert a ":unix" layer below itself to do low level
49 IO.
50
51 :crlf
52 A layer that implements DOS/Windows like CRLF line endings. On
53 read converts pairs of CR,LF to a single "\n" newline character.
54 On write converts each "\n" to a CR,LF pair. Note that this layer
55 likes to be one of its kind: it silently ignores attempts to be
56 pushed into the layer stack more than once.
57
58 It currently does not mimic MS-DOS as far as treating of Control-Z
59 as being an end-of-file marker.
60
61 (Gory details follow) To be more exact what happens is this: after
62 pushing itself to the stack, the ":crlf" layer checks all the
63 layers below itself to find the first layer that is capable of
64 being a CRLF layer but is not yet enabled to be a CRLF layer. If
65 it finds such a layer, it enables the CRLFness of that other deeper
66 layer, and then pops itself off the stack. If not, fine, use the
67 one we just pushed.
68
69 The end result is that a ":crlf" means "please enable the first
70 CRLF layer you can find, and if you can't find one, here would be a
71 good spot to place a new one."
72
73 Based on the ":perlio" layer.
74
75 :mmap
76 A layer which implements "reading" of files by using "mmap()" to
77 make a (whole) file appear in the process's address space, and then
78 using that as PerlIO's "buffer". This may be faster in certain
79 circumstances for large files, and may result in less physical
80 memory use when multiple processes are reading the same file.
81
82 Files which are not "mmap()"-able revert to behaving like the
83 ":perlio" layer. Writes also behave like the ":perlio" layer, as
84 "mmap()" for write needs extra house-keeping (to extend the file)
85 which negates any advantage.
86
87 The ":mmap" layer will not exist if the platform does not support
88 "mmap()".
89
90 :utf8
91 Declares that the stream accepts perl's internal encoding of
92 characters. (Which really is UTF-8 on ASCII machines, but is UTF-
93 EBCDIC on EBCDIC machines.) This allows any character perl can
94 represent to be read from or written to the stream. The UTF-X
95 encoding is chosen to render simple text parts (i.e. non-accented
96 letters, digits and common punctuation) human readable in the
97 encoded file.
98
99 Here is how to write your native data out using UTF-8 (or UTF-
100 EBCDIC) and then read it back in.
101
102 open(F, ">:utf8", "data.utf");
103 print F $out;
104 close(F);
105
106 open(F, "<:utf8", "data.utf");
107 $in = <F>;
108 close(F);
109
110 Note that this layer does not validate byte sequences. For reading
111 input, using ":encoding(utf8)" instead of bare ":utf8" is strongly
112 recommended.
113
114 :bytes
115 This is the inverse of the ":utf8" layer. It turns off the flag on
116 the layer below so that data read from it is considered to be
117 "octets" i.e. characters in the range 0..255 only. Likewise on
118 output perl will warn if a "wide" character is written to a such a
119 stream.
120
121 :raw
122 The ":raw" layer is defined as being identical to calling
123 "binmode($fh)" - the stream is made suitable for passing binary
124 data, i.e. each byte is passed as-is. The stream will still be
125 buffered.
126
127 In Perl 5.6 and some books the ":raw" layer (previously sometimes
128 also referred to as a "discipline") is documented as the inverse of
129 the ":crlf" layer. That is no longer the case - other layers which
130 would alter the binary nature of the stream are also disabled. If
131 you want UNIX line endings on a platform that normally does CRLF
132 translation, but still want UTF-8 or encoding defaults, the
133 appropriate thing to do is to add ":perlio" to the PERLIO
134 environment variable.
135
136 The implementation of ":raw" is as a pseudo-layer which when
137 "pushed" pops itself and then any layers which do not declare
138 themselves as suitable for binary data. (Undoing :utf8 and :crlf
139 are implemented by clearing flags rather than popping layers but
140 that is an implementation detail.)
141
142 As a consequence of the fact that ":raw" normally pops layers, it
143 usually only makes sense to have it as the only or first element in
144 a layer specification. When used as the first element it provides
145 a known base on which to build e.g.
146
147 open($fh,":raw:utf8",...)
148
149 will construct a "binary" stream, but then enable UTF-8
150 translation.
151
152 :pop
153 A pseudo layer that removes the top-most layer. Gives perl code a
154 way to manipulate the layer stack. Should be considered as
155 experimental. Note that ":pop" only works on real layers and will
156 not undo the effects of pseudo layers like ":utf8". An example of
157 a possible use might be:
158
159 open($fh,...)
160 ...
161 binmode($fh,":encoding(...)"); # next chunk is encoded
162 ...
163 binmode($fh,":pop"); # back to un-encoded
164
165 A more elegant (and safer) interface is needed.
166
167 :win32
168 On Win32 platforms this experimental layer uses the native "handle"
169 IO rather than the unix-like numeric file descriptor layer. Known
170 to be buggy as of perl 5.8.2.
171
172 Custom Layers
173 It is possible to write custom layers in addition to the above builtin
174 ones, both in C/XS and Perl. Two such layers (and one example written
175 in Perl using the latter) come with the Perl distribution.
176
177 :encoding
178 Use ":encoding(ENCODING)" either in open() or binmode() to install
179 a layer that transparently does character set and encoding
180 transformations, for example from Shift-JIS to Unicode. Note that
181 under "stdio" an ":encoding" also enables ":utf8". See
182 PerlIO::encoding for more information.
183
184 :via
185 Use ":via(MODULE)" either in open() or binmode() to install a layer
186 that does whatever transformation (for example compression /
187 decompression, encryption / decryption) to the filehandle. See
188 PerlIO::via for more information.
189
190 Alternatives to raw
191 To get a binary stream an alternate method is to use:
192
193 open($fh,"whatever")
194 binmode($fh);
195
196 this has the advantage of being backward compatible with how such
197 things have had to be coded on some platforms for years.
198
199 To get an unbuffered stream specify an unbuffered layer (e.g. ":unix")
200 in the open call:
201
202 open($fh,"<:unix",$path)
203
204 Defaults and how to override them
205 If the platform is MS-DOS like and normally does CRLF to "\n"
206 translation for text files then the default layers are :
207
208 unix crlf
209
210 (The low level "unix" layer may be replaced by a platform specific low
211 level layer.)
212
213 Otherwise if "Configure" found out how to do "fast" IO using the
214 system's stdio, then the default layers are:
215
216 unix stdio
217
218 Otherwise the default layers are
219
220 unix perlio
221
222 These defaults may change once perlio has been better tested and tuned.
223
224 The default can be overridden by setting the environment variable
225 PERLIO to a space separated list of layers ("unix" or platform low
226 level layer is always pushed first).
227
228 This can be used to see the effect of/bugs in the various layers e.g.
229
230 cd .../perl/t
231 PERLIO=stdio ./perl harness
232 PERLIO=perlio ./perl harness
233
234 For the various values of PERLIO see "PERLIO" in perlrun.
235
236 Querying the layers of filehandles
237 The following returns the names of the PerlIO layers on a filehandle.
238
239 my @layers = PerlIO::get_layers($fh); # Or FH, *FH, "FH".
240
241 The layers are returned in the order an open() or binmode() call would
242 use them. Note that the "default stack" depends on the operating
243 system and on the Perl version, and both the compile-time and runtime
244 configurations of Perl.
245
246 The following table summarizes the default layers on UNIX-like and DOS-
247 like platforms and depending on the setting of $ENV{PERLIO}:
248
249 PERLIO UNIX-like DOS-like
250 ------ --------- --------
251 unset / "" unix perlio / stdio [1] unix crlf
252 stdio unix perlio / stdio [1] stdio
253 perlio unix perlio unix perlio
254 mmap unix mmap unix mmap
255
256 # [1] "stdio" if Configure found out how to do "fast stdio" (depends
257 # on the stdio implementation) and in Perl 5.8, otherwise "unix perlio"
258
259 By default the layers from the input side of the filehandle are
260 returned; to get the output side, use the optional "output" argument:
261
262 my @layers = PerlIO::get_layers($fh, output => 1);
263
264 (Usually the layers are identical on either side of a filehandle but
265 for example with sockets there may be differences, or if you have been
266 using the "open" pragma.)
267
268 There is no set_layers(), nor does get_layers() return a tied array
269 mirroring the stack, or anything fancy like that. This is not
270 accidental or unintentional. The PerlIO layer stack is a bit more
271 complicated than just a stack (see for example the behaviour of
272 ":raw"). You are supposed to use open() and binmode() to manipulate
273 the stack.
274
275 Implementation details follow, please close your eyes.
276
277 The arguments to layers are by default returned in parentheses after
278 the name of the layer, and certain layers (like "utf8") are not real
279 layers but instead flags on real layers; to get all of these returned
280 separately, use the optional "details" argument:
281
282 my @layer_and_args_and_flags = PerlIO::get_layers($fh, details => 1);
283
284 The result will be up to be three times the number of layers: the first
285 element will be a name, the second element the arguments (unspecified
286 arguments will be "undef"), the third element the flags, the fourth
287 element a name again, and so forth.
288
289 You may open your eyes now.
290
292 Nick Ing-Simmons <nick@ing-simmons.net>
293
295 "binmode" in perlfunc, "open" in perlfunc, perlunicode, perliol, Encode
296
297
298
299perl v5.12.4 2011-06-07 PerlIO(3pm)