This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: [ID 20020422.003] Suggestion in Perl 5.6.1 installation on AIX
[perl5.git] / lib / PerlIO.pm
CommitLineData
1141d9f8
NIS
1package PerlIO;
2
c1a61b17 3our $VERSION = '1.01';
8de1277c 4
1141d9f8 5# Map layer name to package that defines it
c1a61b17 6our %alias;
1141d9f8
NIS
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
NIS
29
30=head1 NAME
31
7d3b96bb 32PerlIO - On demand loader for PerlIO layers and root of PerlIO::* name space
b3d30bf7
NIS
33
34=head1 SYNOPSIS
35
7d3b96bb
NIS
36 open($fh,">:crlf","my.txt")
37 open($fh,">:raw","his.jpg")
38
39 Shell:
40 PERLIO=perlio perl ....
b3d30bf7
NIS
41
42=head1 DESCRIPTION
43
ec28694c
JH
44When an undefined layer 'foo' is encountered in an C<open> or
45C<binmode> layer specification then C code performs the equivalent of:
b3d30bf7
NIS
46
47 use PerlIO 'foo';
48
49The perl code in PerlIO.pm then attempts to locate a layer by doing
50
51 require PerlIO::foo;
52
47bfe92f
JH
53Otherwise the C<PerlIO> package is a place holder for additional
54PerlIO related functions.
b3d30bf7 55
7d3b96bb 56The following layers are currently defined:
b3d30bf7 57
7d3b96bb
NIS
58=over 4
59
60=item unix
61
62Low level layer which calls C<read>, C<write> and C<lseek> etc.
63
64=item stdio
65
47bfe92f
JH
66Layer which calls C<fread>, C<fwrite> and C<fseek>/C<ftell> etc. Note
67that as this is "real" stdio it will ignore any layers beneath it and
7d3b96bb
NIS
68got straight to the operating system via the C library as usual.
69
70=item perlio
71
47bfe92f
JH
72This is a re-implementation of "stdio-like" buffering written as a
73PerlIO "layer". As such it will call whatever layer is below it for
74its operations.
7d3b96bb
NIS
75
76=item crlf
77
47bfe92f
JH
78A layer which does CRLF to "\n" translation distinguishing "text" and
79"binary" files in the manner of MS-DOS and similar operating systems.
7d3b96bb
NIS
80
81=item utf8
82
47bfe92f
JH
83Declares that the stream accepts perl's internal encoding of
84characters. (Which really is UTF-8 on ASCII machines, but is
85UTF-EBCDIC on EBCDIC machines.) This allows any character perl can
86represent to be read from or written to the stream. The UTF-X encoding
87is chosen to render simple text parts (i.e. non-accented letters,
88digits and common punctuation) human readable in the encoded file.
89
90Here is how to write your native data out using UTF-8 (or UTF-EBCDIC)
91and then read it back in.
92
93 open(F, ">:utf8", "data.utf");
94 print F $out;
95 close(F);
96
97 open(F, "<:utf8", "data.utf");
98 $in = <F>;
99 close(F);
7d3b96bb 100
c1a61b17
NIS
101=item bytes
102
103This is the inverse of C<:utf8> layer. It turns off the flag
104on the layer below so that data read from it is considered to
105be "octets" i.e. characters in range 0..255 only. Likewise
106on output perl will warn if a "wide" character is written
107to a such a stream.
108
7d3b96bb
NIS
109=item raw
110
47bfe92f 111A pseudo-layer which performs two functions (which is messy, but
ec28694c 112necessary to maintain compatibility with non-PerlIO builds of Perl
47bfe92f 113and their way things have been documented elsewhere).
7d3b96bb 114
47bfe92f 115Firstly it forces the file handle to be considered binary at that
c1a61b17 116point in the layer stack, i.e. it turns off any CRLF translation.
7d3b96bb 117
47bfe92f
JH
118Secondly in prevents the IO system seaching back before it in the
119layer specification. Thus:
7d3b96bb 120
47bfe92f 121 open($fh,":raw:perlio",...)
7d3b96bb 122
47bfe92f
JH
123Forces the use of C<perlio> layer even if the platform default, or
124C<use open> default is something else (such as ":encoding(iso-8859-7)")
42234700
JH
125(the C<:encoding> requires C<use Encode>) which would interfere with
126binary nature of the stream.
b3d30bf7 127
7d3b96bb
NIS
128=back
129
130=head2 Defaults and how to override them
131
ec28694c
JH
132If the platform is MS-DOS like and normally does CRLF to "\n"
133translation for text files then the default layers are :
7d3b96bb
NIS
134
135 unix crlf
136
47bfe92f
JH
137(The low level "unix" layer may be replaced by a platform specific low
138level layer.)
7d3b96bb 139
47bfe92f
JH
140Otherwise if C<Configure> found out how to do "fast" IO using system's
141stdio, then the default layers are :
7d3b96bb
NIS
142
143 unix stdio
144
145Otherwise the default layers are
146
147 unix perlio
148
149These defaults may change once perlio has been better tested and tuned.
150
47bfe92f
JH
151The default can be overridden by setting the environment variable
152PERLIO to a space separated list of layers (unix or platform low level
153layer is always pushed first).
154
7d3b96bb
NIS
155This can be used to see the effect of/bugs in the various layers e.g.
156
157 cd .../perl/t
158 PERLIO=stdio ./perl harness
159 PERLIO=perlio ./perl harness
160
161=head1 AUTHOR
162
163Nick Ing-Simmons E<lt>nick@ing-simmons.netE<gt>
164
165=head1 SEE ALSO
166
47bfe92f 167L<perlfunc/"binmode">, L<perlfunc/"open">, L<perlunicode>, L<Encode>
7d3b96bb
NIS
168
169=cut
b3d30bf7 170