Processing Binary Files (Perl Cookbook, 2nd Edition)

## 8.11. Processing Binary Files

### 8.11.1. Problem

You want to read 8-bit binary data as 8-bit binary data, i.e., neither as characters in a particular encoding nor as a text file with any newline or end-of-file conversions that your I/O library might want to do.

### 8.11.2. Solution

Use the `binmode` function on the filehandle:

>     binmode(HANDLE);

### 8.11.3. Discussion

The `binmode` function lets you specify new I/O layers for a filehandle. The default layer to specify is `:raw`, which removes any layers that would interfere with binary data. The Solution is thus equivalent to:

>     binmode(HANDLE, ":raw");

except that explicitly specifying `:raw` only works on Perl 5.8 and later. The one-argument form of `binmode` works on all versions of Perl.

Because Perl makes `:crlf` the default if you are on an operating system that needs it, you should rarely (if ever) need to specify `:crlf` in your program. Furthermore, it's generally not wise to add or remove the `:crlf` layer once you've begun reading from the file, as there may be data already read into buffers that you can't unread. You can, however, safely change the `:encoding(...)` layer midstream (when parsing XML, for example).

You should get into the habit of calling `binmode` when you open a binary file. This will make your program portable to systems that might (un)helpfully translate bytes in your binary file into something unusable.

You may specify the I/O layers when you `open` a filehandle, rather than using `binmode` after the fact:

>     open(FH, "< :raw", $filename);       # binary mode

Specify the default set of layers for all subsequently opened input and output filehandles with the `open` pragma:

>     use open IN => ":raw";      # binary files

### 8.11.4. See Also

The PerlIO(3) manpage; the `open` and `binmode` functions in perlfunc(1) and in Chapter 29 of Programming Perl; your system's open(2) and fopen(3) manpages

Copyright © 2003 O'Reilly & Associates. All rights reserved.