Modules

  • ABCDE
  • FGHIL
  • MNOPS
  • TUX

Tools

Encode::Guess

Perl 5 version 26.0 documentation
Recently read

Encode::Guess

NAME

Encode::Guess -- Guesses encoding from data

SYNOPSIS

  1. # if you are sure $data won't contain anything bogus
  2. use Encode;
  3. use Encode::Guess qw/euc-jp shiftjis 7bit-jis/;
  4. my $utf8 = decode("Guess", $data);
  5. my $data = encode("Guess", $utf8); # this doesn't work!
  6. # more elaborate way
  7. use Encode::Guess;
  8. my $enc = guess_encoding($data, qw/euc-jp shiftjis 7bit-jis/);
  9. ref($enc) or die "Can't guess: $enc"; # trap error this way
  10. $utf8 = $enc->decode($data);
  11. # or
  12. $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.

  1. 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.

  1. # tries all major Japanese Encodings as well
  2. 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 and ascii .

  • Encode::Guess->set_suspects

    You can also change the internal suspects list via set_suspects method.

    1. use Encode::Guess;
    2. Encode::Guess->set_suspects(qw/euc-jp shiftjis 7bit-jis/);
  • Encode::Guess->add_suspects

    Or you can use add_suspects method. The difference is that set_suspects flushes the current suspects list while add_suspects adds.

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

    When you are content with suspects list, you can now

    1. 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;

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

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

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

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

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

    You can also try guess_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 is not reflected to the internal suspects list.

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

CAVEATS

  • Because of the algorithm used, ISO-8859 series and other single-byte encodings do not work well unless either one of ISO-8859 is the only one suspect (besides ascii and utf8).

    1. use Encode::Guess;
    2. # perhaps ok
    3. my $decoder = guess_encoding($data, 'latin1');
    4. # definitely NOT ok
    5. my $decoder = guess_encoding($data, qw/latin1 greek/);

    The reason is that Encode::Guess guesses encoding by trial and error. It first splits $data into lines and tries to decode the line for each suspect. It keeps it going until all but one encoding is eliminated out of suspects list. ISO-8859 series is just too successful for most cases (because it fills almost all code points in \x00-\xff).

  • Do not mix national standard encodings and the corresponding vendor encodings.

    1. # a very bad idea
    2. my $decoder
    3. = guess_encoding($data, qw/shiftjis MacJapanese cp932/);

    The reason is that vendor encoding is usually a superset of national standard so it becomes too ambiguous for most cases.

  • On the other hand, mixing various national standard encodings automagically works unless $data is too short to allow for guessing.

    1. # This is ok if $data is long enough
    2. my $decoder =
    3. guess_encoding($data, qw/euc-cn
    4. euc-jp shiftjis 7bit-jis
    5. euc-kr
    6. big5-eten/);
  • DO NOT PUT TOO MANY SUSPECTS! Don't you try something like this!

    1. my $decoder = guess_encoding($data,
    2. Encode->encodings(":all"));

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