regeq has asked for the wisdom of the Perl Monks concerning the following question:

I am only familiar with processing ascii text files. Now I have some binary files to process for reporting. I have been advised to do this in C, but I want to use Perl!! Any tips on the best way to do this in Perl?

Replies are listed 'Best First'.
Re: Processing Binary Files
by MZSanford (Curate) on Jan 14, 2002 at 23:32 UTC
    IlyaM is very right with read and pack/unpack. I do tons of binary data work in Perl (actually, have a denied proposal with ORA on it because it was deamed to rarly used :( ), and here are a few things i can add :
    1. use binmode, rarly does portability come so easy
    2. Check and re-check your unpack formats
    3. Try to use the code on the same machine the file is created on (or atleast same kind of machine) to avoid :
      • endian problems (IRIX to Linux, among others)
      • int size errors (64-bit ints)
      • things i have not thought of
      • things that none of us can imagine (tech. changes)
    4. Before processing any file, divide the size (-s) by the read size. (Avoid processing 100 Gb of invalid data.)
    5. Make a module of it, that way the pack/unpack are in one place the next time. they suck to re-compute on a cocktail napkin, on a confrence call, from a pub, with a 300 bytes record length, working from an ancient language struct def :/ (not that this happens)

    $ perl -e 'do() || ! do() ;' Undefined subroutine &main::try
Re: Processing Binary Files
by IlyaM (Parson) on Jan 14, 2002 at 22:48 UTC
    Just open file, read it and unpack data structures in it. AFAIK for Win32 platform you should also use binmode to set binary mode for opened file. On UNIX it is just ignored so you can use it anyway if your want portability.

    --
    Ilya Martynov (http://martynov.org/)

Re: Processing Binary Files
by regeq (Initiate) on Jan 14, 2002 at 22:37 UTC
    I failed to mention I am needing to do this on Unix.