++ for taking unpack and making it nearly readable :-). However, I have to wonder why, if you're writing it anyway, you're joining on spaces, and then splitting on spaces. Wouldn't it be slightly more readable if you did:

my $h = unpack2hash( [ 'l:$songid', 'c8:@signature', 'l:$genre', 'f:$bpm', 's3:@level', 's:$unk', 'l12:@unk2', 'a24:$genres', 'l2:@unk3', 'a32:$title', 'a32:$subtitle', 'a32:$artist', 'a32:$noter', 'a32:$musicfile', 'l:$jpg', 'l3:@unk4', 'l4:@notepos' ], $data);
This would arguably be faster (arguable because even going down that road might be a premature optimisation), but I also just think it cleaner. Of course, if you're doing this anyway, you could also just pre-split a bunch of the text, e.g.:
my $h = unpack2hash( [ [ qw(l $ songid) ], [ qw(c8 @ signature) ], #...
Also think about reversing the order of the parameters, then we could get rid of the array-ref:
my $h = unpack2hash( $data, 'l:$songid', 'c8:@signature', #... );
These are all mere suggestions, so take, or not, as you want - which is why I didn't repeat the previous suggestion to possibly pre-split. Of course, you could also be dynamic and if passed a scalar, split it yourself, but if you're passed an array ref, you could treat it as pre-split. But that would depend on usage.


In reply to Re: Unpacking to hash, with context by Tanktalus
in thread Unpacking to hash, with context by Fox

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.