1encoding(3)           User Contributed Perl Documentation          encoding(3)
2
3
4

NAME

6       encoding - allows you to write your script in non-ascii or non-utf8
7

WARNING

9       This module is deprecated under perl 5.18.  It uses a mechanism
10       provided by perl that is deprecated under 5.18 and higher, and may be
11       removed in a future version.
12
13       The easiest and the best alternative is to write your script in UTF-8
14       and declear:
15
16         use utf8; # not use encoding ':utf8';
17
18       See perluniintro and utf8 for details.
19

SYNOPSIS

21         use encoding "greek";  # Perl like Greek to you?
22         use encoding "euc-jp"; # Jperl!
23
24         # or you can even do this if your shell supports your native encoding
25
26         perl -Mencoding=latin2 -e'...' # Feeling centrally European?
27         perl -Mencoding=euc-kr -e'...' # Or Korean?
28
29         # more control
30
31         # A simple euc-cn => utf-8 converter
32         use encoding "euc-cn", STDOUT => "utf8";  while(<>){print};
33
34         # "no encoding;" supported (but not scoped!)
35         no encoding;
36
37         # an alternate way, Filter
38         use encoding "euc-jp", Filter=>1;
39         # now you can use kanji identifiers -- in euc-jp!
40
41         # switch on locale -
42         # note that this probably means that unless you have a complete control
43         # over the environments the application is ever going to be run, you should
44         # NOT use the feature of encoding pragma allowing you to write your script
45         # in any recognized encoding because changing locale settings will wreck
46         # the script; you can of course still use the other features of the pragma.
47         use encoding ':locale';
48

ABSTRACT

50       Let's start with a bit of history: Perl 5.6.0 introduced Unicode
51       support.  You could apply "substr()" and regexes even to complex CJK
52       characters -- so long as the script was written in UTF-8.  But back
53       then, text editors that supported UTF-8 were still rare and many users
54       instead chose to write scripts in legacy encodings, giving up a whole
55       new feature of Perl 5.6.
56
57       Rewind to the future: starting from perl 5.8.0 with the encoding
58       pragma, you can write your script in any encoding you like (so long as
59       the "Encode" module supports it) and still enjoy Unicode support.  This
60       pragma achieves that by doing the following:
61
62       ·   Internally converts all literals ("q//,qq//,qr//,qw///, qx//") from
63           the encoding specified to utf8.  In Perl 5.8.1 and later, literals
64           in "tr///" and "DATA" pseudo-filehandle are also converted.
65
66       ·   Changing PerlIO layers of "STDIN" and "STDOUT" to the encoding
67            specified.
68
69   Literal Conversions
70       You can write code in EUC-JP as follows:
71
72         my $Rakuda = "\xF1\xD1\xF1\xCC"; # Camel in Kanji
73                      #<-char-><-char->   # 4 octets
74         s/\bCamel\b/$Rakuda/;
75
76       And with "use encoding "euc-jp"" in effect, it is the same thing as the
77       code in UTF-8:
78
79         my $Rakuda = "\x{99F1}\x{99DD}"; # two Unicode Characters
80         s/\bCamel\b/$Rakuda/;
81
82   PerlIO layers for "STD(IN|OUT)"
83       The encoding pragma also modifies the filehandle layers of STDIN and
84       STDOUT to the specified encoding.  Therefore,
85
86         use encoding "euc-jp";
87         my $message = "Camel is the symbol of perl.\n";
88         my $Rakuda = "\xF1\xD1\xF1\xCC"; # Camel in Kanji
89         $message =~ s/\bCamel\b/$Rakuda/;
90         print $message;
91
92       Will print "\xF1\xD1\xF1\xCC is the symbol of perl.\n", not
93       "\x{99F1}\x{99DD} is the symbol of perl.\n".
94
95       You can override this by giving extra arguments; see below.
96
97   Implicit upgrading for byte strings
98       By default, if strings operating under byte semantics and strings with
99       Unicode character data are concatenated, the new string will be created
100       by decoding the byte strings as ISO 8859-1 (Latin-1).
101
102       The encoding pragma changes this to use the specified encoding instead.
103       For example:
104
105           use encoding 'utf8';
106           my $string = chr(20000); # a Unicode string
107           utf8::encode($string);   # now it's a UTF-8 encoded byte string
108           # concatenate with another Unicode string
109           print length($string . chr(20000));
110
111       Will print 2, because $string is upgraded as UTF-8.  Without "use
112       encoding 'utf8';", it will print 4 instead, since $string is three
113       octets when interpreted as Latin-1.
114
115   Side effects
116       If the "encoding" pragma is in scope then the lengths returned are
117       calculated from the length of $/ in Unicode characters, which is not
118       always the same as the length of $/ in the native encoding.
119
120       This pragma affects utf8::upgrade, but not utf8::downgrade.
121

