Suresh Govindachar wrote:
> $Bill wrote:
> >Suresh Govindachar wrote:
> >>
> >> The following code is meant for a Big-endian processor running
> >> linux. The code reads binary files and assigns values to
> >> structures. I would like to read the same data files in perl on
> >> Windows on little endian x86. Any tips on doing the translation?
> >
> > I would just use unpack - you could write your own char-by-char
> > conversion routines if you wanted to (get_long, get_ulong,
> > get_short, etc). Longs and shorts are considered to be 32/16
> > bits and ints are based on build type, but there are native
> > options. Read the pack info in perlfunc man page.
> >
> > sysopen IN, $file, O_RDONLY or die "open $file: $! ($^E)";
> > binmode IN;
Add:
use Fcntl;
which defines O_* defines.
> I had to open the file via:
>
> open (IN, "<:raw", $file_name) or die "Unable to open $file_name for
> reading:$!\n";
> binmode IN;
>
> Perl complained about bareword O_RDONLY -- see reproducible code
> below.
>
> > my $bytes = sysread IN, $data, 4;
> > die "Wrong length data ($bytes): $! ($^E)" if $bytes != 4;
> > printf "data=%d\n", unpack 'N', $data; # convert network order long
> > close IN;
> >
> > # or read the whole struct into $data and unpack it in one call:
> >
> > my ($type, $x, $y, $w, ... ) = unpack 'nNNN...', $data;
>
> Monk ikegami pointed out Convert::Binary::C. This module worked
> after two tweaks:
>
> 1) It did not recognize 'bool'. Determined that size of bool was
> 1 byte and so replaced bool by char.
>
> 2) Total of sizes of members of one structure was 22. C-code
> automatically padded 2 bytes. I had to add a short into the
> structure at the right spot.
>
> But I was having trouble reading the file into scalars to be
> unpacked by Convert::Binary::C -- till I saw "sysread " in
> this post!
>
>
> #!/usr/bin/perl
> BEGIN {(*STDERR = *STDOUT) || die;}
> use diagnostics;
> use warnings;
> use strict;
> $| = 1;
>
> my $file = 'foofile';
>
> sysopen IN, $file, O_RDONLY or die "open $file: $! ($^E)"; # this is
> line 10
>
> __END__
>
> Uncaught exception from user code:
> Bareword "O_RDONLY" not allowed while "strict subs" in use at
> foo.pl line 10.
> Execution of foo.pl aborted due to compilation errors.
--
,-/- __ _ _ Bill Luebkert Mailto:[EMAIL PROTECTED]
(_/ / ) // // DBE Collectibles Mailto:[EMAIL PROTECTED]
/ ) /--< o // // Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_</_</_ http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs