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

Marty Galyean mgalyean at acm.org
Thu Jan 27 21:53:20 CST 2000


Hi, I'm mostly a lurker, being too busy shoveling myself out of debt,
but this one got me thinking.

It works fine for me with the below as the only alteration to the
original (that someone else previously suggested):

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

You were correct earlier when you wrote that with the recursion you
shouldn't need the 'while'; the 'if' becomes a 'while' via recursion.

Are you sure you have the '$char = getchar()' rather than just the
'getchar()' in what you are executing, I only ask because I notice the
code you last posted doesn't have it.  When I find myself circling a
blind spot I give up and take a nap; pretend I'm giving up on it.  Thats
when it hits ya; or at least you get your life back for awhile.

Rgds,
Marty

Shay Harding wrote:
> 
> > > kev at primenet.com 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);
> 
> to
> 
> $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?
> 
> Shay
> 
> --------------------------------------------------------------
> 
> #!/usr/bin/perl5
> 
>   my $count = 0;
>   print "\n\n", create_login_id(), "\n";
> 
>   sub create_login_id(){
>       srand;
>       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(){
>       srand;
>       $count++;
> 
>       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