FEATURES THAT REQUIRE 5.8.1

123       Some of the features offered by this pragma requires perl 5.8.1.  Most
124       of these are done by Inaba Hiroto.  Any other features and changes are
125       good for 5.8.0.
126
127       "NON-EUC" doublebyte encodings
128           Because perl needs to parse script before applying this pragma,
129           such encodings as Shift_JIS and Big-5 that may contain '\'
130           (BACKSLASH; \x5c) in the second byte fails because the second byte
131           may accidentally escape the quoting character that follows.  Perl
132           5.8.1 or later fixes this problem.
133
134       tr//
135           "tr//" was overlooked by Perl 5 porters when they released perl
136           5.8.0 See the section below for details.
137
138       DATA pseudo-filehandle
139           Another feature that was overlooked was "DATA".
140

USAGE

142       use encoding [ENCNAME] ;
143           Sets the script encoding to ENCNAME.  And unless ${^UNICODE} exists
144           and non-zero, PerlIO layers of STDIN and STDOUT are set to
145           ":encoding(ENCNAME)".
146
147           Note that STDERR WILL NOT be changed.
148
149           Also note that non-STD file handles remain unaffected.  Use "use
150           open" or "binmode" to change layers of those.
151
152           If no encoding is specified, the environment variable PERL_ENCODING
153           is consulted.  If no encoding can be found, the error "Unknown
154           encoding 'ENCNAME'" will be thrown.
155
156       use encoding ENCNAME [ STDIN => ENCNAME_IN ...] ;
157           You can also individually set encodings of STDIN and STDOUT via the
158           "STDIN => ENCNAME" form.  In this case, you cannot omit the first
159           ENCNAME.  "STDIN => undef" turns the IO transcoding completely off.
160
161           When ${^UNICODE} exists and non-zero, these options will completely
162           ignored.  ${^UNICODE} is a variable introduced in perl 5.8.1.  See
163           perlrun see "${^UNICODE}" in perlvar and "-C" in perlrun for
164           details (perl 5.8.1 and later).
165
166       use encoding ENCNAME Filter=>1;
167           This turns the encoding pragma into a source filter.  While the
168           default approach just decodes interpolated literals (in qq() and
169           qr()), this will apply a source filter to the entire source code.
170           See "The Filter Option" below for details.
171
172       no encoding;
173           Unsets the script encoding. The layers of STDIN, STDOUT are reset
174           to ":raw" (the default unprocessed raw stream of bytes).
175

The Filter Option

177       The magic of "use encoding" is not applied to the names of identifiers.
178       In order to make "${"\x{4eba}"}++" ($human++, where human is a single
179       Han ideograph) work, you still need to write your script in UTF-8 -- or
180       use a source filter.  That's what 'Filter=>1' does.
181
182       What does this mean?  Your source code behaves as if it is written in
183       UTF-8 with 'use utf8' in effect.  So even if your editor only supports
184       Shift_JIS, for example, you can still try examples in Chapter 15 of
185       "Programming Perl, 3rd Ed.".  For instance, you can use UTF-8
186       identifiers.
187
188       This option is significantly slower and (as of this writing) non-ASCII
189       identifiers are not very stable WITHOUT this option and with the source
190       code written in UTF-8.
191
192   Filter-related changes at Encode version 1.87
193       ·   The Filter option now sets STDIN and STDOUT like non-filter
194           options.  And "STDIN=>ENCODING" and "STDOUT=>ENCODING" work like
195           non-filter version.
196
197       ·   "use utf8" is implicitly declared so you no longer have to "use
198           utf8" to "${"\x{4eba}"}++".
199

