tritt has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Is there a nicer way of doing this?

use constant XXX => 0xcccc; ($self->{auth_mode}, $val) = unpack('H4a*',$val); $self->{auth_mode} = hex($self->{auth_mode}); given ($self->{auth_mode}) { when (XXX) {do_smthg;} }

Thanks in advance :)

Replies are listed 'Best First'.
Re: Nicer hex-unpack
by Corion (Patriarch) on Jul 12, 2011 at 09:05 UTC

    Why convert from hex strings to a number when in the end you compare anyway? Unless you use fancy bitmask arithmetic, just stay with strings:

    use constant XXX => 'cccc'; $self->{auth_mode} = lc $val; given ($self->{auth_mode}) { when (XXX) { ... } };

    Note that the change from numeric to string comparisons might prove problematic if you have multiple locations where you check for equality with == (and no test suite that smokes out such situations).

Re: Nicer hex-unpack
by TomDLux (Vicar) on Jul 14, 2011 at 19:33 UTC

    I would use Readonly my $XXX => 0xcccc; rather than use constant, and of course equivalent numbers are equivalent without needing explicit conversion.

    What is it you find clumsy?

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.