binmode
Sets the I/O discipline (encoding layer) for a filehandle.
Configures the encoding layer on a filehandle. When called with just a filehandle, sets binary mode (disabling any CRLF translation, though Linux does not perform CRLF translation by default). When called with a layer string, applies the specified encoding.
Currently recognized layers:
:utf8,:encoding(UTF-8)– mark the handle as UTF-8:raw,:bytes– mark the handle as raw bytes
Returns true on success, false on failure.
Synopsis
binmode(STDOUT); # set binary mode (no CRLF translation)
binmode(STDOUT, ":utf8"); # set UTF-8 encoding layer
binmode(STDOUT, ":raw"); # remove all layers
binmode($fh, ":encoding(UTF-8)");
Implementation Notes
perl5
pp_sys.c:1013(PP_wrapped pp_binmode):
- Pop optional
discp(discipline/layer string) from stack.- Resolve GV → IO → IoIFP.
- Call mode_from_discipline(d, len) for :raw/:crlf.
- Call PerlIO_binmode(fp, IoTYPE, mode, d) which calls PerlIO_apply_layers.
pperl: mirrors the flag model — parse layer string, set/clear PERLIO_F_UTF8. binmode(FH) → clear PERLIO_F_UTF8 (binmode with no layer = raw) binmode(FH, ‘:utf8’) → set PERLIO_F_UTF8 binmode(FH, ‘:raw’) → clear PERLIO_F_UTF8