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

Hey all. I'm writing a passwerd session handler (over CGI of course) and I want to generate big randoms as the key for each open transaction to maintain server-side session status info without embedding static security info within printed HTML. It's not a big deal but the first thing I tried was:
my $rndm = sprintf "%lx", int rand(16**8); #why only 4hex digits when num is 8?
I've printed 16**8 and gotten ~4billion which is 2**32. Are numerics maxed at 4bil unless you use Integer Modules for long text math? I'm using this now which werks fine but It's not as straight forward as the first.
my $rndm = int rand(16**4); my $sess = sprintf "%lx", $rndm; $rndm = int rand(16**4); $sess .= sprintf "%lx", $rndm;
Thanks for any insight. TTFN & Shalom.

-PipTigger
p.s. Is there a reason why not to have a "dec" function to be the reciprocal of "hex"? Why does ProgrammingPerl use sprintf to convert hex->dec instead of a function which seems cleaner?

Replies are listed 'Best First'.
Re: Why doesn't this give an 8 character hex number?
by princepawn (Parson) on May 19, 2000 at 22:24 UTC
    have you seen the CPAN MODULE Apache::Session? It should do what you need.
RE: Why doesn't this give an 8 character hex number?
by turnstep (Parson) on May 19, 2000 at 22:57 UTC

    > p.s. Is there a reason why not to have a "dec" function
    > to be the reciprocal of "hex"? Why does ProgrammingPerl
    > use sprintf to convert hex->dec instead of a function which
    > seems cleaner?

    'Dec' is the default - it's what most of us are used to. Hex and oct are not that way that people learned to count, so having some convienence functions make sense. I think that sprintf is pretty clean myself :) but to each their own. If it really bothers you, why not just create your own "dec" function (subroutine)?

RE: Why doesn't this give an 8 character hex number?
by beppu (Hermit) on Jul 07, 2000 at 10:18 UTC
    Try doing:
    sprintf("%08x", $some_number);
    
    consult 'man printf' for more details