on boston.pm a thread arose about having named subs inside subs. of
course perl5 can do it but they don't do anything useful but they do
have some odd implemenation defined closure behavior.

someone brought up lisp and scheme and how they do it (differently from
each other).

well, i want to dredge up how PL/I did it and i think it makes for a
useful concept.

my $c ;

sub foo {

        my $a ;
        my $b ;

        blah

        bar() ;

        sub bar {

                $b = $a + $c ;
        }
}

first point is that bar is NOT a closure. it makes no copies of $a and
$b into a pad. bar uses the actual $a and $b of foo on the stack.  it
should do work the same for foo's params.

the lexical scope of foo covers bar so it will only see outside vars if
foo doesn't declare them. bar sees the outer $c just fine. bar can't be
seen outside of foo as well.

internally all that is needed (and this is how we did in in PL/I) is to
pass a stack frame reference as a hidden argument to bar(). bar will
need to be declared with this hidden extra var or the internals will have to
pass it some nonstandard way.

bar can be recursive and it will not get fresh copies of $a and $b. it
can declare its own lexicals and they DWYM in recursion.

so there is basically no syntax changes at all. just a compiler feature
to recognize internal named subs and use those semantics.

also we can say child subs can't have child subs inside them but that
isn't required. i am sure one of you can come up with a coding need for
that. :)

any thoughts?

uri

-- 
Uri Guttman  ------  [EMAIL PROTECTED]  -------- http://www.stemsystems.com
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org
Damian Conway Perl Classes - January 2003 -- http://www.stemsystems.com/class

Reply via email to