CAVEATS

201   NOT SCOPED
202       The pragma is a per script, not a per block lexical.  Only the last
203       "use encoding" or "no encoding" matters, and it affects the whole
204       script.  However, the <no encoding> pragma is supported and use
205       encoding can appear as many times as you want in a given script.  The
206       multiple use of this pragma is discouraged.
207
208       By the same reason, the use this pragma inside modules is also
209       discouraged (though not as strongly discouraged as the case above.  See
210       below).
211
212       If you still have to write a module with this pragma, be very careful
213       of the load order.  See the codes below;
214
215         # called module
216         package Module_IN_BAR;
217         use encoding "bar";
218         # stuff in "bar" encoding here
219         1;
220
221         # caller script
222         use encoding "foo"
223         use Module_IN_BAR;
224         # surprise! use encoding "bar" is in effect.
225
226       The best way to avoid this oddity is to use this pragma RIGHT AFTER
227       other modules are loaded.  i.e.
228
229         use Module_IN_BAR;
230         use encoding "foo";
231
232   DO NOT MIX MULTIPLE ENCODINGS
233       Notice that only literals (string or regular expression) having only
234       legacy code points are affected: if you mix data like this
235
236           \xDF\x{100}
237
238       the data is assumed to be in (Latin 1 and) Unicode, not in your native
239       encoding.  In other words, this will match in "greek":
240
241           "\xDF" =~ /\x{3af}/
242
243       but this will not
244
245           "\xDF\x{100}" =~ /\x{3af}\x{100}/
246
247       since the "\xDF" (ISO 8859-7 GREEK SMALL LETTER IOTA WITH TONOS) on the
248       left will not be upgraded to "\x{3af}" (Unicode GREEK SMALL LETTER IOTA
249       WITH TONOS) because of the "\x{100}" on the left.  You should not be
250       mixing your legacy data and Unicode in the same string.
251
252       This pragma also affects encoding of the 0x80..0xFF code point range:
253       normally characters in that range are left as eight-bit bytes (unless
254       they are combined with characters with code points 0x100 or larger, in
255       which case all characters need to become UTF-8 encoded), but if the
256       "encoding" pragma is present, even the 0x80..0xFF range always gets
257       UTF-8 encoded.
258
259       After all, the best thing about this pragma is that you don't have to
260       resort to \x{....} just to spell your name in a native encoding.  So
261       feel free to put your strings in your encoding in quotes and regexes.
262
263   tr/// with ranges
264       The encoding pragma works by decoding string literals in
265       "q//,qq//,qr//,qw///, qx//" and so forth.  In perl 5.8.0, this does not
266       apply to "tr///".  Therefore,
267
268         use encoding 'euc-jp';
269         #....
270         $kana =~ tr/\xA4\xA1-\xA4\xF3/\xA5\xA1-\xA5\xF3/;
271         #           -------- -------- -------- --------
272
273       Does not work as
274
275         $kana =~ tr/\x{3041}-\x{3093}/\x{30a1}-\x{30f3}/;
276
277       Legend of characters above
278             utf8     euc-jp   charnames::viacode()
279             -----------------------------------------
280             \x{3041} \xA4\xA1 HIRAGANA LETTER SMALL A
281             \x{3093} \xA4\xF3 HIRAGANA LETTER N
282             \x{30a1} \xA5\xA1 KATAKANA LETTER SMALL A
283             \x{30f3} \xA5\xF3 KATAKANA LETTER N
284
285       This counterintuitive behavior has been fixed in perl 5.8.1.
286
287       workaround to tr///;
288
289       In perl 5.8.0, you can work around as follows;
290
291         use encoding 'euc-jp';
292         #  ....
293         eval qq{ \$kana =~ tr/\xA4\xA1-\xA4\xF3/\xA5\xA1-\xA5\xF3/ };
294
295       Note the "tr//" expression is surrounded by "qq{}".  The idea behind is
296       the same as classic idiom that makes "tr///" 'interpolate'.
297
298          tr/$from/$to/;            # wrong!
299          eval qq{ tr/$from/$to/ }; # workaround.
300
301       Nevertheless, in case of encoding pragma even "q//" is affected so
302       "tr///" not being decoded was obviously against the will of Perl5
303       Porters so it has been fixed in Perl 5.8.1 or later.
304

