Garrett Rooney writes:
> I'm having some trouble using the &?SUB variable in a subroutine 
> declared with the -> operator.  The following code results in an error 
> about &?SUB being undefined:
> 
> my $s = -> $count {
>   if $count < 10 {
>     say $count;
>     &?SUB($count + 1);
>   }
> };
> 
> $s(1);
> 
> If I change to either a named sub (sub foo($count) { ... }), or an
> anonymous sub declared with the sub keyword (my $s = sub ($count) {
> ...  } ) then it works fine, it's only with the -> that it doesn't
> work.

That's because a pointy sub is not a sub.  Perhaps we should call it a
pointy block.

Not all code objects are Subs.  If you call "return", then you return
from the innermost enclosing "sub", which is marked by that word.
Likewise does $?SUB.  I don't believe $?BLOCK has been implemented yet,
but it will.

To really illustrate the point, try this program:

    sub foo ($ct) {
        say "foo";
        my $s = -> $count {
            if $count < 10 {
                say $count;
                &?SUB($count + 1);
            }
        };
        $s($ct);
    }
    foo(1);

Luke

Reply via email to