some puzzles
Scot Ridgway
scot at ridgways.org
Tue Mar 20 23:53:11 CST 2001
Rick,
In your example callsub() is available globally, though you can only
modify/access $num within the scope $num and callsub() were created....
like-a-so:
{
# scope one...
my $num = 10;
globally_accessible_sub();
# prints 1, undef + 1
}
# scope two...
my $num = 10;
globally_accessible_sub();
# prints 2,
while ($count++ < 1) {
# scope three...
my $num = 10;
# while{} scope
globally_accessible_sub();
# prints 11
sub globally_accessible_sub { print ++$num, "\n" } # we can
modify $num in this scope
$num = 20;
# as we see below...
globally_accessible_sub();
# prints 21
}
# scope two, again...
$num = 30;
# but we can't access $num here...
globally_accessible_sub();
# prints 22
Also you may have noted your were resetting $num to 10 within the while
block via my $num = 10.
--------------------------
--scot
----- Original Message -----
From: "Rick J" <pisium at yahoo.com>
To: "Roxanne Reid-Bennett" <rox at tara-lu.com>
Cc: <anchorage-pm-list at happyfunball.pm.org>
Sent: Tuesday, March 20, 2001 4:41 PM
Subject: Re: some puzzles
> 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.
>
> I know about closure.
>
> 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
>
=================================================
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
More information about the Anchorage-pm
mailing list