1Encode::PerlIO(3pm) Perl Programmers Reference Guide Encode::PerlIO(3pm)
2
3
4
6 Encode::PerlIO -- a detailed document on Encode and PerlIO
7
9 It is very common to want to do encoding transformations when reading
10 or writing files, network connections, pipes etc. If Perl is config‐
11 ured to use the new 'perlio' IO system then "Encode" provides a "layer"
12 (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 charac‐
46 ters into bytes using the API above before doing writes, and to trans‐
47 form the bytes read from a handle into characters before doing "charac‐
48 ter 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
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 encoun‐
86 ters partial character. The following is what happens when PerlIO and
87 Encode tries to encode (from utf8) more than 1024 bytes and the buffer
88 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 encod‐
100 ings such as ISO-2022-JP.
101
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 char‐
116 acter 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
140 As of this writing, any encoding whose class belongs to Encode::XS and
141 Encode::Unicode works. The Encode module has a "perlio_ok" method
142 which you can use before applying PerlIO encoding to the filehandle.
143 Here is an example:
144
145 my $use_perlio = perlio_ok($enc);
146 my $layer = $use_perlio ? "<:raw" : "<:encoding($enc)";
147 open my $fh, $layer, $file or die "$file : $!";
148 while(<$fh>){
149 $_ = decode($enc, $_) unless $use_perlio;
150 # ....
151 }
152
154 Encode::Encoding, Encode::Supported, Encode::PerlIO, encoding, per‐
155 lebcdic, "open" in perlfunc, perlunicode, utf8, the Perl Unicode Mail‐
156 ing List <perl-unicode@perl.org>
157
158
159
160perl v5.8.8 2001-09-21 Encode::PerlIO(3pm)