I would not recommend reading whole file into memory since you said it would be big binary file :) I would have classes for each type MainHeader, ExtensionHeader, PartOne, PartTwo, etc. Each class constructor would take binary data as parameter.

When you say each section has a field that knows length, does it mean it is Tag-Length-Value? https://en.wikipedia.org/wiki/Type-length-value in that case, you could have main loop in parser class and pass each record binary chunk(from length) to appropriate class based on tag.

Parser class would be something like iterable; where client invoke next()/ (or ->() in perl land) method to advance/get next record from file.

#pseudo code # error handling omitted! sub parser_factory { my $file_path = shift; my %options = @_; fh = open_file($file_path) my $iterator = sub { # closure ; flag end of data/closing file omitted while( header = read(fh)) { length = get_length(header) body = read(fh,length) type = get_type(header) class =get_record_class(header) # return class name return class->new(body); } } return $iterator }

In reply to Re: Best way to a parse a big binary file by pwagyi
in thread Best way to a parse a big binary file by Dirk80

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.