How to use Encode::Detective?

Posted by freonpsandoz on 2016-09-22 21:52
Forums: ActivePerl discussion | OS: Windows

I am trying to use Encode::Detective to determine the encoding of cue sheets. Here is my code:

use strict;
use warnings;
use Encode;
use Encode::Detective 'detect';
require Win32::LongPath;

my $fh;

# Read in raw mode and attempt to detect the encoding.
Win32::LongPath::openL(\$fh, '<', $ARGV[0]) or die $!;
binmode($fh);
local $/ = undef;
my $teststring = <$fh>;
my $encoding = Encode::Detective::detect($teststring);
printf("Detected encoding %s\n", $encoding->name);

Encode::Detective::detect seems to always return 'undef'. What am I doing wrong? Thanks in advance.

Update:

I just found out that detect doesn't always return undef. Out of the 1208 audio cue sheets that I'm trying to determine the encoding for, it detects an encoding for 114 of them (9.4%.) Not very helpful. Does detect (or should it be expected to) return 'ascii' if the string contains only byte values 0-0x7f? What exactly does the return value of undef mean? Thanks.