# some puzzles

Rick J pisium at yahoo.com
Tue Mar 20 19:41:20 CST 2001

```Hi, Michael and Rox,

Things are getting intersting now.

First, thanks Michael. So the list assignment returns
the number of elements assigned to the leftest scalar
on the left side of the '=' as in \$v1 = (\$v2, @arr) =
(10, 20, 30, 40); and \$v1 is 4. OK.

But here's the thing. Please look at this.

my \$count = 0;
callsub();
while (\$count++ < 3)
{
my \$num = 10;
callsub();
sub callsub {
print ++\$num, "\n";
}
print "\$num\n";
}
It prints out 1 11 11 12 10 13 10

We first call callsub outside the while loop, and 1 is
for sure. Then in the while loop, \$num's assigned 10
outside sub block, and then increment by 1, that's
second one - 11, then third one - 11 is after sub
block.
Now we are in the second loop, all the values are
supposed to be discarded after first loop's done,
right? But where's 12 from? I can deduce it easily
from 11+1, but why when we print \$num again outside
sub block, it became 10 again?, and same thing happens
in the third loop, 13 and 10.
Why we can change the \$num from outside sub (as 11/11
case), and can't carry the value inside sub
outside(12/10 and 13/10)? Is it due to the elfish
closure again?

Sigh...

Thanks,
Ricky

--- Roxanne Reid-Bennett <rox at tara-lu.com> wrote:
> Michael Fowler wrote:
> > >
> > > However, running this through the Activestate
> debugger, it does in fact
> > > run through the callsub 3 times before executing
> the \$num = 10.
> >
> > Perl doesn't have scoped named subroutines, the
> sub in the block is a bit
> > misleading (and probably part of the source of his
> confusion).  When perl
> > sees a named subroutine declaration the subroutine
> is available almost
> > anywhere.  The only place it wouldn't be available
> is in code being
>
> aha, ok.
>
> it didn't help that I read the following:
>
> > my \$count = 0;
> > callsub() while (\$count++ < 3);
> > {
> ...
> > }
>
> as (re-arranged a little bit to make my point)
>
> > my \$count = 0;
> > callsub();
> > while (\$count++ < 3)
> > {
> ...
> > }
>
> Makes a bit of a difference [and teaches me once
> again to read the code as the
> compiler does, not just read what I'm seeing and
> make allowances for other
> people's formatting ...]
>
> I had also totally forgotten the distinction between
> arrays and lists.
>
> Thanks Michael.
>
> Rox
> --
> Roxanne Reid-Bennett
> rox at tara-lu.com
> President, Tara-Lu Corporation
> http://www.tara-lu.com/
>
> Quality brings its own reward.
> =================================================
> Mailing list info:  If at any time you wish to
> (un|re)subscribe to
> the list send the request to majordomo at hfb.pm.org.
> All requests
> should be in the body, and look like such
>                   subscribe anchorage-pm-list
>                   unsubscribe anchorage-pm-list

__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail.
http://personal.mail.yahoo.com/
=================================================
Mailing list info:  If at any time you wish to (un|re)subscribe to
the list send the request to majordomo at hfb.pm.org.  All requests
should be in the body, and look like such
subscribe anchorage-pm-list
unsubscribe anchorage-pm-list

```