some puzzles

Michael Fowler michael at shoebox.net
Tue Mar 20 16:41:11 CST 2001


On Tue, Mar 20, 2001 at 11:22:15AM -0800, Roxanne Reid-Bennett wrote:
> I haven't used sub-scoped subroutines much, so I don't have the internals
> for perl down on that. It seems weird to me that you can even access
> "callsub" outside the while loop.  Based on my understanding (from 30
> years ago) of scoping.
> 
> 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
evaluated before the sub declaration is, such as within a BEGIN block or a
use'd module.

The closest you can get to a scoped subroutine is by using a lexical scalar
and a code reference:

    {
        my $sub = sub { ... };
        $sub->(...);
    }

    $sub->(...);  # no scalar named $sub, abort, abort!


Michael
--
Administrator                      www.shoebox.net
Programmer, System Administrator   www.gallanttech.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



More information about the Anchorage-pm mailing list