Debian Codegraph @

upstream/mageia-cauldron/man3pm/Encode::PerlIO.3pm

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\"
56 .\" Required to disable full justification in groff 1.23.0.
57 .if n .ds AD l
58 .\" ========================================================================
59 .\"
60 .IX Title "Encode::PerlIO 3pm"
61 .TH Encode::PerlIO 3pm 2023-12-22 "perl v5.42.0" "Perl Programmers Reference Guide"
62 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
63 .\" way too many mistakes in technical documents.
64 .if n .ad l
65 .nh
66 .SH NAME
67 Encode::PerlIO \-\- a detailed document on Encode and PerlIO
68 .SH Overview
69 .IX Header "Overview"
70 It is very common to want to do encoding transformations when
71 reading or writing files, network connections, pipes etc.
72 If Perl is configured to use the new \*(Aqperlio\*(Aq IO system then
73 \&\f(CW\*(C`Encode\*(C'\fR provides a "layer" (see PerlIO) which can transform
74 data as it is read or written.
75 .PP
76 Here is how the blind poet would modernise the encoding:
77 .PP
78 .Vb 7
79 \& use Encode;
80 \& open(my $iliad,\*(Aq<:encoding(iso\-8859\-7)\*(Aq,\*(Aqiliad.greek\*(Aq);
81 \& open(my $utf8,\*(Aq>:utf8\*(Aq,\*(Aqiliad.utf8\*(Aq);
82 \& my @epic = <$iliad>;
83 \& print $utf8 @epic;
84 \& close($utf8);
85 \& close($illiad);
86 .Ve
87 .PP
88 In addition, the new IO system can also be configured to read/write
89 UTF\-8 encoded characters (as noted above, this is efficient):
90 .PP
91 .Vb 2
92 \& open(my $fh,\*(Aq>:utf8\*(Aq,\*(Aqanything\*(Aq);
93 \& print $fh "Any \ex{0021} string \eN{SMILEY FACE}\en";
94 .Ve
95 .PP
96 Either of the above forms of "layer" specifications can be made the default
97 for a lexical scope with the \f(CW\*(C`use open ...\*(C'\fR pragma. See open.
98 .PP
99 Once a handle is open, its layers can be altered using \f(CW\*(C`binmode\*(C'\fR.
100 .PP
101 Without any such configuration, or if Perl itself is built using the
102 system\*(Aqs own IO, then write operations assume that the file handle
103 accepts only \fIbytes\fR and will \f(CW\*(C`die\*(C'\fR if a character larger than 255 is
104 written to the handle. When reading, each octet from the handle becomes
105 a byte\-in\-a\-character. Note that this default is the same behaviour
106 as bytes\-only languages (including Perl before v5.6) would have,
107 and is sufficient to handle native 8\-bit encodings e.g. iso\-8859\-1,
108 EBCDIC etc. and any legacy mechanisms for handling other encodings
109 and binary data.
110 .PP
111 In other cases, it is the program\*(Aqs responsibility to transform
112 characters into bytes using the API above before doing writes, and to
113 transform the bytes read from a handle into characters before doing
114 "character operations" (e.g. \f(CW\*(C`lc\*(C'\fR, \f(CW\*(C`/\eW+/\*(C'\fR, ...).
115 .PP
116 You can also use PerlIO to convert larger amounts of data you don\*(Aqt
117 want to bring into memory. For example, to convert between ISO\-8859\-1
118 (Latin 1) and UTF\-8 (or UTF\-EBCDIC in EBCDIC machines):
119 .PP
120 .Vb 3
121 \& open(F, "<:encoding(iso\-8859\-1)", "data.txt") or die $!;
122 \& open(G, ">:utf8", "data.utf") or die $!;
123 \& while (<F>) { print G }
124 \&
125 \& # Could also do "print G <F>" but that would pull
126 \& # the whole file into memory just to write it out again.
127 .Ve
128 .PP
129 More examples:
130 .PP
131 .Vb 3
132 \& open(my $f, "<:encoding(cp1252)")
133 \& open(my $g, ">:encoding(iso\-8859\-2)")
134 \& open(my $h, ">:encoding(latin9)") # iso\-8859\-15
135 .Ve
136 .PP
137 See also encoding for how to change the default encoding of the
138 data in your script.
139 .SH "How does it work?"
140 .IX Header "How does it work?"
141 Here is a crude diagram of how filehandle, PerlIO, and Encode
142 interact.
143 .PP
144 .Vb 3
145 \& filehandle <\-> PerlIO PerlIO <\-> scalar (read/printed)
146 \& \e /
147 \& Encode
148 .Ve
149 .PP
150 When PerlIO receives data from either direction, it fills a buffer
151 (currently with 1024 bytes) and passes the buffer to Encode.
152 Encode tries to convert the valid part and passes it back to PerlIO,
153 leaving invalid parts (usually a partial character) in the buffer.
154 PerlIO then appends more data to the buffer, calls Encode again,
155 and so on until the data stream ends.
156 .PP
157 To do so, PerlIO always calls (de|en)code methods with CHECK set to 1.
158 This ensures that the method stops at the right place when it
159 encounters partial character. The following is what happens when
160 PerlIO and Encode tries to encode (from utf8) more than 1024 bytes
161 and the buffer boundary happens to be in the middle of a character.
162 .PP
163 .Vb 5
164 \& A B C .... ~ \ex{3000} ....
165 \& 41 42 43 .... 7E e3 80 80 ....
166 \& <\- buffer \-\-\-\-\-\-\-\-\-\-\-\-\-\-\->
167 \& << encoded >>>>>>>>>>
168 \& <\- next buffer \-\-\-\-\-\-
169 .Ve
170 .PP
171 Encode converts from the beginning to \ex7E, leaving \exe3 in the buffer
172 because it is invalid (partial character).
173 .PP
174 Unfortunately, this scheme does not work well with escape\-based
175 encodings such as ISO\-2022\-JP.
176 .SH "Line Buffering"
177 .IX Header "Line Buffering"
178 Now let\*(Aqs see what happens when you try to decode from ISO\-2022\-JP and
179 the buffer ends in the middle of a character.
180 .PP
181 .Vb 5
182 \& JIS208\-ESC \ex{5f3e}
183 \& A B C .... ~ \ee $ B |DAN | ....
184 \& 41 42 43 .... 7E 1b 24 41 43 46 ....
185 \& <\- buffer \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\->
186 \& << encoded >>>>>>>>>>>>>>>>>>>>>>>
187 .Ve
188 .PP
189 As you see, the next buffer begins with \ex43. But \ex43 is \*(AqC\*(Aq in
190 ASCII, which is wrong in this case because we are now in JISX 0208
191 area so it has to convert \ex43\ex46, not \ex43. Unlike utf8 and EUC,
192 in escape\-based encodings you can\*(Aqt tell if a given octet is a whole
193 character or just part of it.
194 .PP
195 Fortunately PerlIO also supports line buffer if you tell PerlIO to use
196 one instead of fixed buffer. Since ISO\-2022\-JP is guaranteed to revert to ASCII at the end of the line, partial
197 character will never happen when line buffer is used.
198 .PP
199 To tell PerlIO to use line buffer, implement \->needs_lines method
200 for your encoding object. See Encode::Encoding for details.
201 .PP
202 Thanks to these efforts most encodings that come with Encode support
203 PerlIO but that still leaves following encodings.
204 .PP
205 .Vb 4
206 \& iso\-2022\-kr
207 \& MIME\-B
208 \& MIME\-Header
209 \& MIME\-Q
210 .Ve
211 .PP
212 Fortunately iso\-2022\-kr is hardly used (according to Jungshik) and
213 MIME\-* are very unlikely to be fed to PerlIO because they are for mail
214 headers. See Encode::MIME::Header for details.
215 .SS "How can I tell whether my encoding fully supports PerlIO ?"
216 .IX Subsection "How can I tell whether my encoding fully supports PerlIO ?"
217 As of this writing, any encoding whose class belongs to Encode::XS and
218 Encode::Unicode works. The Encode module has a \f(CW\*(C`perlio_ok\*(C'\fR method
219 which you can use before applying PerlIO encoding to the filehandle.
220 Here is an example:
221 .PP
222 .Vb 7
223 \& my $use_perlio = perlio_ok($enc);
224 \& my $layer = $use_perlio ? "<:raw" : "<:encoding($enc)";
225 \& open my $fh, $layer, $file or die "$file : $!";
226 \& while(<$fh>){
227 \& $_ = decode($enc, $_) unless $use_perlio;
228 \& # ....
229 \& }
230 .Ve
231 .SH "SEE ALSO"
232 .IX Header "SEE ALSO"
233 Encode::Encoding,
234 Encode::Supported,
235 Encode::PerlIO,
236 encoding,
237 perlebcdic,
238 "open" in perlfunc,
239 perlunicode,
240 utf8,
241 the Perl Unicode Mailing List <perl\[email protected]>