Your skill will accomplish what the force of many cannot |
|
PerlMonks |
Unpacking to hash, with contextby Fox (Pilgrim) |
on Feb 10, 2010 at 12:18 UTC ( [id://822412]=CUFP: print w/replies, xml ) | Need Help?? |
When we got a lot of values to unpack it's a easy task, since perl allows you to concatenate the templates and unpack then in one call. But not so lucky when some of the values need list context. Searching through perlmonks I founded this thread which was almost what I needed, except it unpacks everything in scalar context, so I modified it a bit and here we are: the template syntax is a bit different, see the example: each value is separated by whitespace, in the format <template>:<context><key> this way, the values with @ will be unpacked in list context and the values with $ will be unpacked in scalar context, and stored in the hash with its respective key. UPDATE: I changed the line $source =~ s/$pack//; to substr $source, 0, length $pack, ''; because $pack was being interpreted as a regex.
Back to
Cool Uses for Perl
|
|