Install Perl 5.8.8
[msysgit.git] / mingw / html / lib / Encode / PerlIO.html
blobd7e8c0f1590739abb659ce32456099f1422b3659
1 <?xml version="1.0" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <title>Encode::PerlIO -- a detailed document on Encode and PerlIO</title>
6 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
7 <link rev="made" href="mailto:" />
8 </head>
10 <body style="background-color: white">
11 <table border="0" width="100%" cellspacing="0" cellpadding="3">
12 <tr><td class="block" style="background-color: #cccccc" valign="middle">
13 <big><strong><span class="block">&nbsp;Encode::PerlIO -- a detailed document on Encode and PerlIO</span></strong></big>
14 </td></tr>
15 </table>
17 <p><a name="__index__"></a></p>
18 <!-- INDEX BEGIN -->
20 <ul>
22 <li><a href="#name">NAME</a></li>
23 <li><a href="#overview">Overview</a></li>
24 <li><a href="#how_does_it_work">How does it work?</a></li>
25 <li><a href="#line_buffering">Line Buffering</a></li>
26 <ul>
28 <li><a href="#how_can_i_tell_whether_my_encoding_fully_supports_perlio_">How can I tell whether my encoding fully supports PerlIO ?</a></li>
29 </ul>
31 <li><a href="#see_also">SEE ALSO</a></li>
32 </ul>
33 <!-- INDEX END -->
35 <hr />
36 <p>
37 </p>
38 <h1><a name="name">NAME</a></h1>
39 <p>Encode::PerlIO -- a detailed document on Encode and PerlIO</p>
40 <p>
41 </p>
42 <hr />
43 <h1><a name="overview">Overview</a></h1>
44 <p>It is very common to want to do encoding transformations when
45 reading or writing files, network connections, pipes etc.
46 If Perl is configured to use the new 'perlio' IO system then
47 <code>Encode</code> provides a ``layer'' (see <a href="file://C|\msysgit\mingw\html/lib/Encode/PerlIO.html">the PerlIO manpage</a>) which can transform
48 data as it is read or written.</p>
49 <p>Here is how the blind poet would modernise the encoding:</p>
50 <pre>
51 use Encode;
52 open(my $iliad,'&lt;:encoding(iso-8859-7)','iliad.greek');
53 open(my $utf8,'&gt;:utf8','iliad.utf8');
54 my @epic = &lt;$iliad&gt;;
55 print $utf8 @epic;
56 close($utf8);
57 close($illiad);</pre>
58 <p>In addition, the new IO system can also be configured to read/write
59 UTF-8 encoded characters (as noted above, this is efficient):</p>
60 <pre>
61 open(my $fh,'&gt;:utf8','anything');
62 print $fh &quot;Any \x{0021} string \N{SMILEY FACE}\n&quot;;</pre>
63 <p>Either of the above forms of ``layer'' specifications can be made the default
64 for a lexical scope with the <code>use open ...</code> pragma. See <a href="file://C|\msysgit\mingw\html/lib/open.html">the open manpage</a>.</p>
65 <p>Once a handle is open, its layers can be altered using <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_binmode"><code>binmode</code></a>.</p>
66 <p>Without any such configuration, or if Perl itself is built using the
67 system's own IO, then write operations assume that the file handle
68 accepts only <em>bytes</em> and will <code>die</code> if a character larger than 255 is
69 written to the handle. When reading, each octet from the handle becomes
70 a byte-in-a-character. Note that this default is the same behaviour
71 as bytes-only languages (including Perl before v5.6) would have,
72 and is sufficient to handle native 8-bit encodings e.g. iso-8859-1,
73 EBCDIC etc. and any legacy mechanisms for handling other encodings
74 and binary data.</p>
75 <p>In other cases, it is the program's responsibility to transform
76 characters into bytes using the API above before doing writes, and to
77 transform the bytes read from a handle into characters before doing
78 ``character operations'' (e.g. <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_lc"><code>lc</code></a>, <code>/\W+/</code>, ...).</p>
79 <p>You can also use PerlIO to convert larger amounts of data you don't
80 want to bring into memory. For example, to convert between ISO-8859-1
81 (Latin 1) and UTF-8 (or UTF-EBCDIC in EBCDIC machines):</p>
82 <pre>
83 open(F, &quot;&lt;:encoding(iso-8859-1)&quot;, &quot;data.txt&quot;) or die $!;
84 open(G, &quot;&gt;:utf8&quot;, &quot;data.utf&quot;) or die $!;
85 while (&lt;F&gt;) { print G }</pre>
86 <pre>
87 # Could also do &quot;print G &lt;F&gt;&quot; but that would pull
88 # the whole file into memory just to write it out again.</pre>
89 <p>More examples:</p>
90 <pre>
91 open(my $f, &quot;&lt;:encoding(cp1252)&quot;)
92 open(my $g, &quot;&gt;:encoding(iso-8859-2)&quot;)
93 open(my $h, &quot;&gt;:encoding(latin9)&quot;) # iso-8859-15</pre>
94 <p>See also <a href="file://C|\msysgit\mingw\html/lib/encoding.html">the encoding manpage</a> for how to change the default encoding of the
95 data in your script.</p>
96 <p>
97 </p>
98 <hr />
99 <h1><a name="how_does_it_work">How does it work?</a></h1>
100 <p>Here is a crude diagram of how filehandle, PerlIO, and Encode
101 interact.</p>
102 <pre>
103 filehandle &lt;-&gt; PerlIO PerlIO &lt;-&gt; scalar (read/printed)
105 Encode</pre>
106 <p>When PerlIO receives data from either direction, it fills a buffer
107 (currently with 1024 bytes) and passes the buffer to Encode.
108 Encode tries to convert the valid part and passes it back to PerlIO,
109 leaving invalid parts (usually a partial character) in the buffer.
110 PerlIO then appends more data to the buffer, calls Encode again,
111 and so on until the data stream ends.</p>
112 <p>To do so, PerlIO always calls (de|en)code methods with CHECK set to 1.
113 This ensures that the method stops at the right place when it
114 encounters partial character. The following is what happens when
115 PerlIO and Encode tries to encode (from utf8) more than 1024 bytes
116 and the buffer boundary happens to be in the middle of a character.</p>
117 <pre>
118 A B C .... ~ \x{3000} ....
119 41 42 43 .... 7E e3 80 80 ....
120 &lt;- buffer ---------------&gt;
121 &lt;&lt; encoded &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;
122 &lt;- next buffer ------</pre>
123 <p>Encode converts from the beginning to \x7E, leaving \xe3 in the buffer
124 because it is invalid (partial character).</p>
125 <p>Unfortunately, this scheme does not work well with escape-based
126 encodings such as ISO-2022-JP.</p>
128 </p>
129 <hr />
130 <h1><a name="line_buffering">Line Buffering</a></h1>
131 <p>Now let's see what happens when you try to decode from ISO-2022-JP and
132 the buffer ends in the middle of a character.</p>
133 <pre>
134 JIS208-ESC \x{5f3e}
135 A B C .... ~ \e $ B |DAN | ....
136 41 42 43 .... 7E 1b 24 41 43 46 ....
137 &lt;- buffer ---------------------------&gt;
138 &lt;&lt; encoded &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;</pre>
139 <p>As you see, the next buffer begins with \x43. But \x43 is 'C' in
140 ASCII, which is wrong in this case because we are now in JISX 0208
141 area so it has to convert \x43\x46, not \x43. Unlike utf8 and EUC,
142 in escape-based encodings you can't tell if a given octet is a whole
143 character or just part of it.</p>
144 <p>Fortunately PerlIO also supports line buffer if you tell PerlIO to use
145 one instead of fixed buffer. Since ISO-2022-JP is guaranteed to revert to ASCII at the end of the line, partial
146 character will never happen when line buffer is used.</p>
147 <p>To tell PerlIO to use line buffer, implement -&gt;needs_lines method
148 for your encoding object. See <a href="file://C|\msysgit\mingw\html/lib/Encode/Encoding.html">the Encode::Encoding manpage</a> for details.</p>
149 <p>Thanks to these efforts most encodings that come with Encode support
150 PerlIO but that still leaves following encodings.</p>
151 <pre>
152 iso-2022-kr
153 MIME-B
154 MIME-Header
155 MIME-Q</pre>
156 <p>Fortunately iso-2022-kr is hardly used (according to Jungshik) and
157 MIME-* are very unlikely to be fed to PerlIO because they are for mail
158 headers. See <a href="file://C|\msysgit\mingw\html/lib/Encode/MIME/Header.html">the Encode::MIME::Header manpage</a> for details.</p>
160 </p>
161 <h2><a name="how_can_i_tell_whether_my_encoding_fully_supports_perlio_">How can I tell whether my encoding fully supports PerlIO ?</a></h2>
162 <p>As of this writing, any encoding whose class belongs to Encode::XS and
163 Encode::Unicode works. The Encode module has a <code>perlio_ok</code> method
164 which you can use before applying PerlIO encoding to the filehandle.
165 Here is an example:</p>
166 <pre>
167 my $use_perlio = perlio_ok($enc);
168 my $layer = $use_perlio ? &quot;&lt;:raw&quot; : &quot;&lt;:encoding($enc)&quot;;
169 open my $fh, $layer, $file or die &quot;$file : $!&quot;;
170 while(&lt;$fh&gt;){
171 $_ = decode($enc, $_) unless $use_perlio;
172 # ....
173 }</pre>
175 </p>
176 <hr />
177 <h1><a name="see_also">SEE ALSO</a></h1>
178 <p><a href="file://C|\msysgit\mingw\html/lib/Encode/Encoding.html">the Encode::Encoding manpage</a>,
179 <a href="file://C|\msysgit\mingw\html/lib/Encode/Supported.html">the Encode::Supported manpage</a>,
180 <a href="file://C|\msysgit\mingw\html/lib/Encode/PerlIO.html">the Encode::PerlIO manpage</a>,
181 <a href="file://C|\msysgit\mingw\html/lib/encoding.html">the encoding manpage</a>,
182 <a href="file://C|\msysgit\mingw\html/pod/perlebcdic.html">the perlebcdic manpage</a>,
183 <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_open">open in the perlfunc manpage</a>,
184 <a href="file://C|\msysgit\mingw\html/pod/perlunicode.html">the perlunicode manpage</a>,
185 <a href="file://C|\msysgit\mingw\html/lib/utf8.html">the utf8 manpage</a>,
186 the Perl Unicode Mailing List &lt;<a href="mailto:perl-unicode@perl.org">perl-unicode@perl.org</a>&gt;</p>
187 <table border="0" width="100%" cellspacing="0" cellpadding="3">
188 <tr><td class="block" style="background-color: #cccccc" valign="middle">
189 <big><strong><span class="block">&nbsp;Encode::PerlIO -- a detailed document on Encode and PerlIO</span></strong></big>
190 </td></tr>
191 </table>
193 </body>
195 </html>