in reply to Session ID Generator (Rolled My Own)

Just a few points.

It's no longer necessary to seed the RNG with srand, that has been automatic for the last few versions of Perl.

The @Chars array can be initialized with the list range operator:

my @Chars = '0'..'9', 'A'..'Z', 'a'..'z';

I'd rename $l so it doesn't look like the first captured match builtin, $1. It could also be generated by

$l = join '', map {$Chars[rand @Chars]} 1..7*($ARGV[0] ||= 1);

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: Session ID Generator (Rolled My Own)
by sauoq (Abbot) on Sep 15, 2002 at 04:21 UTC
    my @Chars = '0'..'9', 'A'..'Z', 'a'..'z';

    That won't work as you might expect. You'll actually need:

    my @Chars = ('0'..'9', 'A'..'Z', 'a'..'z');

    Those parens aren't optional. Here's a demonstration:

    $ perl -le 'my @one="0".."9","a".."z"; my @two=("0".."9","a".."z"); pr +int @one; print @two' 0123456789 0123456789abcdefghijklmnopqrstuvwxyz
    -sauoq
    "My two cents aren't worth a dime.";
    

      Right you are. I'm trying to understand that in terms of operator precedence. I think my erroneous one parses as: ( my @Chars = '0' .. '9'), 'A' .. 'Z', 'a' .. 'z'; sauoq++ for the catch.

      After Compline,
      Zaxo

      Thanks for pointing that out sauoq!
      I would certainly never figured out the problem from the symptom!
      jg
      _____________________________________________________
      "The man who grasps principles can successfully select his own methods.
      The man who tries methods, ignoring principles, is sure to have trouble.
      ~ Ralph Waldo Emerson
Re^2: Session ID Generator (Rolled My Own)
by Flexx (Pilgrim) on Sep 15, 2002 at 01:36 UTC

    Hi Zaxo!

    A small glitch in this. The equivalent @Chars range would be:

    v my @Chars = ('a'..'z', 'A'..'Z', '2'..'9'); ^
    Also, I think the $1 (one) you read, is actually a $l (ell)... ;)

    So long,
    Flexx

    Update: Added parens, ++sauoq