Please help, my brain is fried

Mark Pease Mark.Pease at
Thu Jan 27 13:43:51 CST 2000

> Beaves at wrote:
> << sub create_login_id {
>      return join('', map $_->[int(rand(52))],
>                 (['a'..'z','A'..'Z']) x int(rand(6)+6));
>  } >>
> Could you expand this code and put in a few comments about what it is doing.
> I like the conciseness, but I'm not exactly following what each section is
> doing...

Let's split out the sections in the order that they are executed:

# Pick the number of characters to have in the login id. It must have
# at least 6 and no more than 12 elements.
$id_length = int(rand(6)+6);

# Build an array where each element is a reference to another array
# that contains all of the upper and lower case letters (52 elements in
# (The 'x' operator, in array context, will build an array where each
# element is the same.)
@passwd = (['a'..'z','A'..'Z']) x $id_length);

# Now pick one letter from each array reference.
# (Map will execute a chunck of code on each element of an array,
# and return an array where each element contains the value returned
# from the chunck of code.)
@passwd = map $_->[int(rand(52)], @passwd;

# The array now contains the login id, one character per element.
# Join all of the character together into one string, and return.
return join '', @passwd;

> (the classic 'brevity versus understandability' dillemma)

No dillemma, understandablility should always win over brevity.

Mark Pease                                   Mark.Pease at
Motorola DigitalDNA(tm) Laboratories             perl at
2100 E. Elliot Rd.     Phone:(602)413-8191       Mail Stop: AZ34 EL701
Tempe, AZ 85284        Pager:(800)381-3304           FAX:(602)413-8183
   Co-Author (with Carl Dichter) of "Software Engineering with Perl"

More information about the Phoenix-pm mailing list