1PerlIO(3pm)            Perl Programmers Reference Guide            PerlIO(3pm)
2
3
4

NAME

6       PerlIO - On demand loader for PerlIO layers and root of PerlIO::* name
7       space
8

SYNOPSIS

10         # support platform-native and CRLF text files
11         open(my $fh, "<:crlf", "my.txt") or die "open failed: $!";
12
13         # append UTF-8 encoded text
14         open(my $fh, ">>:encoding(UTF-8)", "some.log")
15           or die "open failed: $!";
16
17         # portably open a binary file for reading
18         open(my $fh, "<", "his.jpg") or die "open failed: $!";
19         binmode($fh) or die "binmode failed: $!";
20
21         Shell:
22           PERLIO=:perlio perl ....
23

DESCRIPTION

25       When an undefined layer 'foo' is encountered in an "open" or "binmode"
26       layer specification then C code performs the equivalent of:
27
28         use PerlIO 'foo';
29
30       The Perl code in PerlIO.pm then attempts to locate a layer by doing
31
32         require PerlIO::foo;
33
34       Otherwise the "PerlIO" package is a place holder for additional PerlIO
35       related functions.
36
37   Layers
38       Generally speaking, PerlIO layers (previously sometimes referred to as
39       "disciplines") are an ordered stack applied to a filehandle (specified
40       as a space- or colon-separated list, conventionally written with a
41       leading colon).  Each layer performs some operation on any input or
42       output, except when bypassed such as with "sysread" or "syswrite".
43       Read operations go through the stack in the order they are set (left to
44       right), and write operations in the reverse order.
45
46       There are also layers which actually just set flags on lower layers, or
47       layers that modify the current stack but don't persist on the stack
48       themselves; these are referred to as pseudo-layers.
49
50       When opening a handle, it will be opened with any layers specified
51       explicitly in the open() call (or the platform defaults, if specified
52       as a colon with no following layers).
53
54       If layers are not explicitly specified, the handle will be opened with
55       the layers specified by the ${^OPEN} variable (usually set by using the
56       open pragma for a lexical scope, or the "-C" command-line switch or
57       "PERL_UNICODE" environment variable for the main program scope).
58
59       If layers are not specified in the open() call or "${^OPEN}" variable,
60       the handle will be opened with the default layer stack configured for
61       that architecture; see "Defaults and how to override them".
62
63       Some layers will automatically insert required lower level layers if
64       not present; for example ":perlio" will insert ":unix" below itself for
65       low level IO, and ":encoding" will insert the platform defaults for
66       buffered IO.
67
68       The "binmode" function can be called on an opened handle to push
69       additional layers onto the stack, which may also modify the existing
70       layers.  "binmode" called with no layers will remove or unset any
71       existing layers which transform the byte stream, making the handle
72       suitable for binary data.
73
74       The following layers are currently defined:
75
76       :unix
77           Lowest level layer which provides basic PerlIO operations in terms
78           of UNIX/POSIX numeric file descriptor calls (open(), read(),
79           write(), lseek(), close()).  It is used even on non-Unix
80           architectures, and most other layers operate on top of it.
81
82       :stdio
83           Layer which calls "fread", "fwrite" and "fseek"/"ftell" etc.  Note
84           that as this is "real" stdio it will ignore any layers beneath it
85           and go straight to the operating system via the C library as usual.
86           This layer implements both low level IO and buffering, but is
87           rarely used on modern architectures.
88
89       :perlio
90           A from scratch implementation of buffering for PerlIO. Provides
91           fast access to the buffer for "sv_gets" which implements Perl's
92           readline/<> and in general attempts to minimize data copying.
93
94           ":perlio" will insert a ":unix" layer below itself to do low level
95           IO.
96
97       :crlf
98           A layer that implements DOS/Windows like CRLF line endings.  On
99           read converts pairs of CR,LF to a single "\n" newline character.
100           On write converts each "\n" to a CR,LF pair.  Note that this layer
101           will silently refuse to be pushed on top of itself.
102
103           It currently does not mimic MS-DOS as far as treating of Control-Z
104           as being an end-of-file marker.
105
106           On DOS/Windows like architectures where this layer is part of the
107           defaults, it also acts like the ":perlio" layer, and removing the
108           CRLF translation (such as with ":raw") will only unset the CRLF
109           translation flag.  Since Perl 5.14, you can also apply another
110           ":crlf" layer later, such as when the CRLF translation must occur
111           after an encoding layer.  On other architectures, it is a mundane
112           CRLF translation layer and can be added and removed normally.
113
114               # translate CRLF after encoding on Perl 5.14 or newer
115               binmode $fh, ":raw:encoding(UTF-16LE):crlf"
116                 or die "binmode failed: $!";
117
118       :utf8
119           Pseudo-layer that declares that the stream accepts Perl's internal
120           upgraded encoding of characters, which is approximately UTF-8 on
121           ASCII machines, but UTF-EBCDIC on EBCDIC machines.  This allows any
122           character Perl can represent to be read from or written to the
123           stream.
124
125           This layer (which actually sets a flag on the preceding layer, and
126           is implicitly set by any ":encoding" layer) does not translate or
127           validate byte sequences.  It instead indicates that the byte stream
128           will have been arranged by other layers to be provided in Perl's
129           internal upgraded encoding, which Perl code (and correctly written
130           XS code) will interpret as decoded Unicode characters.
131
132           CAUTION: Do not use this layer to translate from UTF-8 bytes, as
133           invalid UTF-8 or binary data will result in malformed Perl strings.
134           It is unlikely to produce invalid UTF-8 when used for output,
135           though it will instead produce UTF-EBCDIC on EBCDIC systems.  The
136           ":encoding(UTF-8)" layer (hyphen is significant) is preferred as it
137           will ensure translation between valid UTF-8 bytes and valid Unicode
138           characters.
139
140       :bytes
141           This is the inverse of the ":utf8" pseudo-layer.  It turns off the
142           flag on the layer below so that data read from it is considered to
143           be Perl's internal downgraded encoding, thus interpreted as the
144           native single-byte encoding of Latin-1 or EBCDIC.  Likewise on
145           output Perl will warn if a "wide" character (a codepoint not in the
146           range 0..255) is written to a such a stream.
147
148           This is very dangerous to push on a handle using an ":encoding"
149           layer, as such a layer assumes to be working with Perl's internal
150           upgraded encoding, so you will likely get a mangled result.
151           Instead use ":raw" or ":pop" to remove encoding layers.
152
153       :raw
154           The ":raw" pseudo-layer is defined as being identical to calling
155           "binmode($fh)" - the stream is made suitable for passing binary
156           data, i.e. each byte is passed as-is. The stream will still be
157           buffered (but this was not always true before Perl 5.14).
158
159           In Perl 5.6 and some books the ":raw" layer is documented as the
160           inverse of the ":crlf" layer. That is no longer the case - other
161           layers which would alter the binary nature of the stream are also
162           disabled.  If you want UNIX line endings on a platform that
163           normally does CRLF translation, but still want UTF-8 or encoding
164           defaults, the appropriate thing to do is to add ":perlio" to the
165           PERLIO environment variable, or open the handle explicitly with
166           that layer, to replace the platform default of ":crlf".
167
168           The implementation of ":raw" is as a pseudo-layer which when
169           "pushed" pops itself and then any layers which would modify the
170           binary data stream.  (Undoing ":utf8" and ":crlf" may be
171           implemented by clearing flags rather than popping layers but that
172           is an implementation detail.)
173
174           As a consequence of the fact that ":raw" normally pops layers, it
175           usually only makes sense to have it as the only or first element in
176           a layer specification.  When used as the first element it provides
177           a known base on which to build e.g.
178
179               open(my $fh,">:raw:encoding(UTF-8)",...)
180                 or die "open failed: $!";
181
182           will construct a "binary" stream regardless of the platform
183           defaults, but then enable UTF-8 translation.
184
185       :pop
186           A pseudo-layer that removes the top-most layer. Gives Perl code a
187           way to manipulate the layer stack.  Note that ":pop" only works on
188           real layers and will not undo the effects of pseudo-layers or flags
189           like ":utf8".  An example of a possible use might be:
190
191               open(my $fh,...) or die "open failed: $!";
192               ...
193               binmode($fh,":encoding(...)") or die "binmode failed: $!";
194               # next chunk is encoded
195               ...
196               binmode($fh,":pop") or die "binmode failed: $!";
197               # back to un-encoded
198
199           A more elegant (and safer) interface is needed.
200
201   Custom Layers
202       It is possible to write custom layers in addition to the above builtin
203       ones, both in C/XS and Perl, as a module named "PerlIO::<layer name>".
204       Some custom layers come with the Perl distribution.
205
206       :encoding
207           Use ":encoding(ENCODING)" to transparently do character set and
208           encoding transformations, for example from Shift-JIS to Unicode.
209           Note that an ":encoding" also enables ":utf8".  See
210           PerlIO::encoding for more information.
211
212       :mmap
213           A layer which implements "reading" of files by using "mmap()" to
214           make a (whole) file appear in the process's address space, and then
215           using that as PerlIO's "buffer". This may be faster in certain
216           circumstances for large files, and may result in less physical
217           memory use when multiple processes are reading the same file.
218
219           Files which are not "mmap()"-able revert to behaving like the
220           ":perlio" layer. Writes also behave like the ":perlio" layer, as
221           "mmap()" for write needs extra house-keeping (to extend the file)
222           which negates any advantage.
223
224           The ":mmap" layer will not exist if the platform does not support
225           "mmap()".  See PerlIO::mmap for more information.
226
227       :via
228           ":via(MODULE)" allows a transformation to be applied by an
229           arbitrary Perl module, for example compression / decompression,
230           encryption / decryption.  See PerlIO::via for more information.
231
232       :scalar
233           A layer implementing "in memory" files using scalar variables,
234           automatically used in place of the platform defaults for IO when
235           opening such a handle.  As such, the scalar is expected to act like
236           a file, only containing or storing bytes.  See PerlIO::scalar for
237           more information.
238
239   Alternatives to raw
240       To get a binary stream an alternate method is to use:
241
242           open(my $fh,"<","whatever") or die "open failed: $!";
243           binmode($fh) or die "binmode failed: $!";
244
245       This has the advantage of being backward compatible with older versions
246       of Perl that did not use PerlIO or where ":raw" was buggy (as it was
247       before Perl 5.14).
248
249       To get an unbuffered stream specify an unbuffered layer (e.g. ":unix")
250       in the open call:
251
252           open(my $fh,"<:unix",$path) or die "open failed: $!";
253
254   Defaults and how to override them
255       If the platform is MS-DOS like and normally does CRLF to "\n"
256       translation for text files then the default layers are:
257
258         :unix:crlf
259
260       Otherwise if "Configure" found out how to do "fast" IO using the
261       system's stdio (not common on modern architectures), then the default
262       layers are:
263
264         :stdio
265
266       Otherwise the default layers are
267
268         :unix:perlio
269
270       Note that the "default stack" depends on the operating system and on
271       the Perl version, and both the compile-time and runtime configurations
272       of Perl.  The default can be overridden by setting the environment
273       variable PERLIO to a space or colon separated list of layers, however
274       this cannot be used to set layers that require loading modules like
275       ":encoding".
276
277       This can be used to see the effect of/bugs in the various layers e.g.
278
279         cd .../perl/t
280         PERLIO=:stdio  ./perl harness
281         PERLIO=:perlio ./perl harness
282
283       For the various values of PERLIO see "PERLIO" in perlrun.
284
285       The following table summarizes the default layers on UNIX-like and DOS-
286       like platforms and depending on the setting of $ENV{PERLIO}:
287
288        PERLIO     UNIX-like                   DOS-like
289        ------     ---------                   --------
290        unset / "" :unix:perlio / :stdio [1]   :unix:crlf
291        :stdio     :stdio                      :stdio
292        :perlio    :unix:perlio                :unix:perlio
293
294        # [1] ":stdio" if Configure found out how to do "fast stdio" (depends
295        # on the stdio implementation) and in Perl 5.8, else ":unix:perlio"
296
297   Querying the layers of filehandles
298       The following returns the names of the PerlIO layers on a filehandle.
299
300          my @layers = PerlIO::get_layers($fh); # Or FH, *FH, "FH".
301
302       The layers are returned in the order an open() or binmode() call would
303       use them, and without colons.
304
305       By default the layers from the input side of the filehandle are
306       returned; to get the output side, use the optional "output" argument:
307
308          my @layers = PerlIO::get_layers($fh, output => 1);
309
310       (Usually the layers are identical on either side of a filehandle but
311       for example with sockets there may be differences.)
312
313       There is no set_layers(), nor does get_layers() return a tied array
314       mirroring the stack, or anything fancy like that.  This is not
315       accidental or unintentional.  The PerlIO layer stack is a bit more
316       complicated than just a stack (see for example the behaviour of
317       ":raw").  You are supposed to use open() and binmode() to manipulate
318       the stack.
319
320       Implementation details follow, please close your eyes.
321
322       The arguments to layers are by default returned in parentheses after
323       the name of the layer, and certain layers (like ":utf8") are not real
324       layers but instead flags on real layers; to get all of these returned
325       separately, use the optional "details" argument:
326
327          my @layer_and_args_and_flags = PerlIO::get_layers($fh, details => 1);
328
329       The result will be up to be three times the number of layers: the first
330       element will be a name, the second element the arguments (unspecified
331       arguments will be "undef"), the third element the flags, the fourth
332       element a name again, and so forth.
333
334       You may open your eyes now.
335

AUTHOR

337       Nick Ing-Simmons <nick@ing-simmons.net>
338

SEE ALSO

340       "binmode" in perlfunc, "open" in perlfunc, perlunicode, perliol, Encode
341
342
343
344perl v5.36.0                      2022-08-30                       PerlIO(3pm)
Impressum