in reply to Re: Reading binary file byte by byte
in thread Reading binary file byte by byte

$/ is more flexible than you think.

{ open(my $FILE, "file.binary") or die $!; binmode($FILE); local $/ = \1; while ( my $byte = <$FILE> ) { ... } close $FILE; }

But messing with global variables is messy. What if "..." calls a function that reads from a file? Just use read.

{ open(my $FILE, "file.binary") or die $!; binmode($FILE); while (read($FILE, my $byte, 1)) { ... } close $FILE; }

Replies are listed 'Best First'.
Re^3: Reading binary file byte by byte
by anonymized user 468275 (Curate) on Dec 23, 2010 at 13:26 UTC
    Then we are back to reading a byte at a time from a file, which needs some thought applied first in terms of performance. It might be better than sysread which forces a system call with a 1 byte buffer, if thats what you request.

    Either way, if using buffered I/O, it seems more ept to read at least a page of memory in size at a time from the file but then process that one byte at a time.

    One world, one people

      read and readline (<>) are buffered, so it's not that bad. If you need Perl-side buffering, the following would be much better:
      use constant BLK_SIZE => 64*1024; { open(my $FILE, "file.binary") or die $!; binmode($FILE); while (read($FILE, my $buf, BLK_SIZE)) { for my $byte (unpack('a*', $buf)) { ... } } close $FILE; }