Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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):

  1. Pop optional discp (discipline/layer string) from stack.
  2. Resolve GV → IO → IoIFP.
  3. Call mode_from_discipline(d, len) for :raw/:crlf.
  4. 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

See Also

PP runtime: binmode | open, PerlIO, Encode