Reading a Binary File

Perl is designed to handle string data. If you are reading or writing binary data in Perl, you’ve probably got the wrong language. Notwithstanding this fact, it is possible to do binary I/O with Perl.

In this section, you see how to write a program that reads a binary file and dumps it out as a series of hex numbers and characters. A typical run looks like

 53 65 61 72 63 68 43 61: S e a r c h C a 74 3d 62 65 72 65 69 63: t = b e r e i c 68 25 33 64 70 6b 77 25: h % 3 d p k w % 32 36 74 69 74 65 6c 25: 2 6 t i t e l % 33 64 32 26 62 65 72 65: 3 d 2 & b e r e 69 63 68 3d 70 6b 77 26: i c h = p k w & 73 70 72 61 63 68 65 3d: s p r a c h e = 32 26 44 6f 53 65 61 72: 2 & D o S e a r 63 68 3d 31 26 46 6f 72: c h = 1 & F o r ...

Get Perl for C Programmers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.