How can I decode UTF-16 data in Perl?

Posted by Geo on Stack Overflow See other posts from Stack Overflow or by Geo
Published on 2010-05-22T11:21:14Z Indexed on 2010/05/22 14:50 UTC
Read the original article Hit count: 270

Filed under:
|
|

If I open a file ( and specify an encoding directly ) :

open(my $file,"<:encoding(UTF-16)","some.file") || die "error $!\n";
while(<$file>) {
    print "$_\n";
}
close($file);

I can read the file contents nicely. However, if I do:

use Encode;

open(my $file,"some.file") || die "error $!\n";
while(<$file>) {
    print decode("UTF-16",$_);
}
close($file);

I get the following error:

UTF-16:Unrecognised BOM d at F:/Perl/lib/Encode.pm line 174

How can I make it work with decode?

© Stack Overflow or respective owner

Related posts about perl

Related posts about decode