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         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

DESCRIPTION

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

AUTHOR

289       Nick Ing-Simmons <nick@ing-simmons.net>
290

SEE ALSO

292       "binmode" in perlfunc, "open" in perlfunc, perlunicode, perliol, Encode
293
294
295
296perl v5.8.8                       2001-09-21                       PerlIO(3pm)
Impressum