in reply to Best way to represent somewhat large values in a module

You say you are converting to hex but then go on to say And using pack and unpack makes them unprintable, which is annoying.

Why not just treat them as strings (these will typically be unprintable) and upack them into hex as requrired for printable output. You can use bitmap operators on strings (with care)

my $num = "\000\001\002\003\004\005\006\007\010\011\012\013"; my ($bin) = unpack "B*", $num; print length $bin, ": ", $bin, "\n"; print printable($num), "\n"; $num |= "\000\001\002\003\004\005\006\007\010\377\012\013"; print printable($num), "\n"; sub printable { unpack "H*", $_[0] }

cheers

tachyon

Replies are listed 'Best First'.
Re^2: Best way to represent somewhat large values in a module
by sgifford (Prior) on Jun 19, 2004 at 10:11 UTC
    Thanks for your thoughts, tachyon.
    You say you are converting to hex but then go on to say And using pack and unpack makes them unprintable, which is annoying.
    Right, I meant that's why I'm converting to hex instead of using pack and unpack. Those whole-string bit operations look pretty tempting, though...Perhaps the right thing to do is only convert to and from hex when I get data from the user, and store the data as bitstrings, using code like the above. Hmmm...

      There are some 'gotchas' when you do $str BIT_OP $str but for things like masks you should be OK. Sometimes it works like you expect, other times not. It is fairly magical that it works at all!

      cheers

      tachyon