| 1 |
.\" -*- mode: troff; coding: utf-8 -*- |
| 2 |
.\" Automatically generated by Pod::Man 5.0102 (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 |
.\" |
| 57 |
.IX Title "encoding 3perl" |
| 58 |
.TH encoding 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide" |
| 59 |
.\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 60 |
.\" way too many mistakes in technical documents. |
| 61 |
.if n .ad l |
| 62 |
.nh |
| 63 |
.SH NAME |
| 64 |
encoding \- allows you to write your script in non\-ASCII and non\-UTF\-8 |
| 65 |
.SH WARNING |
| 66 |
.IX Header "WARNING" |
| 67 |
This module has been deprecated since perl v5.18. See "DESCRIPTION" and |
| 68 |
"BUGS". |
| 69 |
.SH SYNOPSIS |
| 70 |
.IX Header "SYNOPSIS" |
| 71 |
.Vb 2 |
| 72 |
\& use encoding "greek"; # Perl like Greek to you? |
| 73 |
\& use encoding "euc\-jp"; # Jperl! |
| 74 |
\& |
| 75 |
\& # or you can even do this if your shell supports your native encoding |
| 76 |
\& |
| 77 |
\& perl \-Mencoding=latin2 \-e\*(Aq...\*(Aq # Feeling centrally European? |
| 78 |
\& perl \-Mencoding=euc\-kr \-e\*(Aq...\*(Aq # Or Korean? |
| 79 |
\& |
| 80 |
\& # more control |
| 81 |
\& |
| 82 |
\& # A simple euc\-cn => utf\-8 converter |
| 83 |
\& use encoding "euc\-cn", STDOUT => "utf8"; while(<>){print}; |
| 84 |
\& |
| 85 |
\& # "no encoding;" supported |
| 86 |
\& no encoding; |
| 87 |
\& |
| 88 |
\& # an alternate way, Filter |
| 89 |
\& use encoding "euc\-jp", Filter=>1; |
| 90 |
\& # now you can use kanji identifiers \-\- in euc\-jp! |
| 91 |
\& |
| 92 |
\& # encode based on the current locale \- specialized purposes only; |
| 93 |
\& # fraught with danger!! |
| 94 |
\& use encoding \*(Aq:locale\*(Aq; |
| 95 |
.Ve |
| 96 |
.SH DESCRIPTION |
| 97 |
.IX Header "DESCRIPTION" |
| 98 |
This pragma is used to enable a Perl script to be written in encodings that |
| 99 |
aren't strictly ASCII nor UTF\-8. It translates all or portions of the Perl |
| 100 |
program script from a given encoding into UTF\-8, and changes the PerlIO layers |
| 101 |
of \f(CW\*(C`STDIN\*(C'\fR and \f(CW\*(C`STDOUT\*(C'\fR to the encoding specified. |
| 102 |
.PP |
| 103 |
This pragma dates from the days when UTF\-8\-enabled editors were uncommon. But |
| 104 |
that was long ago, and the need for it is greatly diminished. That, coupled |
| 105 |
with the fact that it doesn't work with threads, along with other problems, |
| 106 |
(see "BUGS") have led to its being deprecated. It is planned to remove this |
| 107 |
pragma in a future Perl version. New code should be written in UTF\-8, and the |
| 108 |
\&\f(CW\*(C`use utf8\*(C'\fR pragma used instead (see perluniintro and utf8 for details). |
| 109 |
Old code should be converted to UTF\-8, via something like the recipe in the |
| 110 |
"SYNOPSIS" (though this simple approach may require manual adjustments |
| 111 |
afterwards). |
| 112 |
.PP |
| 113 |
If UTF\-8 is not an option, it is recommended that one use a simple source |
| 114 |
filter, such as that provided by Filter::Encoding on CPAN or this |
| 115 |
pragma's own \f(CW\*(C`Filter\*(C'\fR option (see below). |
| 116 |
.PP |
| 117 |
The only legitimate use of this pragma is almost certainly just one per file, |
| 118 |
near the top, with file scope, as the file is likely going to only be written |
| 119 |
in one encoding. Further restrictions apply in Perls before v5.22 (see |
| 120 |
"Prior to Perl v5.22"). |
| 121 |
.PP |
| 122 |
There are two basic modes of operation (plus turning if off): |
| 123 |
.ie n .IP """use encoding [\*(Aq\fIENCNAME\fR\*(Aq] ;""" 4 |
| 124 |
.el .IP "\f(CWuse encoding [\*(Aq\fR\f(CIENCNAME\fR\f(CW\*(Aq] ;\fR" 4 |
| 125 |
.IX Item "use encoding [ENCNAME] ;" |
| 126 |
Please note: This mode of operation is no longer supported as of Perl |
| 127 |
v5.26. |
| 128 |
.Sp |
| 129 |
This is the normal operation. It translates various literals encountered in |
| 130 |
the Perl source file from the encoding \fIENCNAME\fR into UTF\-8, and similarly |
| 131 |
converts character code points. This is used when the script is a combination |
| 132 |
of ASCII (for the variable names and punctuation, \fIetc\fR), but the literal |
| 133 |
data is in the specified encoding. |
| 134 |
.Sp |
| 135 |
\&\fIENCNAME\fR is optional. If omitted, the encoding specified in the environment |
| 136 |
variable \f(CW\*(C`PERL_ENCODING\*(C'\fR is used. If this isn't |
| 137 |
set, or the resolved-to encoding is not known to \f(CW\*(C`Encode\*(C'\fR, the error |
| 138 |
\&\f(CW\*(C`Unknown encoding \*(Aq\fR\f(CIENCNAME\fR\f(CW\*(Aq\*(C'\fR will be thrown. |
| 139 |
.Sp |
| 140 |
Starting in Perl v5.8.6 (\f(CW\*(C`Encode\*(C'\fR version 2.0.1), \fIENCNAME\fR may be the |
| 141 |
name \f(CW\*(C`:locale\*(C'\fR. This is for very specialized applications, and is documented |
| 142 |
in "The \f(CW\*(C`:locale\*(C'\fR sub-pragma" below. |
| 143 |
.Sp |
| 144 |
The literals that are converted are \f(CW\*(C`q//, qq//, qr//, qw///, qx//\*(C'\fR, and |
| 145 |
starting in v5.8.1, \f(CW\*(C`tr///\*(C'\fR. Operations that do conversions include \f(CW\*(C`chr\*(C'\fR, |
| 146 |
\&\f(CW\*(C`ord\*(C'\fR, \f(CW\*(C`utf8::upgrade\*(C'\fR (but not \f(CW\*(C`utf8::downgrade\*(C'\fR), and \f(CW\*(C`chomp\*(C'\fR. |
| 147 |
.Sp |
| 148 |
Also starting in v5.8.1, the \f(CW\*(C`DATA\*(C'\fR pseudo-filehandle is translated from the |
| 149 |
encoding into UTF\-8. |
| 150 |
.Sp |
| 151 |
For example, you can write code in EUC-JP as follows: |
| 152 |
.Sp |
| 153 |
.Vb 3 |
| 154 |
\& my $Rakuda = "\exF1\exD1\exF1\exCC"; # Camel in Kanji |
| 155 |
\& #<\-char\-><\-char\-> # 4 octets |
| 156 |
\& s/\ebCamel\eb/$Rakuda/; |
| 157 |
.Ve |
| 158 |
.Sp |
| 159 |
And with \f(CW\*(C`use encoding "euc\-jp"\*(C'\fR in effect, it is the same thing as |
| 160 |
that code in UTF\-8: |
| 161 |
.Sp |
| 162 |
.Vb 2 |
| 163 |
\& my $Rakuda = "\ex{99F1}\ex{99DD}"; # two Unicode Characters |
| 164 |
\& s/\ebCamel\eb/$Rakuda/; |
| 165 |
.Ve |
| 166 |
.Sp |
| 167 |
See "EXAMPLE" below for a more complete example. |
| 168 |
.Sp |
| 169 |
Unless \f(CW\*(C`${^UNICODE}\*(C'\fR (available starting in v5.8.2) exists and is non-zero, the |
| 170 |
PerlIO layers of \f(CW\*(C`STDIN\*(C'\fR and \f(CW\*(C`STDOUT\*(C'\fR are set to "\f(CW:encoding(\fR\f(CIENCNAME\fR\f(CW)\fR". |
| 171 |
Therefore, |
| 172 |
.Sp |
| 173 |
.Vb 5 |
| 174 |
\& use encoding "euc\-jp"; |
| 175 |
\& my $message = "Camel is the symbol of perl.\en"; |
| 176 |
\& my $Rakuda = "\exF1\exD1\exF1\exCC"; # Camel in Kanji |
| 177 |
\& $message =~ s/\ebCamel\eb/$Rakuda/; |
| 178 |
\& print $message; |
| 179 |
.Ve |
| 180 |
.Sp |
| 181 |
will print |
| 182 |
.Sp |
| 183 |
.Vb 1 |
| 184 |
\& "\exF1\exD1\exF1\exCC is the symbol of perl.\en" |
| 185 |
.Ve |
| 186 |
.Sp |
| 187 |
not |
| 188 |
.Sp |
| 189 |
.Vb 1 |
| 190 |
\& "\ex{99F1}\ex{99DD} is the symbol of perl.\en" |
| 191 |
.Ve |
| 192 |
.Sp |
| 193 |
You can override this by giving extra arguments; see below. |
| 194 |
.Sp |
| 195 |
Note that \f(CW\*(C`STDERR\*(C'\fR WILL NOT be changed, regardless. |
| 196 |
.Sp |
| 197 |
Also note that non-STD file handles remain unaffected. Use \f(CW\*(C`use |
| 198 |
open\*(C'\fR or \f(CW\*(C`binmode\*(C'\fR to change the layers of those. |
| 199 |
.ie n .IP """use encoding \fIENCNAME\fR, Filter=>1;""" 4 |
| 200 |
.el .IP "\f(CWuse encoding \fR\f(CIENCNAME\fR\f(CW, Filter=>1;\fR" 4 |
| 201 |
.IX Item "use encoding ENCNAME, Filter=>1;" |
| 202 |
This operates as above, but the \f(CW\*(C`Filter\*(C'\fR argument with a non-zero |
| 203 |
value causes the entire script, and not just literals, to be translated from |
| 204 |
the encoding into UTF\-8. This allows identifiers in the source to be in that |
| 205 |
encoding as well. (Problems may occur if the encoding is not a superset of |
| 206 |
ASCII; imagine all your semi-colons being translated into something |
| 207 |
different.) One can use this form to make |
| 208 |
.Sp |
| 209 |
.Vb 1 |
| 210 |
\& ${"\ex{4eba}"}++ |
| 211 |
.Ve |
| 212 |
.Sp |
| 213 |
work. (This is equivalent to \f(CW\*(C`$\fR\f(CIhuman\fR\f(CW++\*(C'\fR, where \fIhuman\fR is a single Han |
| 214 |
ideograph). |
| 215 |
.Sp |
| 216 |
This effectively means that your source code behaves as if it were written in |
| 217 |
UTF\-8 with \f(CW\*(C`\*(Aquse utf8\*(C'\fR' in effect. So even if your editor only supports |
| 218 |
Shift_JIS, for example, you can still try examples in Chapter 15 of |
| 219 |
\&\f(CW\*(C`Programming Perl, 3rd Ed.\*(C'\fR. |
| 220 |
.Sp |
| 221 |
This option is significantly slower than the other one. |
| 222 |
.ie n .IP """no encoding;""" 4 |
| 223 |
.el .IP "\f(CWno encoding;\fR" 4 |
| 224 |
.IX Item "no encoding;" |
| 225 |
Unsets the script encoding. The layers of \f(CW\*(C`STDIN\*(C'\fR, \f(CW\*(C`STDOUT\*(C'\fR are |
| 226 |
reset to "\f(CW\*(C`:raw\*(C'\fR" (the default unprocessed raw stream of bytes). |
| 227 |
.SH OPTIONS |
| 228 |
.IX Header "OPTIONS" |
| 229 |
.ie n .SS "Setting ""STDIN"" and/or ""STDOUT"" individually" |
| 230 |
.el .SS "Setting \f(CWSTDIN\fP and/or \f(CWSTDOUT\fP individually" |
| 231 |
.IX Subsection "Setting STDIN and/or STDOUT individually" |
| 232 |
The encodings of \f(CW\*(C`STDIN\*(C'\fR and \f(CW\*(C`STDOUT\*(C'\fR are individually settable by parameters to |
| 233 |
the pragma: |
| 234 |
.PP |
| 235 |
.Vb 1 |
| 236 |
\& use encoding \*(Aqeuc\-tw\*(Aq, STDIN => \*(Aqgreek\*(Aq ...; |
| 237 |
.Ve |
| 238 |
.PP |
| 239 |
In this case, you cannot omit the first \fIENCNAME\fR. \f(CW\*(C`STDIN => undef\*(C'\fR |
| 240 |
turns the I/O transcoding completely off for that filehandle. |
| 241 |
.PP |
| 242 |
When \f(CW\*(C`${^UNICODE}\*(C'\fR (available starting in v5.8.2) exists and is non-zero, |
| 243 |
these options will be completely ignored. See "\f(CW\*(C`${^UNICODE}\*(C'\fR" in perlvar and |
| 244 |
"\f(CW\*(C`\-C\*(C'\fR" in perlrun for details. |
| 245 |
.ie n .SS "The "":locale"" sub-pragma" |
| 246 |
.el .SS "The \f(CW:locale\fP sub-pragma" |
| 247 |
.IX Subsection "The :locale sub-pragma" |
| 248 |
Starting in v5.8.6, the encoding name may be \f(CW\*(C`:locale\*(C'\fR. This means that the |
| 249 |
encoding is taken from the current locale, and not hard-coded by the pragma. |
| 250 |
Since a script really can only be encoded in exactly one encoding, this option |
| 251 |
is dangerous. It makes sense only if the script itself is written in ASCII, |
| 252 |
and all the possible locales that will be in use when the script is executed |
| 253 |
are supersets of ASCII. That means that the script itself doesn't get |
| 254 |
changed, but the I/O handles have the specified encoding added, and the |
| 255 |
operations like \f(CW\*(C`chr\*(C'\fR and \f(CW\*(C`ord\*(C'\fR use that encoding. |
| 256 |
.PP |
| 257 |
The logic of finding which locale \f(CW\*(C`:locale\*(C'\fR uses is as follows: |
| 258 |
.IP 1. 4 |
| 259 |
If the platform supports the \f(CWlanginfo(CODESET)\fR interface, the codeset |
| 260 |
returned is used as the default encoding for the open pragma. |
| 261 |
.IP 2. 4 |
| 262 |
If 1. didn't work but we are under the locale pragma, the environment |
| 263 |
variables \f(CW\*(C`LC_ALL\*(C'\fR and \f(CW\*(C`LANG\*(C'\fR (in that order) are matched for encodings |
| 264 |
(the part after "\f(CW\*(C`.\*(C'\fR", if any), and if any found, that is used |
| 265 |
as the default encoding for the open pragma. |
| 266 |
.IP 3. 4 |
| 267 |
If 1. and 2. didn't work, the environment variables \f(CW\*(C`LC_ALL\*(C'\fR and \f(CW\*(C`LANG\*(C'\fR |
| 268 |
(in that order) are matched for anything looking like UTF\-8, and if |
| 269 |
any found, \f(CW\*(C`:utf8\*(C'\fR is used as the default encoding for the open |
| 270 |
pragma. |
| 271 |
.PP |
| 272 |
If your locale environment variables (\f(CW\*(C`LC_ALL\*(C'\fR, \f(CW\*(C`LC_CTYPE\*(C'\fR, \f(CW\*(C`LANG\*(C'\fR) |
| 273 |
contain the strings 'UTF\-8' or 'UTF8' (case-insensitive matching), |
| 274 |
the default encoding of your \f(CW\*(C`STDIN\*(C'\fR, \f(CW\*(C`STDOUT\*(C'\fR, and \f(CW\*(C`STDERR\*(C'\fR, and of |
| 275 |
\&\fBany subsequent file open\fR, is UTF\-8. |
| 276 |
.SH CAVEATS |
| 277 |
.IX Header "CAVEATS" |
| 278 |
.SS "SIDE EFFECTS" |
| 279 |
.IX Subsection "SIDE EFFECTS" |
| 280 |
.IP \(bu 4 |
| 281 |
If the \f(CW\*(C`encoding\*(C'\fR pragma is in scope then the lengths returned are |
| 282 |
calculated from the length of \f(CW$/\fR in Unicode characters, which is not |
| 283 |
always the same as the length of \f(CW$/\fR in the native encoding. |
| 284 |
.IP \(bu 4 |
| 285 |
Without this pragma, if strings operating under byte semantics and strings |
| 286 |
with Unicode character data are concatenated, the new string will |
| 287 |
be created by decoding the byte strings as \fIISO 8859\-1 (Latin\-1)\fR. |
| 288 |
.Sp |
| 289 |
The \fBencoding\fR pragma changes this to use the specified encoding |
| 290 |
instead. For example: |
| 291 |
.Sp |
| 292 |
.Vb 5 |
| 293 |
\& use encoding \*(Aqutf8\*(Aq; |
| 294 |
\& my $string = chr(20000); # a Unicode string |
| 295 |
\& utf8::encode($string); # now it\*(Aqs a UTF\-8 encoded byte string |
| 296 |
\& # concatenate with another Unicode string |
| 297 |
\& print length($string . chr(20000)); |
| 298 |
.Ve |
| 299 |
.Sp |
| 300 |
Will print \f(CW2\fR, because \f(CW$string\fR is upgraded as UTF\-8. Without |
| 301 |
\&\f(CW\*(C`use encoding \*(Aqutf8\*(Aq;\*(C'\fR, it will print \f(CW4\fR instead, since \f(CW$string\fR |
| 302 |
is three octets when interpreted as Latin\-1. |
| 303 |
.SS "DO NOT MIX MULTIPLE ENCODINGS" |
| 304 |
.IX Subsection "DO NOT MIX MULTIPLE ENCODINGS" |
| 305 |
Notice that only literals (string or regular expression) having only |
| 306 |
legacy code points are affected: if you mix data like this |
| 307 |
.PP |
| 308 |
.Vb 2 |
| 309 |
\& \ex{100}\exDF |
| 310 |
\& \exDF\ex{100} |
| 311 |
.Ve |
| 312 |
.PP |
| 313 |
the data is assumed to be in (Latin 1 and) Unicode, not in your native |
| 314 |
encoding. In other words, this will match in "greek": |
| 315 |
.PP |
| 316 |
.Vb 1 |
| 317 |
\& "\exDF" =~ /\ex{3af}/ |
| 318 |
.Ve |
| 319 |
.PP |
| 320 |
but this will not |
| 321 |
.PP |
| 322 |
.Vb 1 |
| 323 |
\& "\exDF\ex{100}" =~ /\ex{3af}\ex{100}/ |
| 324 |
.Ve |
| 325 |
.PP |
| 326 |
since the \f(CW\*(C`\exDF\*(C'\fR (ISO 8859\-7 GREEK SMALL LETTER IOTA WITH TONOS) on |
| 327 |
the left will \fBnot\fR be upgraded to \f(CW\*(C`\ex{3af}\*(C'\fR (Unicode GREEK SMALL |
| 328 |
LETTER IOTA WITH TONOS) because of the \f(CW\*(C`\ex{100}\*(C'\fR on the left. You |
| 329 |
should not be mixing your legacy data and Unicode in the same string. |
| 330 |
.PP |
| 331 |
This pragma also affects encoding of the 0x80..0xFF code point range: |
| 332 |
normally characters in that range are left as eight-bit bytes (unless |
| 333 |
they are combined with characters with code points 0x100 or larger, |
| 334 |
in which case all characters need to become UTF\-8 encoded), but if |
| 335 |
the \f(CW\*(C`encoding\*(C'\fR pragma is present, even the 0x80..0xFF range always |
| 336 |
gets UTF\-8 encoded. |
| 337 |
.PP |
| 338 |
After all, the best thing about this pragma is that you don't have to |
| 339 |
resort to \ex{....} just to spell your name in a native encoding. |
| 340 |
So feel free to put your strings in your encoding in quotes and |
| 341 |
regexes. |
| 342 |
.SS "Prior to Perl v5.22" |
| 343 |
.IX Subsection "Prior to Perl v5.22" |
| 344 |
The pragma was a per script, not a per block lexical. Only the last |
| 345 |
\&\f(CW\*(C`use encoding\*(C'\fR or \f(CW\*(C`no encoding\*(C'\fR mattered, and it affected |
| 346 |
\&\fBthe whole script\fR. However, the \f(CW\*(C`no encoding\*(C'\fR pragma was supported and |
| 347 |
\&\f(CW\*(C`use encoding\*(C'\fR could appear as many times as you want in a given script |
| 348 |
(though only the last was effective). |
| 349 |
.PP |
| 350 |
Since the scope wasn't lexical, other modules' use of \f(CW\*(C`chr\*(C'\fR, \f(CW\*(C`ord\*(C'\fR, \fIetc.\fR |
| 351 |
were affected. This leads to spooky, incorrect action at a distance that is |
| 352 |
hard to debug. |
| 353 |
.PP |
| 354 |
This means you would have to be very careful of the load order: |
| 355 |
.PP |
| 356 |
.Vb 5 |
| 357 |
\& # called module |
| 358 |
\& package Module_IN_BAR; |
| 359 |
\& use encoding "bar"; |
| 360 |
\& # stuff in "bar" encoding here |
| 361 |
\& 1; |
| 362 |
\& |
| 363 |
\& # caller script |
| 364 |
\& use encoding "foo" |
| 365 |
\& use Module_IN_BAR; |
| 366 |
\& # surprise! use encoding "bar" is in effect. |
| 367 |
.Ve |
| 368 |
.PP |
| 369 |
The best way to avoid this oddity is to use this pragma RIGHT AFTER |
| 370 |
other modules are loaded. i.e. |
| 371 |
.PP |
| 372 |
.Vb 2 |
| 373 |
\& use Module_IN_BAR; |
| 374 |
\& use encoding "foo"; |
| 375 |
.Ve |
| 376 |
.SS "Prior to Encode version 1.87" |
| 377 |
.IX Subsection "Prior to Encode version 1.87" |
| 378 |
.IP \(bu 4 |
| 379 |
\&\f(CW\*(C`STDIN\*(C'\fR and \f(CW\*(C`STDOUT\*(C'\fR were not set under the filter option. |
| 380 |
And \f(CW\*(C`STDIN=>\fR\f(CIENCODING\fR\f(CW\*(C'\fR and \f(CW\*(C`STDOUT=>\fR\f(CIENCODING\fR\f(CW\*(C'\fR didn't work like |
| 381 |
non-filter version. |
| 382 |
.IP \(bu 4 |
| 383 |
\&\f(CW\*(C`use utf8\*(C'\fR wasn't implicitly declared so you have to \f(CW\*(C`use utf8\*(C'\fR to do |
| 384 |
.Sp |
| 385 |
.Vb 1 |
| 386 |
\& ${"\ex{4eba}"}++ |
| 387 |
.Ve |
| 388 |
.SS "Prior to Perl v5.8.1" |
| 389 |
.IX Subsection "Prior to Perl v5.8.1" |
| 390 |
.IP """NON-EUC"" doublebyte encodings" 4 |
| 391 |
.IX Item """NON-EUC"" doublebyte encodings" |
| 392 |
Because perl needs to parse the script before applying this pragma, such |
| 393 |
encodings as Shift_JIS and Big\-5 that may contain \f(CW\*(Aq\e\*(Aq\fR (BACKSLASH; |
| 394 |
\&\f(CW\*(C`\ex5c\*(C'\fR) in the second byte fail because the second byte may |
| 395 |
accidentally escape the quoting character that follows. |
| 396 |
.ie n .IP """tr///""" 4 |
| 397 |
.el .IP \f(CWtr///\fR 4 |
| 398 |
.IX Item "tr///" |
| 399 |
The \fBencoding\fR pragma works by decoding string literals in |
| 400 |
\&\f(CW\*(C`q//,qq//,qr//,qw///, qx//\*(C'\fR and so forth. In perl v5.8.0, this |
| 401 |
does not apply to \f(CW\*(C`tr///\*(C'\fR. Therefore, |
| 402 |
.Sp |
| 403 |
.Vb 4 |
| 404 |
\& use encoding \*(Aqeuc\-jp\*(Aq; |
| 405 |
\& #.... |
| 406 |
\& $kana =~ tr/\exA4\exA1\-\exA4\exF3/\exA5\exA1\-\exA5\exF3/; |
| 407 |
\& # \-\-\-\-\-\-\-\- \-\-\-\-\-\-\-\- \-\-\-\-\-\-\-\- \-\-\-\-\-\-\-\- |
| 408 |
.Ve |
| 409 |
.Sp |
| 410 |
Does not work as |
| 411 |
.Sp |
| 412 |
.Vb 1 |
| 413 |
\& $kana =~ tr/\ex{3041}\-\ex{3093}/\ex{30a1}\-\ex{30f3}/; |
| 414 |
.Ve |
| 415 |
.RS 4 |
| 416 |
.IP "Legend of characters above" 4 |
| 417 |
.IX Item "Legend of characters above" |
| 418 |
.Vb 6 |
| 419 |
\& utf8 euc\-jp charnames::viacode() |
| 420 |
\& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- |
| 421 |
\& \ex{3041} \exA4\exA1 HIRAGANA LETTER SMALL A |
| 422 |
\& \ex{3093} \exA4\exF3 HIRAGANA LETTER N |
| 423 |
\& \ex{30a1} \exA5\exA1 KATAKANA LETTER SMALL A |
| 424 |
\& \ex{30f3} \exA5\exF3 KATAKANA LETTER N |
| 425 |
.Ve |
| 426 |
.RE |
| 427 |
.RS 4 |
| 428 |
.Sp |
| 429 |
This counterintuitive behavior has been fixed in perl v5.8.1. |
| 430 |
.Sp |
| 431 |
In perl v5.8.0, you can work around this as follows; |
| 432 |
.Sp |
| 433 |
.Vb 3 |
| 434 |
\& use encoding \*(Aqeuc\-jp\*(Aq; |
| 435 |
\& # .... |
| 436 |
\& eval qq{ \e$kana =~ tr/\exA4\exA1\-\exA4\exF3/\exA5\exA1\-\exA5\exF3/ }; |
| 437 |
.Ve |
| 438 |
.Sp |
| 439 |
Note the \f(CW\*(C`tr//\*(C'\fR expression is surrounded by \f(CW\*(C`qq{}\*(C'\fR. The idea behind |
| 440 |
this is the same as the classic idiom that makes \f(CW\*(C`tr///\*(C'\fR 'interpolate': |
| 441 |
.Sp |
| 442 |
.Vb 2 |
| 443 |
\& tr/$from/$to/; # wrong! |
| 444 |
\& eval qq{ tr/$from/$to/ }; # workaround. |
| 445 |
.Ve |
| 446 |
.RE |
| 447 |
.SH "EXAMPLE \- Greekperl" |
| 448 |
.IX Header "EXAMPLE - Greekperl" |
| 449 |
.Vb 1 |
| 450 |
\& use encoding "iso 8859\-7"; |
| 451 |
\& |
| 452 |
\& # \exDF in ISO 8859\-7 (Greek) is \ex{3af} in Unicode. |
| 453 |
\& |
| 454 |
\& $a = "\exDF"; |
| 455 |
\& $b = "\ex{100}"; |
| 456 |
\& |
| 457 |
\& printf "%#x\en", ord($a); # will print 0x3af, not 0xdf |
| 458 |
\& |
| 459 |
\& $c = $a . $b; |
| 460 |
\& |
| 461 |
\& # $c will be "\ex{3af}\ex{100}", not "\ex{df}\ex{100}". |
| 462 |
\& |
| 463 |
\& # chr() is affected, and ... |
| 464 |
\& |
| 465 |
\& print "mega\en" if ord(chr(0xdf)) == 0x3af; |
| 466 |
\& |
| 467 |
\& # ... ord() is affected by the encoding pragma ... |
| 468 |
\& |
| 469 |
\& print "tera\en" if ord(pack("C", 0xdf)) == 0x3af; |
| 470 |
\& |
| 471 |
\& # ... as are eq and cmp ... |
| 472 |
\& |
| 473 |
\& print "peta\en" if "\ex{3af}" eq pack("C", 0xdf); |
| 474 |
\& print "exa\en" if "\ex{3af}" cmp pack("C", 0xdf) == 0; |
| 475 |
\& |
| 476 |
\& # ... but pack/unpack C are not affected, in case you still |
| 477 |
\& # want to go back to your native encoding |
| 478 |
\& |
| 479 |
\& print "zetta\en" if unpack("C", (pack("C", 0xdf))) == 0xdf; |
| 480 |
.Ve |
| 481 |
.SH BUGS |
| 482 |
.IX Header "BUGS" |
| 483 |
.IP "Thread safety" 4 |
| 484 |
.IX Item "Thread safety" |
| 485 |
\&\f(CW\*(C`use encoding ...\*(C'\fR is not thread-safe (i.e., do not use in threaded |
| 486 |
applications). |
| 487 |
.IP "Can't be used by more than one module in a single program." 4 |
| 488 |
.IX Item "Can't be used by more than one module in a single program." |
| 489 |
Only one encoding is allowed. If you combine modules in a program that have |
| 490 |
different encodings, only one will be actually used. |
| 491 |
.ie n .IP "Other modules using ""STDIN"" and ""STDOUT"" get the encoded stream" 4 |
| 492 |
.el .IP "Other modules using \f(CWSTDIN\fR and \f(CWSTDOUT\fR get the encoded stream" 4 |
| 493 |
.IX Item "Other modules using STDIN and STDOUT get the encoded stream" |
| 494 |
They may be expecting something completely different. |
| 495 |
.IP "literals in regex that are longer than 127 bytes" 4 |
| 496 |
.IX Item "literals in regex that are longer than 127 bytes" |
| 497 |
For native multibyte encodings (either fixed or variable length), |
| 498 |
the current implementation of the regular expressions may introduce |
| 499 |
recoding errors for regular expression literals longer than 127 bytes. |
| 500 |
.IP EBCDIC 4 |
| 501 |
.IX Item "EBCDIC" |
| 502 |
The encoding pragma is not supported on EBCDIC platforms. |
| 503 |
.ie n .IP """format""" 4 |
| 504 |
.el .IP \f(CWformat\fR 4 |
| 505 |
.IX Item "format" |
| 506 |
This pragma doesn't work well with \f(CW\*(C`format\*(C'\fR because PerlIO does not |
| 507 |
get along very well with it. When \f(CW\*(C`format\*(C'\fR contains non-ASCII |
| 508 |
characters it prints funny or gets "wide character warnings". |
| 509 |
To understand it, try the code below. |
| 510 |
.Sp |
| 511 |
.Vb 11 |
| 512 |
\& # Save this one in utf8 |
| 513 |
\& # replace *non\-ascii* with a non\-ascii string |
| 514 |
\& my $camel; |
| 515 |
\& format STDOUT = |
| 516 |
\& *non\-ascii*@>>>>>>> |
| 517 |
\& $camel |
| 518 |
\& . |
| 519 |
\& $camel = "*non\-ascii*"; |
| 520 |
\& binmode(STDOUT=>\*(Aq:encoding(utf8)\*(Aq); # bang! |
| 521 |
\& write; # funny |
| 522 |
\& print $camel, "\en"; # fine |
| 523 |
.Ve |
| 524 |
.Sp |
| 525 |
Without binmode this happens to work but without binmode, \fBprint()\fR |
| 526 |
fails instead of \fBwrite()\fR. |
| 527 |
.Sp |
| 528 |
At any rate, the very use of \f(CW\*(C`format\*(C'\fR is questionable when it comes to |
| 529 |
unicode characters since you have to consider such things as character |
| 530 |
width (i.e. double-width for ideographs) and directions (i.e. BIDI for |
| 531 |
Arabic and Hebrew). |
| 532 |
.IP "See also ""CAVEATS""" 4 |
| 533 |
.IX Item "See also ""CAVEATS""" |
| 534 |
.SH HISTORY |
| 535 |
.IX Header "HISTORY" |
| 536 |
This pragma first appeared in Perl v5.8.0. It has been enhanced in later |
| 537 |
releases as specified above. |
| 538 |
.SH "SEE ALSO" |
| 539 |
.IX Header "SEE ALSO" |
| 540 |
perlunicode, Encode, open, Filter::Util::Call, |
| 541 |
.PP |
| 542 |
Ch. 15 of \f(CW\*(C`Programming Perl (3rd Edition)\*(C'\fR |
| 543 |
by Larry Wall, Tom Christiansen, Jon Orwant; |
| 544 |
O'Reilly & Associates; ISBN 0\-596\-00027\-8 |