Hello.

I'm relatively new to Perl programming (appx. 1 month) and my only book (so far!) is the B&N $9.95 Perl book. I try to rely on existing (Internet) documentation for the bulk of all things Perl on my end.

My main question revolves around Perl's binary file handling. I still have a queasy feeling whenever I try to open a binary file under Perl. I use Perl v5.8.5 on FC3 Linux. Here's a code snippet:

open (FHDLR, "< :raw", "$fname") or die ("Cannot open $fname!\n"); # Opens the file in binary mode. seek (FHDLR, 258, 0); # zero onto FORMAT_TYPE of partition $ftype = ord (getc (FHDLR)); # just get the FORMAT_TYPE variable
When I first started, I thought I could use a C-language 'Jedi' trick by using the getc function to get an individual (unsigned) char and treat it as a 8-bit binary value for $ftype. Needless to say, I found out that it didn't work.

Thinking that I had to treat it as a pure binary file, I added the "< :raw" argument to the open function, and the getc function still didn't work, as Perl insisted on wedging in an actual character for $ftype.

I finally found the ord function and put that thing before the getc function call. This time, it worked, as Perl put in a 8-bit binary (unsigned) integer into $ftype.

So, I've got it working now, but the whole experience has led me to some middling doubts about how Perl handles binary files. Just how does Perl buffer file reads? What about writes, do I have to 'reconvert' these values via the chr function calls?

I can't afford for Perl to munge a binary file by treating it as an ASCII file. My other concern is that by eventually overusing the ord/chr functions in many parts of my Perl script, I may actually slow the script down. I'm also using the read function to read in 256-byte chunks of data into a superarray. Even then, the 256-byte chunks are full of characters, printable and non-printable. So far, the script seems 'Snappy(tm)'.

I was hoping to use Tk along with my initial Perl coding efforts. I'd hate to grapple with the complexities of C language programming and GTK+ programming.

Enjoy.


In reply to Binary Reading Questions by Eyeth

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.