1Encode::PerlIO(3)     User Contributed Perl Documentation    Encode::PerlIO(3)
2
3
4

NAME

6       Encode::PerlIO -- a detailed document on Encode and PerlIO
7

Overview

9       It is very common to want to do encoding transformations when reading
10       or writing files, network connections, pipes etc.  If Perl is
11       configured to use the new 'perlio' IO system then "Encode" provides a
12       "layer" (see PerlIO) which can transform data as it is read or written.
13
14       Here is how the blind poet would modernise the encoding:
15
16           use Encode;
17           open(my $iliad,'<:encoding(iso-8859-7)','iliad.greek');
18           open(my $utf8,'>:utf8','iliad.utf8');
19           my @epic = <$iliad>;
20           print $utf8 @epic;
21           close($utf8);
22           close($illiad);
23
24       In addition, the new IO system can also be configured to read/write
25       UTF-8 encoded characters (as noted above, this is efficient):
26
27           open(my $fh,'>:utf8','anything');
28           print $fh "Any \x{0021} string \N{SMILEY FACE}\n";
29
30       Either of the above forms of "layer" specifications can be made the
31       default for a lexical scope with the "use open ..." pragma. See open.
32
33       Once a handle is open, its layers can be altered using "binmode".
34
35       Without any such configuration, or if Perl itself is built using the
36       system's own IO, then write operations assume that the file handle
37       accepts only bytes and will "die" if a character larger than 255 is
38       written to the handle. When reading, each octet from the handle becomes
39       a byte-in-a-character. Note that this default is the same behaviour as
40       bytes-only languages (including Perl before v5.6) would have, and is
41       sufficient to handle native 8-bit encodings e.g. iso-8859-1, EBCDIC
42       etc. and any legacy mechanisms for handling other encodings and binary
43       data.
44
45       In other cases, it is the program's responsibility to transform
46       characters into bytes using the API above before doing writes, and to
47       transform the bytes read from a handle into characters before doing
48       "character operations" (e.g. "lc", "/\W+/", ...).
49
50       You can also use PerlIO to convert larger amounts of data you don't
51       want to bring into memory.  For example, to convert between ISO-8859-1
52       (Latin 1) and UTF-8 (or UTF-EBCDIC in EBCDIC machines):
53
54           open(F, "<:encoding(iso-8859-1)", "data.txt") or die $!;
55           open(G, ">:utf8",                 "data.utf") or die $!;
56           while (<F>) { print G }
57
58           # Could also do "print G <F>" but that would pull
59           # the whole file into memory just to write it out again.
60
61       More examples:
62
63           open(my $f, "<:encoding(cp1252)")
64           open(my $g, ">:encoding(iso-8859-2)")
65           open(my $h, ">:encoding(latin9)")       # iso-8859-15
66
67       See also encoding for how to change the default encoding of the data in
68       your script.
69

How does it work?

71       Here is a crude diagram of how filehandle, PerlIO, and Encode interact.
72
73         filehandle <-> PerlIO        PerlIO <-> scalar (read/printed)
74                              \      /
75                               Encode
76
77       When PerlIO receives data from either direction, it fills a buffer
78       (currently with 1024 bytes) and passes the buffer to Encode.  Encode
79       tries to convert the valid part and passes it back to PerlIO, leaving
80       invalid parts (usually a partial character) in the buffer.  PerlIO then
81       appends more data to the buffer, calls Encode again, and so on until
82       the data stream ends.
83
84       To do so, PerlIO always calls (de|en)code methods with CHECK set to 1.
85       This ensures that the method stops at the right place when it
86       encounters partial character.  The following is what happens when
87       PerlIO and Encode tries to encode (from utf8) more than 1024 bytes and
88       the buffer boundary happens to be in the middle of a character.
89
90          A   B   C   ....   ~     \x{3000}    ....
91         41  42  43   ....  7E   e3   80   80  ....
92         <- buffer --------------->
93         << encoded >>>>>>>>>>
94                              <- next buffer ------
95
96       Encode converts from the beginning to \x7E, leaving \xe3 in the buffer
97       because it is invalid (partial character).
98
99       Unfortunately, this scheme does not work well with escape-based
100       encodings such as ISO-2022-JP.
101

Line Buffering

103       Now let's see what happens when you try to decode from ISO-2022-JP and
104       the buffer ends in the middle of a character.
105
106                     JIS208-ESC   \x{5f3e}
107          A   B   C   ....   ~   \e   $   B  |DAN | ....
108         41  42  43   ....  7E   1b  24  41  43  46 ....
109         <- buffer --------------------------->
110         << encoded >>>>>>>>>>>>>>>>>>>>>>>
111
112       As you see, the next buffer begins with \x43.  But \x43 is 'C' in
113       ASCII, which is wrong in this case because we are now in JISX 0208 area
114       so it has to convert \x43\x46, not \x43.  Unlike utf8 and EUC, in
115       escape-based encodings you can't tell if a given octet is a whole
116       character or just part of it.
117
118       Fortunately PerlIO also supports line buffer if you tell PerlIO to use
119       one instead of fixed buffer.  Since ISO-2022-JP is guaranteed to revert
120       to ASCII at the end of the line, partial character will never happen
121       when line buffer is used.
122
123       To tell PerlIO to use line buffer, implement ->needs_lines method for
124       your encoding object.  See  Encode::Encoding for details.
125
126       Thanks to these efforts most encodings that come with Encode support
127       PerlIO but that still leaves following encodings.
128
129         iso-2022-kr
130         MIME-B
131         MIME-Header
132         MIME-Q
133
134       Fortunately iso-2022-kr is hardly used (according to Jungshik) and
135       MIME-* are very unlikely to be fed to PerlIO because they are for mail
136       headers.  See Encode::MIME::Header for details.
137
138   How can I tell whether my encoding fully supports PerlIO ?
139       As of this writing, any encoding whose class belongs to Encode::XS and
140       Encode::Unicode works.  The Encode module has a "perlio_ok" method
141       which you can use before applying PerlIO encoding to the filehandle.
142       Here is an example:
143
144         my $use_perlio = perlio_ok($enc);
145         my $layer = $use_perlio ? "<:raw" : "<:encoding($enc)";
146         open my $fh, $layer, $file or die "$file : $!";
147         while(<$fh>){
148           $_ = decode($enc, $_) unless $use_perlio;
149           # ....
150         }
151

SEE ALSO

153       Encode::Encoding, Encode::Supported, Encode::PerlIO, encoding,
154       perlebcdic, "open" in perlfunc, perlunicode, utf8, the Perl Unicode
155       Mailing List <perl-unicode@perl.org>
156
157
158
159perl v5.16.3                      2011-11-11                 Encode::PerlIO(3)
Impressum