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
11 # CRLF text files
12
13 open($fh, "<", "his.jpg"); # portably open a binary file for reading
14 binmode($fh);
15
16 Shell:
17 PERLIO=perlio perl ....
18
20 When an undefined layer 'foo' is encountered in an "open" or "binmode"
21 layer specification then C code performs the equivalent of:
22
23 use PerlIO 'foo';
24
25 The perl code in PerlIO.pm then attempts to locate a layer by doing
26
27 require PerlIO::foo;
28
29 Otherwise the "PerlIO" package is a place holder for additional PerlIO
30 related functions.
31
32 The following layers are currently defined:
33
34 :unix
35 Lowest level layer which provides basic PerlIO operations in terms
36 of UNIX/POSIX numeric file descriptor calls (open(), read(),
37 write(), lseek(), close()).
38
39 :stdio
40 Layer which calls "fread", "fwrite" and "fseek"/"ftell" etc. Note
41 that as this is "real" stdio it will ignore any layers beneath it
42 and go straight to the operating system via the C library as usual.
43
44 :perlio
45 A from scratch implementation of buffering for PerlIO. Provides
46 fast access to the buffer for "sv_gets" which implements perl's
47 readline/<> and in general attempts to minimize data copying.
48
49 ":perlio" will insert a ":unix" layer below itself to do low level
50 IO.
51
52 :crlf
53 A layer that implements DOS/Windows like CRLF line endings. On
54 read converts pairs of CR,LF to a single "\n" newline character.
55 On write converts each "\n" to a CR,LF pair. Note that this layer
56 will silently refuse to be pushed on top of itself.
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 Based on the ":perlio" layer.
62
63 :utf8
64 Declares that the stream accepts perl's internal encoding of
65 characters. (Which really is UTF-8 on ASCII machines, but is UTF-
66 EBCDIC on EBCDIC machines.) This allows any character perl can
67 represent to be read from or written to the stream. The UTF-X
68 encoding is chosen to render simple text parts (i.e. non-accented
69 letters, digits and common punctuation) human readable in the
70 encoded file.
71
72 (CAUTION: This layer does not validate byte sequences. For reading
73 input, you should instead use ":encoding(UTF-8)" instead of bare
74 ":utf8".)
75
76 Here is how to write your native data out using UTF-8 (or UTF-
77 EBCDIC) and then read it back in.
78
79 open(F, ">:utf8", "data.utf");
80 print F $out;
81 close(F);
82
83 open(F, "<:utf8", "data.utf");
84 $in = <F>;
85 close(F);
86
87 :bytes
88 This is the inverse of the ":utf8" layer. It turns off the flag on
89 the layer below so that data read from it is considered to be
90 "octets" i.e. characters in the range 0..255 only. Likewise on
91 output perl will warn if a "wide" character is written to a such a
92 stream.
93
94 :raw
95 The ":raw" layer is defined as being identical to calling
96 "binmode($fh)" - the stream is made suitable for passing binary
97 data, i.e. each byte is passed as-is. The stream will still be
98 buffered.
99
100 In Perl 5.6 and some books the ":raw" layer (previously sometimes
101 also referred to as a "discipline") is documented as the inverse of
102 the ":crlf" layer. That is no longer the case - other layers which
103 would alter the binary nature of the stream are also disabled. If
104 you want UNIX line endings on a platform that normally does CRLF
105 translation, but still want UTF-8 or encoding defaults, the
106 appropriate thing to do is to add ":perlio" to the PERLIO
107 environment variable.
108
109 The implementation of ":raw" is as a pseudo-layer which when
110 "pushed" pops itself and then any layers which do not declare
111 themselves as suitable for binary data. (Undoing :utf8 and :crlf
112 are implemented by clearing flags rather than popping layers but
113 that is an implementation detail.)
114
115 As a consequence of the fact that ":raw" normally pops layers, it
116 usually only makes sense to have it as the only or first element in
117 a layer specification. When used as the first element it provides
118 a known base on which to build e.g.
119
120 open($fh,":raw:utf8",...)
121
122 will construct a "binary" stream, but then enable UTF-8
123 translation.
124
125 :pop
126 A pseudo layer that removes the top-most layer. Gives perl code a
127 way to manipulate the layer stack. Note that ":pop" only works on
128 real layers and will not undo the effects of pseudo layers like
129 ":utf8". An example of 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.30.1 2019-11-29 PerlIO(3pm)