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       :win32
202           On Win32 platforms this experimental layer uses the native "handle"
203           IO rather than the unix-like numeric file descriptor layer. Known
204           to be buggy as of Perl 5.8.2.
205
206   Custom Layers
207       It is possible to write custom layers in addition to the above builtin
208       ones, both in C/XS and Perl, as a module named "PerlIO::<layer name>".
209       Some custom layers come with the Perl distribution.
210
211       :encoding
212           Use ":encoding(ENCODING)" to transparently do character set and
213           encoding transformations, for example from Shift-JIS to Unicode.
214           Note that an ":encoding" also enables ":utf8".  See
215           PerlIO::encoding for more information.
216
217       :mmap
218           A layer which implements "reading" of files by using "mmap()" to
219           make a (whole) file appear in the process's address space, and then
220           using that as PerlIO's "buffer". This may be faster in certain
221           circumstances for large files, and may result in less physical
222           memory use when multiple processes are reading the same file.
223
224           Files which are not "mmap()"-able revert to behaving like the
225           ":perlio" layer. Writes also behave like the ":perlio" layer, as
226           "mmap()" for write needs extra house-keeping (to extend the file)
227           which negates any advantage.
228
229           The ":mmap" layer will not exist if the platform does not support
230           "mmap()".  See PerlIO::mmap for more information.
231
232       :via
233           ":via(MODULE)" allows a transformation to be applied by an
234           arbitrary Perl module, for example compression / decompression,
235           encryption / decryption.  See PerlIO::via for more information.
236
237       :scalar
238           A layer implementing "in memory" files using scalar variables,
239           automatically used in place of the platform defaults for IO when
240           opening such a handle.  As such, the scalar is expected to act like
241           a file, only containing or storing bytes.  See PerlIO::scalar for
242           more information.
243
244   Alternatives to raw
245       To get a binary stream an alternate method is to use:
246
247           open(my $fh,"<","whatever") or die "open failed: $!";
248           binmode($fh) or die "binmode failed: $!";
249
250       This has the advantage of being backward compatible with older versions
251       of Perl that did not use PerlIO or where ":raw" was buggy (as it was
252       before Perl 5.14).
253
254       To get an unbuffered stream specify an unbuffered layer (e.g. ":unix")
255       in the open call:
256
257           open(my $fh,"<:unix",$path) or die "open failed: $!";
258
259   Defaults and how to override them
260       If the platform is MS-DOS like and normally does CRLF to "\n"
261       translation for text files then the default layers are:
262
263         :unix:crlf
264
265       Otherwise if "Configure" found out how to do "fast" IO using the
266       system's stdio (not common on modern architectures), then the default
267       layers are:
268
269         :stdio
270
271       Otherwise the default layers are
272
273         :unix:perlio
274
275       Note that the "default stack" depends on the operating system and on
276       the Perl version, and both the compile-time and runtime configurations
277       of Perl.  The default can be overridden by setting the environment
278       variable PERLIO to a space or colon separated list of layers, however
279       this cannot be used to set layers that require loading modules like
280       ":encoding".
281
282       This can be used to see the effect of/bugs in the various layers e.g.
283
284         cd .../perl/t
285         PERLIO=:stdio  ./perl harness
286         PERLIO=:perlio ./perl harness
287
288       For the various values of PERLIO see "PERLIO" in perlrun.
289
290       The following table summarizes the default layers on UNIX-like and DOS-
291       like platforms and depending on the setting of $ENV{PERLIO}:
292
293        PERLIO     UNIX-like                   DOS-like
294        ------     ---------                   --------
295        unset / "" :unix:perlio / :stdio [1]   :unix:crlf
296        :stdio     :stdio                      :stdio
297        :perlio    :unix:perlio                :unix:perlio
298
299        # [1] ":stdio" if Configure found out how to do "fast stdio" (depends
300        # on the stdio implementation) and in Perl 5.8, else ":unix:perlio"
301
302   Querying the layers of filehandles
303       The following returns the names of the PerlIO layers on a filehandle.
304
305          my @layers = PerlIO::get_layers($fh); # Or FH, *FH, "FH".
306
307       The layers are returned in the order an open() or binmode() call would
308       use them, and without colons.
309
310       By default the layers from the input side of the filehandle are
311       returned; to get the output side, use the optional "output" argument:
312
313          my @layers = PerlIO::get_layers($fh, output => 1);
314
315       (Usually the layers are identical on either side of a filehandle but
316       for example with sockets there may be differences.)
317
318       There is no set_layers(), nor does get_layers() return a tied array
319       mirroring the stack, or anything fancy like that.  This is not
320       accidental or unintentional.  The PerlIO layer stack is a bit more
321       complicated than just a stack (see for example the behaviour of
322       ":raw").  You are supposed to use open() and binmode() to manipulate
323       the stack.
324
325       Implementation details follow, please close your eyes.
326
327       The arguments to layers are by default returned in parentheses after
328       the name of the layer, and certain layers (like ":utf8") are not real
329       layers but instead flags on real layers; to get all of these returned
330       separately, use the optional "details" argument:
331
332          my @layer_and_args_and_flags = PerlIO::get_layers($fh, details => 1);
333
334       The result will be up to be three times the number of layers: the first
335       element will be a name, the second element the arguments (unspecified
336       arguments will be "undef"), the third element the flags, the fourth
337       element a name again, and so forth.
338
339       You may open your eyes now.
340

AUTHOR

342       Nick Ing-Simmons <nick@ing-simmons.net>
343

SEE ALSO

345       "binmode" in perlfunc, "open" in perlfunc, perlunicode, perliol, Encode
346
347
348
349perl v5.34.0                      2021-10-18                       PerlIO(3pm)
Impressum