in reply to perl in DOS woes

DOS uses 1AH (^Z) to indicate eof for a text file -- which means that if the value exists somewhere in your file, you can't use text file semantics to read it. You'll get the same sorts of problems if there are any null bytes in the file. What I usually do in this situation is:
open "< FH"; binmode FH;
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 would need to use the read function to get chunks of data from the file and then parse out the resultant buffer. Something like:
while (read(FH, $buf, 512)) { do something; }
Oh ... and thanks for finding a question I could answer so soon ... My apologies if this first attempt is not as helpful as it might be :)

Replies are listed 'Best First'.
Re: Re: perl in DOS woes
by sauoq (Abbot) on Nov 05, 2002 at 01:33 UTC
    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.";
    
      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.