http://www.perlmonks.org?node_id=11116393


in reply to Re^3: Error binmode() on unopened filehandle
in thread Error binmode() on unopened filehandle

You can't switch modes in the midst of a file.

You sure can:

use warnings;
use strict;
use utf8;
use Data::Dumper;
$Data::Dumper::Useqq=1;

print join(", ",PerlIO::get_layers(*DATA)),"\n";
print Dumper( scalar <DATA> );
binmode DATA;
print join(", ",PerlIO::get_layers(*DATA)),"\n";
print Dumper( scalar <DATA> );

__DATA__
H∃llⓄ, 🗺!
H∃llⓄ, 🗺!

Output:

unix, perlio, utf8 $VAR1 = "H\x{2203}ll\x{24c4}, \x{1f5fa}!\n"; unix, perlio $VAR1 = "H\342\210\203ll\342\223\204, \360\237\227\272!\n";