1 # $File: //member/autrijus/.vimrc $ $Author: autrijus $
2 # $Revision: #14 $ $Change: 4137 $ $DateTime: 2003/02/08 11:41:59 $
4 package encoding::warnings;
5 $encoding::warnings::VERSION = '0.05';
11 encoding::warnings - Warn on implicit encoding conversions
15 This document describes version 0.05 of encoding::warnings, released
20 use encoding::warnings; # or 'FATAL' to raise fatal exceptions
22 utf8::encode($a = chr(20000)); # a byte-string (raw bytes)
23 $b = chr(20000); # a unicode-string (wide characters)
25 # "Bytes implicitly upgraded into wide characters as iso-8859-1"
30 =head2 Overview of the problem
32 By default, there is a fundamental asymmetry in Perl's unicode model:
33 implicit upgrading from byte-strings to unicode-strings assumes that
34 they were encoded in I<ISO 8859-1 (Latin-1)>, but unicode-strings are
35 downgraded with UTF-8 encoding. This happens because the first 256
36 codepoints in Unicode happens to agree with Latin-1.
38 However, this silent upgrading can easily cause problems, if you happen
39 to mix unicode strings with non-Latin1 data -- i.e. byte-strings encoded
40 in UTF-8 or other encodings. The error will not manifest until the
41 combined string is written to output, at which time it would be impossible
42 to see where did the silent upgrading occur.
44 =head2 Detecting the problem
46 This module simplifies the process of diagnosing such problems. Just put
47 this line on top of your main program:
49 use encoding::warnings;
51 Afterwards, implicit upgrading of high-bit bytes will raise a warning.
52 Ex.: C<Bytes implicitly upgraded into wide characters as iso-8859-1 at
55 However, strings composed purely of ASCII code points (C<0x00>..C<0x7F>)
56 will I<not> trigger this warning.
58 You can also make the warnings fatal by importing this module as:
60 use encoding::warnings 'FATAL';
62 =head2 Solving the problem
64 Most of the time, this warning occurs when a byte-string is concatenated
65 with a unicode-string. There are a number of ways to solve it:
69 =item * Upgrade both sides to unicode-strings
71 If your program does not need compatibility for Perl 5.6 and earlier,
72 the recommended approach is to apply appropriate IO disciplines, so all
73 data in your program become unicode-strings. See L<encoding>, L<open> and
74 L<perlfunc/binmode> for how.
76 =item * Downgrade both sides to byte-strings
78 The other way works too, especially if you are sure that all your data
79 are under the same encoding, or if compatibility with older versions
82 You may downgrade strings with C<Encode::encode> and C<utf8::encode>.
83 See L<Encode> and L<utf8> for details.
85 =item * Specify the encoding for implicit byte-string upgrading
87 If you are confident that all byte-strings will be in a specific
88 encoding like UTF-8, I<and> need not support older versions of Perl,
89 use the C<encoding> pragma:
93 Similarly, this will silence warnings from this module, and preserve the
96 use encoding 'iso-8859-1';
98 However, note that C<use encoding> actually had three distinct effects:
102 =item * PerlIO layers for B<STDIN> and B<STDOUT>
104 This is similar to what L<open> pragma does.
106 =item * Literal conversions
108 This turns I<all> literal string in your program into unicode-strings
109 (equivalent to a C<use utf8>), by decoding them using the specified
112 =item * Implicit upgrading for byte-strings
114 This will silence warnings from this module, as shown above.
118 Because literal conversions also work on empty strings, it may surprise
123 my $byte_string = pack("C*", 0xA4, 0x40);
124 print length $a; # 2 here.
125 $a .= ""; # concatenating with a unicode string...
126 print length $a; # 1 here!
128 In other words, do not C<use encoding> unless you are certain that the
129 program will not deal with any raw, 8-bit binary data at all.
131 However, the C<Filter =E<gt> 1> flavor of C<use encoding> will I<not>
132 affect implicit upgrading for byte-strings, and is thus incapable of
133 silencing warnings from this module. See L<encoding> for more details.
139 This module currently affects the whole script, instead of inside its
140 lexical block. This is expected to be addressed during Perl 5.9 development,
141 where the B<encoding> module will also be made lexical.
150 # Install a ${^ENCODING} handler if no other one are already in place.
153 my $fatal = shift || '';
156 return if ${^ENCODING} and ref(${^ENCODING}) ne $class;
157 return unless eval { require Encode; 1 };
159 my $ascii = Encode::find_encoding('us-ascii') or return;
160 my $latin1 = Encode::find_encoding('iso-8859-1') or return;
162 # Have to undef explicitly here
165 # Install a warning handler for decode()
166 ${^ENCODING} = bless(
170 (($fatal eq 'FATAL') ? 'Carp::croak' : 'Carp::carp'),
175 # Don't worry about source code literals.
178 return $self->[LATIN1]->cat_decode(@_);
181 # Warn if the data is not purely US-ASCII.
186 my $rv = eval { $self->[ASCII]->decode($_[0], Encode::FB_CROAK()) };
187 return $rv unless $@;
192 "Bytes implicitly upgraded into wide characters as iso-8859-1"
194 return $self->[LATIN1]->decode(@_);
197 sub name { 'iso-8859-1' }
205 L<perlunicode>, L<perluniintro>
207 L<open>, L<utf8>, L<encoding>, L<Encode>
211 Autrijus Tang E<lt>autrijus@autrijus.orgE<gt>
215 Copyright 2004 by Autrijus Tang E<lt>autrijus@autrijus.orgE<gt>.
217 This program is free software; you can redistribute it and/or modify it
218 under the same terms as Perl itself.
220 See L<http://www.perl.com/perl/misc/Artistic.html>