in reply to Re: Reading binary file in perl having records of different length
in thread Reading binary file in perl having records of different length
Thanks for the snippet. Yes that is an option I am exploring as well. Since I don't have a production grade binary yet (application is still being developed) I only have a binary file with 5 records (so can't test the performance benefit).
What I am doing is creating a separate parsing subroutine which would expect one record at a time for parsing, so how I read is independent of parsing. I am putting a user defined choice at run time if the user wants to read the binary in slurp mode or byte mode. This is what I have for slurp mode:
#!/usr/bin/perl use strict; use warnings; use Fcntl qw(:seek); use Data::Dumper; open my $fh, '<', 'Test.NEW' or die "File not found: $!"; binmode($fh); my $data = do{ undef $/; <$fh> }; my @data = split /(?=3d3d)/, unpack ('H*',$data); for my $xdr (@data) { open (my $fh1, '<', \$xdr); # create a filehandle from sc +alar read ($fh1, my $buffer, 2); # read the eye catcher read ($fh1, $buffer, 2); # read the length my $length = unpack 'n', $buffer; # identify the length in decimal seek $fh1, 0, 0; # reset the offset read ($fh1, $buffer, $length); #read till the length to preve +nt garbage bytes process($buffer); } #print Dumper \@data;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Reading binary file in perl having records of different length
by johngg (Canon) on Jun 18, 2014 at 10:18 UTC | |
by sundialsvc4 (Abbot) on Jun 18, 2014 at 21:12 UTC | |
by jaypal (Beadle) on Jun 19, 2014 at 01:32 UTC |
In Section
Seekers of Perl Wisdom