[Wellington-pm] Pack/unpack - nagging guilt

Ewen McNeill ewen at naos.co.nz
Wed Jun 22 03:11:57 PDT 2005

In message <1119433075.4455.29.camel at localhost>, Grant McLean writes:
>I recently had a need to take a string like this:
>  #FFFFCCCC3333
>and turn it into a list of three integers:
>  (65535, 52428, 13107)

my @rgb = unpack("n*", pack("H*", substr("#FFFFCCCC3333", 1)));

Possibly that's the solution that you came up with, and then rejected.

Without benchmarking it I'd expect the unpack(pack(substr())) approach
to be marginally faster than the map { hex } regex approach.  But
probably not enough to justify choosing one or the other unless you're
doing it an awful lot.  And in either case the subroutine call overhead
(you would hide this in a subroutine, right) would probably be higher.

And the unpack(pack(substr())) will fail in a different manner to the
map { hex} regex approach, as well as being specific to 4-character hex
values.  (Well you could do 8-character hex values (N*) or 
2-character hex (C*).)


More information about the Wellington-pm mailing list