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.
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |