Encode::Guess(3perl)

πŸ’– Encode::Guess(3perl) πŸ’–

okay bestie, welcome to the Perl Programmers Reference Guide, and today we're decoding the mystery of what even is this data encoded as. it's Encode::Guess, she's cute, she's useful, she's occasionally gonna lie to you πŸ’…


✨ NAME ✨

Encode::Guess -- Guesses encoding from data

like literally she looks at your bytes and goes "hmm, that's giving euc-jp" πŸ¦‹


🌸 SYNOPSIS 🌸

# if you are sure $data won't contain anything bogus

use 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 way
use 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)

bestie, literally read the comment. the encode one is giving "no babe, that's not how this works" 😭 use the elaborate way, it's the main character path πŸ‘‘


πŸŽ€ ABSTRACT πŸŽ€

Encode::Guess enables you to guess in what encoding a given data is encoded, or at least tries to. and honestly?? the "tries to" is doing a LOT of heavy lifting there, keep that in your notes πŸ“


πŸ’… DESCRIPTION πŸ’…

by default she's lowkey chill and only checks ascii, utf8 and UTF-16/32 with BOM. that's it, that's the whole default vibe:

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

to make her actually useful, you have to hand her the names of encodings to check β€” those are the suspects πŸ“‹ and the name of a suspect can be either a canonical name or an alias, your call, she understands both fr fr.

CAVEAT: Unlike UTF-(16|32), BOM in utf8 is NOT AUTOMATICALLY STRIPPED. not me finding that out the hard way 😭 the BOM stays. it lives there now. πŸ™ˆ
# tries all major Japanese Encodings as well
use Encode::Guess qw/euc-jp shiftjis 7bit-jis/;

and if $Encode::Guess::NoUTFAutoGuess is set to a true value, then no heuristics get applied to UTF8/16/32, and the result will be limited to the suspects and "ascii". that variable is basically the "bestie don't guess, be ✨explicit✨" switch πŸ’–


πŸ‘‘ Encode::Guess->set_suspects πŸ‘‘

you can also change the internal suspects list through the "set_suspects" method:

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

πŸ¦‹ Encode::Guess->add_suspects πŸ¦‹

or you can use the "add_suspects" method. the difference is that "set_suspects" flushes the current suspects list while "add_suspects" just adds on top, no deletions, she's not like that 🌸

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-eten
Encode::Guess->add_suspects(qw/euc-kr euc-cn big5-eten/);

πŸ’œ Encode::decode("Guess" ...) πŸ’œ

when you're content with your suspects list, you can now do:

my $utf8 = Encode::decode("Guess", $data);

⭐ Encode::Guess->guess($data) ⭐

but heads up bestie, this one 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 in Encode::Encoding. so now you can do:

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

on failure though?? $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);

pro tip from me to you: always ref check. periodt. πŸ’…

🌈 guess_encoding($data, [, list of suspects]) 🌈

you can also try the "guess_encoding" function which is exported by default, no extra importing needed, she comes included 🎁 it takes $data to check and it also takes the list of suspects by option. and important: the optional suspect list is not reflected to the internal suspects list, it's a one-time thing, a little situationship if you will 😌

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 BOM
my $decoder = guess_encoding($data);

😭 CAVEATS 😭

okay this is the part where I hold your hand, bestie, because this module is a guess and guesses can be wrong 🫢

  • 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).
use Encode::Guess;
# perhaps ok
my $decoder = guess_encoding($data, 'latin1');
# definitely NOT ok
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 the suspects list. and the ISO-8859 series is just too successful for most cases (because it fills almost all code points in \x00-\xff). it's giving overachiever, it's giving "I can decode anything", and that's the problem πŸ’€

  • Do not mix national standard encodings and the corresponding vendor encodings.
# a very bad idea
my $decoder
   = 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. no cap, don't do it πŸ™…β€β™€οΈ

  • on the other hand, mixing various national standard encodings automagically works unless $data is too short to allow for guessing. slay, that one's fine πŸ¦‹
# This is ok if $data is long enough
my $decoder =
 guess_encoding($data, qw/euc-cn
                          euc-jp shiftjis 7bit-jis
                          euc-kr
                          big5-eten/);
  • DO NOT PUT TOO MANY SUSPECTS! Don't you try something like this! πŸ”₯
my $decoder = guess_encoding($data,
                             Encode->encodings(":all"));

it is, after all, just a guess. you should always 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. that's it, that's the todo, it's a short list, she said "not today" βœ‹πŸ˜”


πŸ‘€ SEE ALSO πŸ‘€

Encode, Encode::Encoding


and that's the whole page, bestie!! you ate, go decode something βœ¨πŸŒΈπŸ¦‹

signing off from perl v5.44.0, dated 2026-10-17 β€” Encode::Guess(3perl) out πŸ’…πŸ’–

πŸ“¦ where this page comes from
  • arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms