How can I decode UTF-16 data in Perl?
- by Geo
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?