4 use Encode qw(:fallbacks find_encoding);
5 our $VERSION = do { my @r = (q$Revision: 1.4 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
9 our %DEF_SUSPECTS = map { $_ => find_encoding($_) } qw(ascii utf8);
10 $Encode::Encoding{$Canon} =
13 Suspects => { %DEF_SUSPECTS },
16 use base qw(Encode::Encoding);
20 our @EXPORT = qw(guess_encoding);
22 sub import { # Exporter not used so we do it on our own
24 for my $item (@EXPORT){
26 *{"$callpkg\::$item"} = \&{"$item"};
33 my $self = ref($class) ? $class : $Encode::Encoding{$Canon};
34 $self->{Suspects} = { %DEF_SUSPECTS };
35 $self->add_suspects(@_);
40 my $self = ref($class) ? $class : $Encode::Encoding{$Canon};
42 my $e = find_encoding($c) or die "Unknown encoding: $c";
43 $self->{Suspects}{$e->name} = $e;
44 $DEBUG and warn "Added: ", $e->name;
49 my ($obj, $octet, $chk) = @_;
50 my $guessed = guess($obj, $octet);
51 unless (ref($guessed)){
53 Carp::croak($guessed);
55 my $utf8 = $guessed->decode($octet, $chk);
56 $_[1] = $octet if $chk;
61 guess($Encode::Encoding{$Canon}, @_);
66 my $obj = ref($class) ? $class : $Encode::Encoding{$Canon};
69 Encode::is_utf8($octet) and return find_encoding('utf8');
72 my $BOM = unpack('n', $octet);
73 return find_encoding('UTF-16')
74 if ($BOM == 0xFeFF or $BOM == 0xFFFe);
75 $BOM = unpack('N', $octet);
76 return find_encoding('UTF-32')
77 if ($BOM == 0xFeFF or $BOM == 0xFFFe0000);
79 my %try = %{$obj->{Suspects}};
81 my $e = find_encoding($c) or die "Unknown encoding: $c";
83 $DEBUG and warn "Added: ", $e->name;
86 for my $line (split /\r|\n|\r\n/, $octet){
87 # cheat 2 -- \e in the string
90 delete @try{qw/utf8 ascii/};
92 ref($try{$k}) eq 'Encode::XS' and delete $try{$k};
96 # warn join(",", keys %try);
97 for my $k (keys %try){
99 $try{$k}->decode($scratch, FB_QUIET);
101 $DEBUG and warn sprintf("%4d:%-24s ok\n", $nline, $k);
105 warn sprintf("%4d:%-24s not ok; %d bytes left\n",
106 $nline, $k, bytes::length($scratch));
111 %ok or return "No appropriate encodings found!";
112 if (scalar(keys(%ok)) == 1){
113 my ($retval) = values(%ok);
116 %try = %ok; $nline++;
119 return "Encodings too ambiguous: ", join(" or ", keys %try);
130 Encode::Guess -- Guesses encoding from data
134 # if you are sure $data won't contain anything bogus
137 use Encode::Guess qw/euc-jp shiftjis 7bit-jis/;
138 my $utf8 = decode("Guess", $data);
139 my $data = encode("Guess", $utf8); # this doesn't work!
143 my $enc = guess_encoding($data, qw/euc-jp shiftjis 7bit-jis/);
144 ref($enc) or die "Can't guess: $enc"; # trap error this way
145 $utf8 = $enc->decode($data);
147 $utf8 = decode($enc->name, $data)
151 Encode::Guess enables you to guess in what encoding a given data is
152 encoded, or at least tries to.
156 By default, it checks only ascii, utf8 and UTF-16/32 with BOM.
158 use Encode::Guess; # ascii/utf8/BOMed UTF
160 To use it more practically, you have to give the names of encodings to
161 check (I<suspects> as follows). The name of suspects can either be
162 canonical names or aliases.
164 # tries all major Japanese Encodings as well
165 use Encode::Guess qw/euc-jp shiftjis 7bit-jis/;
169 =item Encode::Guess->set_suspects
171 You can also change the internal suspects list via C<set_suspects>
175 Encode::Guess->set_suspects(qw/euc-jp shiftjis 7bit-jis/);
177 =item Encode::Guess->add_suspects
179 Or you can use C<add_suspects> method. The difference is that
180 C<set_suspects> flushes the current suspects list while
181 C<add_suspects> adds.
184 Encode::Guess->add_suspects(qw/euc-jp shiftjis 7bit-jis/);
185 # now the suspects are euc-jp,shiftjis,7bit-jis, AND
186 # euc-kr,euc-cn, and big5-eten
187 Encode::Guess->add_suspects(qw/euc-kr euc-cn big5-eten/);
189 =item Encode::decode("Guess" ...)
191 When you are content with suspects list, you can now
193 my $utf8 = Encode::decode("Guess", $data);
195 =item Encode::Guess->guess($data)
197 But it will croak if Encode::Guess fails to eliminate all other
198 suspects but the right one or no suspect was good. So you should
201 my $decoder = Encode::Guess->guess($data);
203 On success, $decoder is an object that is documented in
204 L<Encode::Encoding>. So you can now do this;
206 my $utf8 = $decoder->decode($data);
208 On failure, $decoder now contains an error message so the whole thing
211 my $decoder = Encode::Guess->guess($data);
212 die $decoder unless ref($decoder);
213 my $utf8 = $decoder->decode($data);
215 =item guess_encoding($data, [, I<list of suspects>])
217 You can also try C<guess_encoding> function which is exported by
218 default. It takes $data to check and it also takes the list of
219 suspects by option. The optional suspect list is I<not reflected> to
220 the internal suspects list.
222 my $decoder = guess_encoding($data, qw/euc-jp euc-kr euc-cn/);
223 die $decoder unless ref($decoder);
224 my $utf8 = $decoder->decode($data);
225 # check only ascii and utf8
226 my $decoder = guess_encoding($data);
236 Because of the algorithm used, ISO-8859 series and other single-byte
237 encodings do not work well unless either one of ISO-8859 is the only
238 one suspect (besides ascii and utf8).
242 my $decoder = guess_encoding($data, 'latin1');
244 my $decoder = guess_encoding($data, qw/latin1 greek/);
246 The reason is that Encode::Guess guesses encoding by trial and error.
247 It first splits $data into lines and tries to decode the line for each
248 suspect. It keeps it going until all but one encoding was eliminated
249 out of suspects list. ISO-8859 series is just too successful for most
250 cases (because it fills almost all code points in \x00-\xff).
254 Do not mix national standard encodings and the corresponding vendor
259 = guess_encoding($data, qw/shiftjis MacJapanese cp932/);
261 The reason is that vendor encoding is usually a superset of national
262 standard so it becomes too ambiguous for most cases.
266 On the other hand, mixing various national standard encodings
267 automagically works unless $data is too short to allow for guessing.
269 # This is ok if $data is long enough
271 guess_encoding($data, qw/euc-cn
272 euc-jp shiftjis 7bit-jis
278 DO NOT PUT TOO MANY SUSPECTS! Don't you try something like this!
280 my $decoder = guess_encoding($data,
281 Encode->encodings(":all"));
285 It is, after all, just a guess. You should alway be explicit when it
286 comes to encodings. But there are some, especially Japanese,
287 environment that guess-coding is a must. Use this module with care.
291 L<Encode>, L<Encode::Encoding>