From your code, your files have a a 66-byte header; 0 or more 222-byte data sections; and a 69-byte trailer. And you know when you've read the trailer record because the first byte is 'T'.

But, you unpack the data to hex before checking if the first 2 bytes are the hex for 'T'.

If it is, you then passed the hexified data into print_trailer(), where you unpack it again. Twice! Once as TEXT ('A*'); once as HEX ('H*') again!

You then attempt to process those, conditionally upon a bunch of mystery variables, sometimes accessing the ASCIIfied hex; and sometimes the HEXified hex. I suspect that at the very least you should be passing the raw data (not its hexified representation) into print_trailer().

And that then calls into question, why are you hexifying it in the first place, if all you want to do is test if the first character is 'T'? A simple if( $data =~/^T/ ) { ... would do that nicely, and save you having to substr 2 bytes of the hex and then convert those back to ascii.

And you're doing yourself no favours by using variables (package globals) within your subs that are not passed into them via the parameters. And by not showing the full script, or at least a runnable cut-down of it, you make life neigh impossible for us to help you beyond the above observations.

That's why I prefer to put my subs at the very top of my scripts where it's impossible to accidentally create closures.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"I'd rather go naked than blow up my ass"

In reply to Re: How to read a binary file with file header, data and file trailer ? by BrowserUk
in thread How to read a binary file with file header, data and file trailer ? by bh_perl

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.