Think about Loose Coupling | |
PerlMonks |
Perlish approach to parsing a binary blobby mxb (Pilgrim) |
on Apr 09, 2018 at 12:21 UTC ( [id://1212579]=perlquestion: print w/replies, xml ) | Need Help?? |
mxb has asked for the wisdom of the Perl Monks concerning the following question: Hi Monks. I'm currently improving my Perl by practising Perl idioms and trying to write more 'Perlish' code. I'm currently practising by parsing a binary blob with unpack. My input is a scalar, and my desired output is an array of arrays. As I'm from a C background my initial approach is to use the C-like for loop and I end up with code like the following:
While it works, it seems unnecessarily verbose for Perl and I'm aware I'm trying to write C in Perl. Therefore, I've been attempting to rewrite the code in a more Perlish manner. I've ended up with the following:
I'm happy with the map { unpack ... } ... construct as this is clear and concise, but I'm a little less sure about the first unpack to split the $data scalar into the list of items. Therefore I'm deferring to the wisdom of the Monks, is there a better way to achieve what I am doing? Maybe the approach should be a single unpack "(CA4)*", $data and then rebuilding the child lists?
Back to
Seekers of Perl Wisdom
|
|