I am not at all sure that STDIN can even be opened in binary mode at all!

Yes it works the same on STDIN as other handles. Specifically, it disables crlf→lf conversion on Windows machines, it stops treating chr(26) as the end of file on non-PerlIO Windows builds, and does nothing elsewhere.

I mean CTL-C, CTL-Z mean things to STDIN although these are certainly valid binary values.

No they don't. They may mean something to the tty/console, but STDIN doesn't even know about the Ctrl key. It doesn't treat character 3 or 26 specially.

>perl -e"print qq{\x03\x1A}" | perl -le"print uc unpack 'H*', <STDIN>" 031A $perl -e'print qq{\x03\x1A}' | perl -le'print uc unpack "H*", <STDIN>' 031A

If you want to read binary data, open a file in binmode... local $/ = undef; is not needed.

Not true at all. $/ is quite useful on binary files.

my @records = map parse_rec($_), map /(.{$RECSIZE})/sg, do { local $/; <$fh> };

and

my @records; local $/ = \$RECSIZE; local *_; while (<$fh>) { push @records, parse_rec($_); }

are equivalent to

my @records; local *_; while (read($fh, $_, $RECSIZE)) { push @records, parse_rec($rec); }

Mind you, read is unaffected by $/, but that has nothing to do with whether the file is binary or not.


In reply to Re^2: Mysteries of unpack("a", ...) by ikegami
in thread Mysteries of unpack("a", ...) by pspinler

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.