in reply to Re: perl in DOS woes
in thread perl in DOS woes

Then of course you can't use the usual file operators to read your data (but you shouldn't be doing that with a binary file anyway, even on *nix -- hopefully someone with more *nix experience can point out the pitfalls there).

You can use the usual file operators. There is nothing wrong with using them to read binary data. In fact, it can be quite handy as long as you understand what you are doing.

Usually, you'll want to change $/, the input record separator, before you do so. By default, $/ is a newline which is unlikely to be a meaningful delimiter in your binary data. A null, "\0", is often used to delimit variable length records though. If you have fixed length records or even if you just want to read chunks of a specific length in, you can do that by setting $/ to a reference. For example,

$/ = \16384;
would result in 16Kb block reads. Another useful value to set $/ to for binary data (as well as for text) is undef. Setting it to undef results in the whole file being slurped in a single read.

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re: Re: Re: perl in DOS woes
by scholar (Acolyte) on Nov 05, 2002 at 13:56 UTC
    Oh, I just knew I wasn't being as helpful as I thought.

    "Of course you can use regular file operators on a binmode file" I said as I slapped myself silly reading your reply. "Why, I've done it a hundred times myself!" And I'd forgotten about how useful the input record seperator can be, especially setting it to undef. I think that using

    $/=\<a number>;
    would have made things a lot easier sometimes too.

    I have fallen prey to the fallacy of assuming that my habit is actually a recommended practice. When I'm dealing with a "binary file" I almost always use read, which makes it clear to me later what I was intending to do with it. On that somewhat flimsy notion, I beg forgiveness for misleading comments.

    I have spent a couple of hours attempting to recreate problems I could swear I have had with null bytes (\x0) in files but have been unable to do -- please ignore that part of my answer as well.