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