I'm working with a data file that has a published format in a software manual; it's a binary file with a well-defined format. While I've been able to interprete and write out to this format, I've been scratching my head over a few of the issues with it.

Basically, the code that I'm using is of the following (in this case, I'm reading a header consisting of 50 integers and a 150-char string):

open FILE, "<$file" or die $!; binmode FILE; my $line; read FILE, $line, 50*2 + 150*1; my @header = unpack( "s50c150", $line ); $string = join '', map{ chr($_) } @header[50..149]; # etc, etc...
Now, I've got two questions on this:
  1. The manual that describes this format says that the integers are "4 bytes", with "1 byte" characters. Yet, when I initially ran this code, I only got it to work right by reading in 2 byte integers (the calculation in the read line). I'm pretty sure this program is 32-bit based, and it has (and continues to be) updated today. Could that just be a typo in their documentation, or is this something to do with perl and system dependant byte sizes? (The program is based on Windows, I'm using it on WinNT, with perl 5.005).
  2. It seems silly to have to define the size that I want to read, then unpack what I need. It would seem trival that given an unpack string, one could determine the number of bytes that one needs to read from the file, sortof something like:
    read FILE, $line, sizeof("s50c150"); unpack( "s50c150", $line );
    where sizeof() is this hypothetical function. Is there already something available in perl, or a better way to combine these reads and unpacks?

-----------------------------------------------------
Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
"I can see my house from here!"
It's not what you know, but knowing how to find it if you don't know that's important


In reply to read() and unpacking, data size issues by Masem

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.