Tiny tweaks.
[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.
7d3b96bb 82
83=item utf8
84
47bfe92f 85Declares that the stream accepts perl's internal encoding of
86characters. (Which really is UTF-8 on ASCII machines, but is
87UTF-EBCDIC on EBCDIC machines.) This allows any character perl can
88represent to be read from or written to the stream. The UTF-X encoding
89is chosen to render simple text parts (i.e. non-accented letters,
90digits and common punctuation) human readable in the encoded file.
91
92Here is how to write your native data out using UTF-8 (or UTF-EBCDIC)
93and then read it back in.
94
95 open(F, ">:utf8", "data.utf");
96 print F $out;
97 close(F);
98
99 open(F, "<:utf8", "data.utf");
100 $in = <F>;
101 close(F);
7d3b96bb 102
c1a61b17 103=item bytes
104
105This is the inverse of C<:utf8> layer. It turns off the flag
106on the layer below so that data read from it is considered to
107be "octets" i.e. characters in range 0..255 only. Likewise
108on output perl will warn if a "wide" character is written
109to a such a stream.
110
7d3b96bb 111=item raw
112
01e6739c 113B<Note that the explicit use of the C<raw> layer is deprecated:>
1cbfc93d 114
01e6739c 115C<:raw> has been documented as both the opposite of C<:crlf> and
116as a way to make a stream "binary". With the new IO system those
117two are no longer equivalent. The name has also been read as meaning
118an unbuffered stream "as close to the operating system as possible".
119See below for better ways to do things.
120
121The C<:raw> layer exists to maintain compatibility with non-PerlIO builds
122of Perl and to approximate the way it has been documented and how
123it was "faked" in perl5.6. It is a pseudo-layer which performs two
124functions (which is messy).
7d3b96bb 125
47bfe92f 126Firstly it forces the file handle to be considered binary at that
7d0fcbfa 127point in the layer stack, i.e. it turns off any CRLF translation.
7d3b96bb 128
47bfe92f 129Secondly in prevents the IO system seaching back before it in the
01e6739c 130layer specification. This second effect is intended to disable other
131non-binary features of the stream.
132
133Thus:
7d3b96bb 134
47bfe92f 135 open($fh,":raw:perlio",...)
7d3b96bb 136
01e6739c 137forces the use of C<perlio> layer even if the platform default, or
47bfe92f 138C<use open> default is something else (such as ":encoding(iso-8859-7)")
42234700 139(the C<:encoding> requires C<use Encode>) which would interfere with
140binary nature of the stream.
b3d30bf7 141
7d3b96bb 142=back
143
01e6739c 144=head2 Alternatives to raw
145
146To get a binary stream the prefered method is to use:
147
148 binmode($fh);
149
150which is neatly backward compatible with how such things have
151had to be coded on some platforms for years.
152The current implementation comprehends the effects of C<:utf8> and
153C<:crlf> layers and will be extended to comprehend similar translations
154if they are defined in future releases of perl.
155
156To get an un-buffered stream specify an unbuffered layer (e.g. C<:unix>)
157the open call:
158
159 open($fh,"<:unix",$path)
160
161To get a non-CRLF translated stream on any platform start from
162the un-buffered stream and add the appropriate layer:
163
164 open($fh,"<:unix:perlio",$path)
165
166
7d3b96bb 167=head2 Defaults and how to override them
168
ec28694c 169If the platform is MS-DOS like and normally does CRLF to "\n"
170translation for text files then the default layers are :
7d3b96bb 171
172 unix crlf
173
47bfe92f 174(The low level "unix" layer may be replaced by a platform specific low
175level layer.)
7d3b96bb 176
47bfe92f 177Otherwise if C<Configure> found out how to do "fast" IO using system's
178stdio, then the default layers are :
7d3b96bb 179
180 unix stdio
181
182Otherwise the default layers are
183
184 unix perlio
185
186These defaults may change once perlio has been better tested and tuned.
187
47bfe92f 188The default can be overridden by setting the environment variable
189PERLIO to a space separated list of layers (unix or platform low level
190layer is always pushed first).
191
7d3b96bb 192This can be used to see the effect of/bugs in the various layers e.g.
193
194 cd .../perl/t
195 PERLIO=stdio ./perl harness
196 PERLIO=perlio ./perl harness
197
198=head1 AUTHOR
199
200Nick Ing-Simmons E<lt>nick@ing-simmons.netE<gt>
201
202=head1 SEE ALSO
203
47bfe92f 204L<perlfunc/"binmode">, L<perlfunc/"open">, L<perlunicode>, L<Encode>
7d3b96bb 205
206=cut
b3d30bf7 207