Movatterモバイル変換


[0]ホーム

URL:


Encode::Guess
(source,CPAN)
version 2.04
You are viewing the version of this documentation from Perl 5.14.4.View the latest version

CONTENTS

#NAME

Encode::Guess -- Guesses encoding from data

#SYNOPSIS

# if you are sure $data won't contain anything bogususe Encode;use Encode::Guess qw/euc-jp shiftjis 7bit-jis/;my $utf8 = decode("Guess", $data);my $data = encode("Guess", $utf8);   # this doesn't work!# more elaborate wayuse Encode::Guess;my $enc = guess_encoding($data, qw/euc-jp shiftjis 7bit-jis/);ref($enc) or die "Can't guess: $enc"; # trap error this way$utf8 = $enc->decode($data);# or$utf8 = decode($enc->name, $data)

#ABSTRACT

Encode::Guess enables you to guess in what encoding a given data is encoded, or at least tries to.

#DESCRIPTION

By default, it checks only ascii, utf8 and UTF-16/32 with BOM.

use Encode::Guess; # ascii/utf8/BOMed UTF

To use it more practically, you have to give the names of encodings to check (suspects as follows). The name of suspects can either be canonical names or aliases.

CAVEAT: Unlike UTF-(16|32), BOM in utf8 is NOT AUTOMATICALLY STRIPPED.

# tries all major Japanese Encodings as well use Encode::Guess qw/euc-jp shiftjis 7bit-jis/;

If the$Encode::Guess::NoUTFAutoGuess variable is set to a true value, no heuristics will be applied to UTF8/16/32, and the result will be limited to the suspects andascii.

#Encode::Guess->set_suspects

You can also change the internal suspects list viaset_suspects method.

use Encode::Guess;Encode::Guess->set_suspects(qw/euc-jp shiftjis 7bit-jis/);
#Encode::Guess->add_suspects

Or you can useadd_suspects method. The difference is thatset_suspects flushes the current suspects list whileadd_suspects adds.

use Encode::Guess;Encode::Guess->add_suspects(qw/euc-jp shiftjis 7bit-jis/);# now the suspects are euc-jp,shiftjis,7bit-jis, AND# euc-kr,euc-cn, and big5-etenEncode::Guess->add_suspects(qw/euc-kr euc-cn big5-eten/);
#Encode::decode("Guess" ...)

When you are content with suspects list, you can now

my $utf8 = Encode::decode("Guess", $data);
#Encode::Guess->guess($data)

But it will croak if:

  • Two or more suspects remain

  • No suspects left

So you should instead try this;

my $decoder = Encode::Guess->guess($data);

On success, $decoder is an object that is documented inEncode::Encoding. So you can now do this;

my $utf8 = $decoder->decode($data);

On failure, $decoder now contains an error message so the whole thing would be as follows;

my $decoder = Encode::Guess->guess($data);die $decoder unless ref($decoder);my $utf8 = $decoder->decode($data);
#guess_encoding($data, [,list of suspects])

You can also tryguess_encoding function which is exported by default. It takes $data to check and it also takes the list of suspects by option. The optional suspect list isnot reflected to the internal suspects list.

my $decoder = guess_encoding($data, qw/euc-jp euc-kr euc-cn/);die $decoder unless ref($decoder);my $utf8 = $decoder->decode($data);# check only ascii, utf8 and UTF-(16|32) with BOMmy $decoder = guess_encoding($data);

#CAVEATS

It is, after all, just a guess. You should alway be explicit when it comes to encodings. But there are some, especially Japanese, environment that guess-coding is a must. Use this module with care.

#TO DO

Encode::Guess does not work on EBCDIC platforms.

#SEE ALSO

Encode,Encode::Encoding

Perldoc Browser is maintained by Dan Book (DBOOK). Please contact him via theGitHub issue tracker oremail regarding any issues with the site itself, search, or rendering of documentation.

The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via thePerl issue tracker, themailing list, orIRC to report any issues with the contents or format of the documentation.


[8]ページ先頭

©2009-2025 Movatter.jp