binmode encoding: handling malformed data

154 Views Asked by At

With Encode::encode there are different ways to handle malformed data.

Does for binmode like binmode STDOUT ":encoding( $encoding_out )" also exist possibilities to handle malformed data?

1

There are 1 best solutions below

7
On BEST ANSWER

Yes, there's $PerlIO::encoding::fallback. You can assign the wanted “check” argument to that variable. When you then push a PerlIO layer onto a filehandle, the current value from that variable is saved for that layers failure behavior. E.g.:

use PerlIO::encoding;
use Encode;

# set a value
$PerlIO::encoding::fallback = Encode::FB_WARN;
# apply a layer under that value
open my $fh, '<:encoding(UTF-8)', $some_file or die ...;
# $PerlIO::encoding::fallback can be changed now