If you just specify “UTF-16,” Perl will look for the byte character (BOM) to figure out how to parse it. If there is no specification, it will explode. In this case, you must specify an Encode that is in byte order, specifying either "UTF-16LE" for little-endian or "UTF-16BE" for big-endian.
Something else is happening with your situation, but it's hard to say without seeing the data that you have in the file. I get the same error with both fragments. If I don't have a specification and I don't specify the byte order, my Perl complains anyway. What Perl do you use and what platform do you have? Does your platform have your own source code for your file? I think the behavior that I see is correct according to the docs.
Furthermore, you cannot just read the string in some unknown encoding (no matter what was the default for Perl), and then pass it to decode . You may be in the middle of a multibyte sequence. You should use Encode::FB_QUIET to save the portion of the buffer that you could not decode and add to the following piece of data:
open my($lefh), '<:raw', 'text-utf16.txt'; my $string; while( $string .= <$lefh> ) { print decode("UTF-16LE", $string, Encode::FB_QUIET) }
brian d foy
source share