while (!eof(DATFILE))

sub _read { my ($fh, $bytes_to_read) = @_; my $buf = ''; while ($bytes_to_read) { my $bytes_read = read($fh, $buf, $bytes_to_read, length($buf)); if (!$bytes_read) { die "Error reading: $!\n" if !defined($bytes_read); die "Unexpected EOF\n"; } $size -= $bytes_read; } return $buf; } sub read_uint32 { my ($fh) = @_; unpack('N', _read($fh, 4)) } #Or V? sub read_uint16 { my ($fh) = @_; unpack('n', _read($fh, 2)) } #Or v? sub read_pstring { my ($fh) = @_; _read($fh, read_uint16($fh)) } { open(my $fh, '<:raw', $datfilename) or die $!; read_uint32($fh); while (!eof($fh)) { my $profile_id = read_uint32($fh); my $seq1 = read_pstring($fh); my $seq2 = read_pstring($fh); read_uint16($fh); ... } }
Or if the "trailing zero" is meant to indicate the end of a list of sequences,
{ open(my $fh, '<:raw', $datfilename) or die $!; read_uint32($fh); while (!eof($fh)) { my $profile_id = read_uint32($fh); my @seqs; while (length( my $seq = read_pstring($fh) )) { push @seqs, $seq; } ... } }

In reply to Re: while behaviour on binary files by ikegami
in thread while behaviour on binary files by Anonymous Monk

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.