1encoding::warnings(3pm)Perl Programmers Reference Guideencoding::warnings(3pm)
2
3
4

NAME

6       encoding::warnings - Warn on implicit encoding conversions
7

VERSION

9       This document describes version 0.11 of encoding::warnings, released
10       June 5, 2007.
11

SYNOPSIS

13           use encoding::warnings; # or 'FATAL' to raise fatal exceptions
14
15           utf8::encode($a = chr(20000));  # a byte-string (raw bytes)
16           $b = chr(20000);                # a unicode-string (wide characters)
17
18           # "Bytes implicitly upgraded into wide characters as iso-8859-1"
19           $c = $a . $b;
20

DESCRIPTION

22   Overview of the problem
23       By default, there is a fundamental asymmetry in Perl's unicode model:
24       implicit upgrading from byte-strings to unicode-strings assumes that
25       they were encoded in ISO 8859-1 (Latin-1), but unicode-strings are
26       downgraded with UTF-8 encoding.  This happens because the first 256
27       codepoints in Unicode happens to agree with Latin-1.
28
29       However, this silent upgrading can easily cause problems, if you happen
30       to mix unicode strings with non-Latin1 data -- i.e. byte-strings
31       encoded in UTF-8 or other encodings.  The error will not manifest until
32       the combined string is written to output, at which time it would be
33       impossible to see where did the silent upgrading occur.
34
35   Detecting the problem
36       This module simplifies the process of diagnosing such problems.  Just
37       put this line on top of your main program:
38
39           use encoding::warnings;
40
41       Afterwards, implicit upgrading of high-bit bytes will raise a warning.
42       Ex.: "Bytes implicitly upgraded into wide characters as iso-8859-1 at -
43       line 7".
44
45       However, strings composed purely of ASCII code points (0x00..0x7F) will
46       not trigger this warning.
47
48       You can also make the warnings fatal by importing this module as:
49
50           use encoding::warnings 'FATAL';
51
52   Solving the problem
53       Most of the time, this warning occurs when a byte-string is
54       concatenated with a unicode-string.  There are a number of ways to
55       solve it:
56
57       ·   Upgrade both sides to unicode-strings
58
59           If your program does not need compatibility for Perl 5.6 and
60           earlier, the recommended approach is to apply appropriate IO
61           disciplines, so all data in your program become unicode-strings.
62           See encoding, open and "binmode" in perlfunc for how.
63
64       ·   Downgrade both sides to byte-strings
65
66           The other way works too, especially if you are sure that all your
67           data are under the same encoding, or if compatibility with older
68           versions of Perl is desired.
69
70           You may downgrade strings with "Encode::encode" and "utf8::encode".
71           See Encode and utf8 for details.
72
73       ·   Specify the encoding for implicit byte-string upgrading
74
75           If you are confident that all byte-strings will be in a specific
76           encoding like UTF-8, and need not support older versions of Perl,
77           use the "encoding" pragma:
78
79               use encoding 'utf8';
80
81           Similarly, this will silence warnings from this module, and
82           preserve the default behaviour:
83
84               use encoding 'iso-8859-1';
85
86           However, note that "use encoding" actually had three distinct
87           effects:
88
89           ·   PerlIO layers for STDIN and STDOUT
90
91               This is similar to what open pragma does.
92
93           ·   Literal conversions
94
95               This turns all literal string in your program into unicode-
96               strings (equivalent to a "use utf8"), by decoding them using
97               the specified encoding.
98
99           ·   Implicit upgrading for byte-strings
100
101               This will silence warnings from this module, as shown above.
102
103           Because literal conversions also work on empty strings, it may
104           surprise some people:
105
106               use encoding 'big5';
107
108               my $byte_string = pack("C*", 0xA4, 0x40);
109               print length $a;    # 2 here.
110               $a .= "";           # concatenating with a unicode string...
111               print length $a;    # 1 here!
112
113           In other words, do not "use encoding" unless you are certain that
114           the program will not deal with any raw, 8-bit binary data at all.
115
116           However, the "Filter => 1" flavor of "use encoding" will not affect
117           implicit upgrading for byte-strings, and is thus incapable of
118           silencing warnings from this module.  See encoding for more
119           details.
120

CAVEATS

122       For Perl 5.9.4 or later, this module's effect is lexical.
123
124       For Perl versions prior to 5.9.4, this module affects the whole script,
125       instead of inside its lexical block.
126

SEE ALSO

128       perlunicode, perluniintro
129
130       open, utf8, encoding, Encode
131

AUTHORS

133       Audrey Tang
134
136       Copyright 2004, 2005, 2006, 2007 by Audrey Tang <cpan@audreyt.org>.
137
138       This program is free software; you can redistribute it and/or modify it
139       under the same terms as Perl itself.
140
141       See <http://www.perl.com/perl/misc/Artistic.html>
142
143
144
145perl v5.12.4                      2011-06-01           encoding::warnings(3pm)
Impressum