Tried Perl debugger on this code (Was Please help, my brain is fried)

Shay Harding mekla at
Thu Jan 27 20:39:19 CST 2000

> > kev at writes:
> > 
> > << sub create_login_id {
> >      return join('', map $_->[int(rand(52))], 
> >                 (['a'..'z','A'..'Z']) x int(rand(6)+6));
> >  } >>
> > 

I like the above code, but I think my whole question was missed...

I really didn't want to fix it since I could've done that, but I wanted to know
what was wrong with it and why it was not acting as expected. I already had
fixed it with much the same method, although not as cool as the one above :)

Can anyone shed light as to why this code is not working. Has nothing to do
with an 'if' statement or the use of 'while', which by the way will put it into
an infinite loop the first pass through if I don't change:

get_char while ($char >= 91 && $char <= 96);


$char = get_char while ($char >= 91 && $char <= 96);

I'm not even sure why the above is also true? I don't know a lot of the
compiler and how it works, etc. Maybe inside the parens of the first while loop
it goes to 0 (false) or 1 (true) and then the value of $char doesn't matter,
and never changes as in the second example. Just a guess.

I stepped this thing through the perl debugger and I think I came out more
confused than enlightened :)

Basically everything works fine. If 'get_char' is called and it ends up with an
ASCII 93, it correctly recurses and $char is empty. It goes and reassignes
$char (say to ASCII 79) and then gets passed the 'if' block. This next part is
strange in that the debugger tells me it is on the line 'return $char'. I
execute that line and do 'p $char'. It tells me the value of the return is
'93'?? Somehow it is returning the last value of $char. I can see this
happening if I did something like 'local $char' since local just masks the old
value with a new, temporary one. My understanding of 'my' is that the variable
never gets a symbol table entry and thus is not saved anywhere, and is
destroyed upon exit of the current block. Question about Perl recursion... did
I ever exit the 'if' block? Does Perl 'anchor' me in the if block until I
evaluate to false (thereby never destroying the old value of $char) then
reassign the old value and overwrite the current, good value?




  my $count = 0;
  print "\n\n", create_login_id(), "\n";

  sub create_login_id(){
      my $temp = '';

      my $length = int(rand(6))+6;

      for (my $x = 1; $x <= $length; $x++){
          my $t = get_char();
          $temp .= chr($t)."($t)";

      return $temp;

  sub get_char(){

      my $char = int(rand(57))+65;
      print $count, "  ", chr($char), "\n";

      get_char() if ($char >= 91 && $char <= 96);
      return $char;

More information about the Phoenix-pm mailing list