Packing and Unpacking in Perl
This post is more to serve as a reminder to myself. Perl has a handy keyword called pack which helps to store a variable in the template specified. pack thus takes two arguments. The first argument is the template and the second argument is the string to be packed according to the template.
Now, a search produces lots of links to various Perl pack/unpack cheatlists. And they are all wonderful quick references to an otherwise complicated option set.
I was intrigued by two aspects of the pack/unpack set. One of the cute little tricks I found was by using pack. Ofcourse, they would be elsewhere on the Wide Wide Web(pun intended). But I found these by myself. So explorers who went before me, forgive for this was an accidental discovery. And I hadn’t known that this was a discovery but a revisit for unbeknownst to the crusaders of other languages, Perl has a lot of accomplices.
The pack Pack :
The follwoing code
prints the decimal value of 10110 as evaluated from right to left. I may find uses for it in the future. But the one thing I could do find it well adapted was in obfuscation. Maybe a brilliant JAPH, anyone ?
The unpack Pack:
The following code
prints the decimal value of each character from the string “Just”. Apart from obfuscation, the real use of pack/unpack functions is to create easily exchangable data.
As we saw in the earlier example, converting from little-endian to big-endian is a trifle in Perl.
If Perl could Perl then Perl would be Perl 6!
Related
Comments
Leave a Reply
Binny V A on 06.26.2009
I have wrote a good amount of perl code – but as of yet, I did not have to use pack. I wonder why.