Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^4: Error binmode() on unopened filehandle

by haukex (Bishop)
on May 03, 2020 at 15:55 UTC ( #11116393=note: print w/replies, xml ) Need Help??


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";

Replies are listed 'Best First'.
Re^5: Error binmode() on unopened filehandle
by Marshall (Canon) on May 03, 2020 at 21:54 UTC
    Ok. But I refer you to https://perldoc.perl.org/functions/binmode.html.
    In general, binmode should be called after open but before any I/O is done on the filehandle.
    Yes, there are some exceptions to this "rule". But I figure this "rule" is a good one.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11116393]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (1)
As of 2021-09-27 03:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?