#!/usr/bin/perl -w use strict; use Encode qw(decode); use List::Util qw(max); my $expected_octets = "äöü"; my $expected_charset = "iso-8859-1"; my $got_octets = "\204\224\201"; my @characters = split //, decode($expected_charset, $expected_octets); my @res; for my $encoding (Encode->encodings(":all")) { eval { my @got_characters = split //, decode($encoding, $got_octets); my $max_length = max(scalar(@characters), scalar(@got_characters)); my $hits = 0; for my $i (0 .. $max_length-1) { no warnings 'uninitialized'; $hits++ if ($characters[$i] eq $got_characters[$i]); } push @res, { Quality => ($hits/$max_length)*100, Encoding => $encoding, }; }; } print join("", map { sprintf "%-20s: %d%%\n", $_->{Encoding}, $_->{Quality} } sort { $b->{Quality} <=> $a->{Quality} } grep { $_->{Quality} > 0 } @res);