I'm writing code to write data to a well-specified binary format, and also to read it back in. It's pretty simple to recursively serialize everything. Now, I'd also like to make an annotated hex dump of the file. That is, I don't just want to de-serialize the data and call Dumper on the meaningful structure. Rather, I want to show the original bytes and offsets along side the parsed interpretation.

I'm thinking that the code that reads should also do the dumping.

So, how can I do that kind of code reuse?

Normally, a function will take a file handle and consume some bytes and return some object that is the re-constituted meaning of what it read.

My stream isn't necessarily a real file, so I can't assume I can back up; so noting the before position, calling the reader, then rewinding and reading the same bytes to dump in hex is no good. Also, it's recursive and I want to dump each primitive this way, not the entire finished structure.

I really need to remember the offset and bytes content along with the interesting value being returned.

Perhaps have every such reader function return two results as a list, the reconstructed object and the report. When one reader calls subreaders, it adds the breakheads to the report, and catenates (after indenting) the reports from the nested calls.

Or, perhaps have the report attached to the object read and returned, as a property of some kind. A recient discussion convered assigning properties, but it required XS. Either that or always return Perl objects, never simple numbers or strings(yech?).

Or have a global variable for the report. Everyone appends to that as the last step before returning. Is that a double-yech? I think it's doable since I don't have backtracking.

—John


In reply to Parse and Dump from Same Code by John M. Dlugosz

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.