Encode::Guess
NAMESYNOPSIS
ABSTRACT
DESCRIPTION
CAVEATS
TO DO
SEE ALSO
NAME
Encode::Guess −− Guesses encoding from data
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)
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 and "ascii".
Encode::Guess−>set_suspects
You can also change the internal suspects list via "set_suspects" method.
use Encode::Guess;
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.
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 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 in Encode::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 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.
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
• |
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 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. |
# 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.
• |
On the other hand, mixing various national standard encodings automagically works unless $data is too short to allow for guessing. |
# 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 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
More Linux Commands
manpages/dhcping.8.html
dhcping(8) - send a DHCP request to DHCP server to see if it
This command allows the system administrator to check if a remote DHCP server is still functioning. Options are: -v Verbose, print some information. -i Use DHCP
manpages/chown.1.html
chown(1) - change file owner and group - Linux manual page
This manual page documents the GNU version of chown. chown changes the user and/or group ownership of each given file. If only an owner (a user name or numeric
manpages/Tk_GetColor.3.html
Tk_GetColor(3) - maintain database of colors (Man Page).....
These procedures manage the colors being used by a Tk application. They allow colors to be shared whenever possible, so that colormap space is preserved, and th
manpages/Tcl_GlobalEval.3.html
Tcl_GlobalEval(3) - execute Tcl scripts - Linux manual page
The procedures described here are invoked to execute Tcl scripts in various forms. Tcl_EvalObjEx is the core procedure and is used by many of the others. It exe
manpages/gstack.1.html
gstack(1) - print a stack trace of a running process........
gstack attaches to the active process named by the pid on the command line, and prints out an execution stack trace. If ELF symbols exist in the binary (usually
manpages/zypper.8.html
zypper(8) - Command-line interface to ZYpp system management
zypper is a command-line interface to ZYpp system management library (libzypp). It can be used to install, update, remove software, manage repositories, perform
manpages/perlsolaris.1.html
perlsolaris(1) - Perl version 5 on Solaris systems (ManPage)
This document describes various features of Suns Solaris operating system that will affect how Perl version 5 (hereafter just perl) is compiled and/or runs. Som
manpages/semctl.2.html
semctl(2) - semaphore control operations - Linux man page...
semctl() performs the control operation specified by cmd on the System V semaphore set identified by semid, or on the semnum-th semaphore of that set. (The sema
manpages/sysv_signal.3.html
sysv_signal(3) - signal handling with System V semantics....
The sysv_signal() function takes the same arguments, and performs the same task, as signal(2). However sysv_signal() provides the System V unreliable signal sem
manpages/xprt_register.3.html
xprt_register(3) - library routines for remote procedure cal
These routines allow C programs to make procedure calls on other machines across the network. First, the client calls a procedure to send a data packet to the s
manpages/Tcl_PosixError.3.html
Tcl_PosixError(3) - retrieve or record information about err
The Tcl_SetReturnOptions and Tcl_GetReturnOptions routines expose the │ same capabilities as the return and catch commands, respectively, in │ the f
manpages/XTextExtents.3.html
XTextExtents(3) - compute or query text extents (Man Page)
The XTextExtents and XTextExtents16 functions perform the size computation locally and, thereby, avoid the round-trip overhead of XQueryTextExtents and XQueryTe
