Eyeth has asked for the wisdom of the Perl Monks concerning the following question:
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:
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.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
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Binary Reading Questions
by Corion (Patriarch) on Jun 30, 2005 at 06:41 UTC | |
by Eyeth (Sexton) on Jun 30, 2005 at 14:06 UTC | |
by idsfa (Vicar) on Jun 30, 2005 at 14:25 UTC | |
by Eyeth (Sexton) on Jun 30, 2005 at 15:57 UTC | |
by BrowserUk (Patriarch) on Jun 30, 2005 at 13:17 UTC | |
by Corion (Patriarch) on Jun 30, 2005 at 14:40 UTC | |
|
Re: Binary Reading Questions
by polettix (Vicar) on Jun 30, 2005 at 10:18 UTC |