Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: forming a word from an array of its bytes

by jcb (Parson)
on Apr 08, 2021 at 23:29 UTC ( [id://11131031] : note . print w/replies, xml ) Need Help??


in reply to Re^2: forming a word from an array of its bytes
in thread forming a word from an array of its bytes

The simple way to understand that combination is that pack converts "stuff" to an octet string and unpack converts an octet string to "stuff" according to template strings. Unpacking with a different template than was used for packing reinterprets the octet string, essentially "type-punning" the data. (Type-punning is a C trick where the element of a C union object that is read is not the element that was most recently written.)

This explanation is how I remember when to use pack and when to use unpack.