This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Typos in *.p[lm]
[perl5.git] / lib / encoding / warnings.pm
CommitLineData
de1df517
RGS
1# $File: //member/autrijus/.vimrc $ $Author: autrijus $
2# $Revision: #14 $ $Change: 4137 $ $DateTime: 2003/02/08 11:41:59 $
3
4package encoding::warnings;
5$encoding::warnings::VERSION = '0.05';
6
7use strict;
8
9=head1 NAME
10
11encoding::warnings - Warn on implicit encoding conversions
12
13=head1 VERSION
14
15This document describes version 0.05 of encoding::warnings, released
16July 15, 2004.
17
18=head1 SYNOPSIS
19
20 use encoding::warnings; # or 'FATAL' to raise fatal exceptions
21
22 utf8::encode($a = chr(20000)); # a byte-string (raw bytes)
23 $b = chr(20000); # a unicode-string (wide characters)
24
25 # "Bytes implicitly upgraded into wide characters as iso-8859-1"
26 $c = $a . $b;
27
28=head1 DESCRIPTION
29
30=head2 Overview of the problem
31
32By default, there is a fundamental asymmetry in Perl's unicode model:
33implicit upgrading from byte-strings to unicode-strings assumes that
34they were encoded in I<ISO 8859-1 (Latin-1)>, but unicode-strings are
35downgraded with UTF-8 encoding. This happens because the first 256
36codepoints in Unicode happens to agree with Latin-1.
37
38However, this silent upgrading can easily cause problems, if you happen
39to mix unicode strings with non-Latin1 data -- i.e. byte-strings encoded
40in UTF-8 or other encodings. The error will not manifest until the
41combined string is written to output, at which time it would be impossible
42to see where did the silent upgrading occur.
43
44=head2 Detecting the problem
45
46This module simplifies the process of diagnosing such problems. Just put
47this line on top of your main program:
48
49 use encoding::warnings;
50
51Afterwards, implicit upgrading of high-bit bytes will raise a warning.
52Ex.: C<Bytes implicitly upgraded into wide characters as iso-8859-1 at
53- line 7>.
54
55However, strings composed purely of ASCII code points (C<0x00>..C<0x7F>)
56will I<not> trigger this warning.
57
58You can also make the warnings fatal by importing this module as:
59
60 use encoding::warnings 'FATAL';
61
62=head2 Solving the problem
63
64Most of the time, this warning occurs when a byte-string is concatenated
65with a unicode-string. There are a number of ways to solve it:
66
67=over 4
68
69=item * Upgrade both sides to unicode-strings
70
71If your program does not need compatibility for Perl 5.6 and earlier,
72the recommended approach is to apply appropriate IO disciplines, so all
73data in your program become unicode-strings. See L<encoding>, L<open> and
74L<perlfunc/binmode> for how.
75
76=item * Downgrade both sides to byte-strings
77
78The other way works too, especially if you are sure that all your data
79are under the same encoding, or if compatibility with older versions
80of Perl is desired.
81
82You may downgrade strings with C<Encode::encode> and C<utf8::encode>.
83See L<Encode> and L<utf8> for details.
84
85=item * Specify the encoding for implicit byte-string upgrading
86
87If you are confident that all byte-strings will be in a specific
88encoding like UTF-8, I<and> need not support older versions of Perl,
89use the C<encoding> pragma:
90
91 use encoding 'utf8';
92
93Similarly, this will silence warnings from this module, and preserve the
94default behaviour:
95
96 use encoding 'iso-8859-1';
97
98However, note that C<use encoding> actually had three distinct effects:
99
100=over 4
101
102=item * PerlIO layers for B<STDIN> and B<STDOUT>
103
104This is similar to what L<open> pragma does.
105
106=item * Literal conversions
107
108This turns I<all> literal string in your program into unicode-strings
109(equivalent to a C<use utf8>), by decoding them using the specified
110encoding.
111
112=item * Implicit upgrading for byte-strings
113
114This will silence warnings from this module, as shown above.
115
116=back
117
118Because literal conversions also work on empty strings, it may surprise
119some people:
120
121 use encoding 'big5';
122
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!
127
128In other words, do not C<use encoding> unless you are certain that the
129program will not deal with any raw, 8-bit binary data at all.
130
131However, the C<Filter =E<gt> 1> flavor of C<use encoding> will I<not>
132affect implicit upgrading for byte-strings, and is thus incapable of
133silencing warnings from this module. See L<encoding> for more details.
134
135=back
136
137=head1 CAVEATS
138
139This module currently affects the whole script, instead of inside its
140lexical block. This is expected to be addressed during Perl 5.9 development,
141where the B<encoding> module will also be made lexical.
142
143=cut
144
145# Constants.
146sub ASCII () { 0 }
147sub LATIN1 () { 1 }
148sub FATAL () { 2 }
149
150# Install a ${^ENCODING} handler if no other one are already in place.
151sub import {
152 my $class = shift;
153 my $fatal = shift || '';
154
155 local $@;
156 return if ${^ENCODING} and ref(${^ENCODING}) ne $class;
157 return unless eval { require Encode; 1 };
158
159 my $ascii = Encode::find_encoding('us-ascii') or return;
160 my $latin1 = Encode::find_encoding('iso-8859-1') or return;
161
162 # Have to undef explicitly here
163 undef ${^ENCODING};
164
165 # Install a warning handler for decode()
166 ${^ENCODING} = bless(
167 [
168 $ascii,
169 $latin1,
170 (($fatal eq 'FATAL') ? 'Carp::croak' : 'Carp::carp'),
171 ], $class,
172 );
173}
174
175# Don't worry about source code literals.
176sub cat_decode {
177 my $self = shift;
178 return $self->[LATIN1]->cat_decode(@_);
179}
180
181# Warn if the data is not purely US-ASCII.
182sub decode {
183 my $self = shift;
184
185 local $@;
186 my $rv = eval { $self->[ASCII]->decode($_[0], Encode::FB_CROAK()) };
187 return $rv unless $@;
188
189 require Carp;
190 no strict 'refs';
191 $self->[FATAL]->(
192 "Bytes implicitly upgraded into wide characters as iso-8859-1"
193 );
194 return $self->[LATIN1]->decode(@_);
195}
196
197sub name { 'iso-8859-1' }
198
1991;
200
201__END__
202
203=head1 SEE ALSO
204
205L<perlunicode>, L<perluniintro>
206
207L<open>, L<utf8>, L<encoding>, L<Encode>
208
209=head1 AUTHORS
210
211Autrijus Tang E<lt>autrijus@autrijus.orgE<gt>
212
213=head1 COPYRIGHT
214
215Copyright 2004 by Autrijus Tang E<lt>autrijus@autrijus.orgE<gt>.
216
217This program is free software; you can redistribute it and/or modify it
218under the same terms as Perl itself.
219
220See L<http://www.perl.com/perl/misc/Artistic.html>
221
222=cut