perlopentut and PerlIO documentation
[p5sagit/p5-mst-13.2.git] / lib / PerlIO.pm
CommitLineData
1141d9f8 1package PerlIO;
2
c1a61b17 3our $VERSION = '1.01';
8de1277c 4
1141d9f8 5# Map layer name to package that defines it
c1a61b17 6our %alias;
1141d9f8 7
8sub import
9{
10 my $class = shift;
11 while (@_)
12 {
13 my $layer = shift;
14 if (exists $alias{$layer})
15 {
16 $layer = $alias{$layer}
17 }
18 else
19 {
20 $layer = "${class}::$layer";
21 }
22 eval "require $layer";
23 warn $@ if $@;
24 }
25}
26
271;
28__END__
b3d30bf7 29
30=head1 NAME
31
7d3b96bb 32PerlIO - On demand loader for PerlIO layers and root of PerlIO::* name space
b3d30bf7 33
34=head1 SYNOPSIS
35
01e6739c 36 open($fh,"<:crlf", "my.txt"); # portably open a text file for reading
1cbfc93d 37
38 open($fh,"<","his.jpg"); # portably open a binary file for reading
39 binmode($fh);
7d3b96bb 40
41 Shell:
42 PERLIO=perlio perl ....
b3d30bf7 43
44=head1 DESCRIPTION
45
ec28694c 46When an undefined layer 'foo' is encountered in an C<open> or
47C<binmode> layer specification then C code performs the equivalent of:
b3d30bf7 48
49 use PerlIO 'foo';
50
51The perl code in PerlIO.pm then attempts to locate a layer by doing
52
53 require PerlIO::foo;
54
47bfe92f 55Otherwise the C<PerlIO> package is a place holder for additional
56PerlIO related functions.
b3d30bf7 57
7d3b96bb 58The following layers are currently defined:
b3d30bf7 59
7d3b96bb 60=over 4
61
62=item unix
63
64Low level layer which calls C<read>, C<write> and C<lseek> etc.
65
66=item stdio
67
47bfe92f 68Layer which calls C<fread>, C<fwrite> and C<fseek>/C<ftell> etc. Note
69that as this is "real" stdio it will ignore any layers beneath it and
7d3b96bb 70got straight to the operating system via the C library as usual.
71
72=item perlio
73
47bfe92f 74This is a re-implementation of "stdio-like" buffering written as a
75PerlIO "layer". As such it will call whatever layer is below it for
76its operations.
7d3b96bb 77
78=item crlf
79
47bfe92f 80A layer which does CRLF to "\n" translation distinguishing "text" and
81"binary" files in the manner of MS-DOS and similar operating systems.
0226bbdb 82(It currently does I<not> mimic MS-DOS as far as treating of Control-Z
83as being an end-of-file marker.)
7d3b96bb 84
85=item utf8
86
47bfe92f 87Declares that the stream accepts perl's internal encoding of
88characters. (Which really is UTF-8 on ASCII machines, but is
89UTF-EBCDIC on EBCDIC machines.) This allows any character perl can
90represent to be read from or written to the stream. The UTF-X encoding
91is chosen to render simple text parts (i.e. non-accented letters,
92digits and common punctuation) human readable in the encoded file.
93
94Here is how to write your native data out using UTF-8 (or UTF-EBCDIC)
95and then read it back in.
96
97 open(F, ">:utf8", "data.utf");
98 print F $out;
99 close(F);
100
101 open(F, "<:utf8", "data.utf");
102 $in = <F>;
103 close(F);
7d3b96bb 104
c1a61b17 105=item bytes
106
107This is the inverse of C<:utf8> layer. It turns off the flag
108on the layer below so that data read from it is considered to
109be "octets" i.e. characters in range 0..255 only. Likewise
110on output perl will warn if a "wide" character is written
111to a such a stream.
112
7d3b96bb 113=item raw
114
0226bbdb 115The C<:raw> layer is I<defined> as being identical to calling
116C<binmode($fh)> - the stream is made suitable for passing binary
117data i.e. each byte is passed as-is. The stream will still be
118buffered. Unlike earlier versions of perl C<:raw> is I<not> just the
119inverse of C<:crlf> - other layers which would affect the binary nature of
120the stream are also removed or disabled.
1cbfc93d 121
0226bbdb 122The implementation of C<:raw> is as a pseudo-layer which when "pushed"
123pops itself and then any layers which do not declare themselves as suitable
124for binary data. (Undoing :utf8 and :crlf are implemented by clearing
125flags rather than poping layers but that is an implementation detail.)
01e6739c 126
0226bbdb 127As a consequence of the fact that C<:raw> normally pops layers
128it usually only makes sense to have it as the only or first element in a
129layer specification. When used as the first element it provides
130a known base on which to build e.g.
7d3b96bb 131
0226bbdb 132 open($fh,":raw:utf8",...)
7d3b96bb 133
0226bbdb 134will construct a "binary" stream, but then enable UTF-8 translation.
b3d30bf7 135
4ec2216f 136=item pop
137
138A pseudo layer that removes the top-most layer. Gives perl code
139a way to manipulate the layer stack. Should be considered
140as experimental. Note that C<:pop> only works on real layers
141and will not undo the effects of pseudo layers like C<:utf8>.
142An example of a possible use might be:
143
144 open($fh,...)
145 ...
146 binmode($fh,":encoding(...)"); # next chunk is encoded
147 ...
148 binmode($fh,":pop"); # back to un-encocded
149
150A more elegant (and safer) interface is needed.
151
7d3b96bb 152=back
153
01e6739c 154=head2 Alternatives to raw
155
0226bbdb 156To get a binary stream an alternate method is to use:
01e6739c 157
0226bbdb 158 open($fh,"whatever")
01e6739c 159 binmode($fh);
160
0226bbdb 161this has advantage of being backward compatible with how such things have
01e6739c 162had to be coded on some platforms for years.
01e6739c 163
164To get an un-buffered stream specify an unbuffered layer (e.g. C<:unix>)
0226bbdb 165in the open call:
01e6739c 166
167 open($fh,"<:unix",$path)
168
7d3b96bb 169=head2 Defaults and how to override them
170
ec28694c 171If the platform is MS-DOS like and normally does CRLF to "\n"
172translation for text files then the default layers are :
7d3b96bb 173
174 unix crlf
175
47bfe92f 176(The low level "unix" layer may be replaced by a platform specific low
177level layer.)
7d3b96bb 178
47bfe92f 179Otherwise if C<Configure> found out how to do "fast" IO using system's
180stdio, then the default layers are :
7d3b96bb 181
182 unix stdio
183
184Otherwise the default layers are
185
186 unix perlio
187
188These defaults may change once perlio has been better tested and tuned.
189
47bfe92f 190The default can be overridden by setting the environment variable
191PERLIO to a space separated list of layers (unix or platform low level
192layer is always pushed first).
193
7d3b96bb 194This can be used to see the effect of/bugs in the various layers e.g.
195
196 cd .../perl/t
197 PERLIO=stdio ./perl harness
198 PERLIO=perlio ./perl harness
199
200=head1 AUTHOR
201
202Nick Ing-Simmons E<lt>nick@ing-simmons.netE<gt>
203
204=head1 SEE ALSO
205
80fea0d2 206L<perlfunc/"binmode">, L<perlfunc/"open">, L<perlunicode>, L<perliol>, L<Encode>
7d3b96bb 207
208=cut
b3d30bf7 209