in reply to Help me decipher code containing map function
I thought that the map function takes in a list(perhaps an array), I do not see the above code having a list passed to it.
unpack outputs a list. In this case it extracts a list of 6 numbers (ascii values) from the (first) 6 bytes in $octetstr.
It then uses map to iterate over those 6 numbers converting them to 2-digit hex strings and appending them to $val
Before spliting them (the 2-digit hex strings) back apart and then joining them again interspersed with ':'s.
It is an altogether overcomplicated and clumsy piece of code. The whole process can be done with just $mac = join ':', unpack '(H2)*', $octetstr;
Ie. Given:
$octetstr = join '', map chr(), 0xab, 0xcd, 0x00, 0x1d, 0x94, 0x56;;
Instead of:
val = ''; map { $val .= sprintf("%02x",$_) } unpack "CCCCCC", $octetstr;; $mac = join( ":", unpack("a2 a2 a2 a2 a2 a2", $val ) );; print $mac;; ab:cd:00:1d:94:56
You could just do:
$mac = join ':', unpack '(H2)*', $octetstr;; print $mac;; ab:cd:00:1d:94:56
|
|---|