EXAMPLE - Greekperl

306           use encoding "iso 8859-7";
307
308           # \xDF in ISO 8859-7 (Greek) is \x{3af} in Unicode.
309
310           $a = "\xDF";
311           $b = "\x{100}";
312
313           printf "%#x\n", ord($a); # will print 0x3af, not 0xdf
314
315           $c = $a . $b;
316
317           # $c will be "\x{3af}\x{100}", not "\x{df}\x{100}".
318
319           # chr() is affected, and ...
320
321           print "mega\n"  if ord(chr(0xdf)) == 0x3af;
322
323           # ... ord() is affected by the encoding pragma ...
324
325           print "tera\n" if ord(pack("C", 0xdf)) == 0x3af;
326
327           # ... as are eq and cmp ...
328
329           print "peta\n" if "\x{3af}" eq  pack("C", 0xdf);
330           print "exa\n"  if "\x{3af}" cmp pack("C", 0xdf) == 0;
331
332           # ... but pack/unpack C are not affected, in case you still
333           # want to go back to your native encoding
334
335           print "zetta\n" if unpack("C", (pack("C", 0xdf))) == 0xdf;
336

KNOWN PROBLEMS

338       literals in regex that are longer than 127 bytes
339           For native multibyte encodings (either fixed or variable length),
340           the current implementation of the regular expressions may introduce
341           recoding errors for regular expression literals longer than 127
342           bytes.
343
344       EBCDIC
345           The encoding pragma is not supported on EBCDIC platforms.  (Porters
346           who are willing and able to remove this limitation are welcome.)
347
348       format
349           This pragma doesn't work well with format because PerlIO does not
350           get along very well with it.  When format contains non-ascii
351           characters it prints funny or gets "wide character warnings".  To
352           understand it, try the code below.
353
354             # Save this one in utf8
355             # replace *non-ascii* with a non-ascii string
356             my $camel;
357             format STDOUT =
358             *non-ascii*@>>>>>>>
359             $camel
360             .
361             $camel = "*non-ascii*";
362             binmode(STDOUT=>':encoding(utf8)'); # bang!
363             write;              # funny
364             print $camel, "\n"; # fine
365
366           Without binmode this happens to work but without binmode, print()
367           fails instead of write().
368
369           At any rate, the very use of format is questionable when it comes
370           to unicode characters since you have to consider such things as
371           character width (i.e. double-width for ideographs) and directions
372           (i.e. BIDI for Arabic and Hebrew).
373
374       Thread safety
375           "use encoding ..." is not thread-safe (i.e., do not use in threaded
376           applications).
377
378   The Logic of :locale
379       The logic of ":locale" is as follows:
380
381       1.  If the platform supports the langinfo(CODESET) interface, the
382           codeset returned is used as the default encoding for the open
383           pragma.
384
385       2.  If 1. didn't work but we are under the locale pragma, the
386           environment variables LC_ALL and LANG (in that order) are matched
387           for encodings (the part after ".", if any), and if any found, that
388           is used as the default encoding for the open pragma.
389
390       3.  If 1. and 2. didn't work, the environment variables LC_ALL and LANG
391           (in that order) are matched for anything looking like UTF-8, and if
392           any found, ":utf8" is used as the default encoding for the open
393           pragma.
394
395       If your locale environment variables (LC_ALL, LC_CTYPE, LANG) contain
396       the strings 'UTF-8' or 'UTF8' (case-insensitive matching), the default
397       encoding of your STDIN, STDOUT, and STDERR, and of any subsequent file
398       open, is UTF-8.
399

HISTORY

401       This pragma first appeared in Perl 5.8.0.  For features that require
402       5.8.1 and better, see above.
403
404       The ":locale" subpragma was implemented in 2.01, or Perl 5.8.6.
405

SEE ALSO

407       perlunicode, Encode, open, Filter::Util::Call,
408
409       Ch. 15 of "Programming Perl (3rd Edition)" by Larry Wall, Tom
410       Christiansen, Jon Orwant; O'Reilly & Associates; ISBN 0-596-00027-8
411
412
413
414perl v5.16.3                      2013-04-29                       encoding(3)
Impressum