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.
Is there something wrong with my code, or is this an actual bug?
-garrett
> That's because a pointy sub is not a sub. Perhaps we should call it a
> pointy block.
That might be clearer ;-)
> 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.
If that's the case then the "Pointy subs" section of S06.pod should
probably be clarified a bit. It says that -> is a synonym for an
anonymous C<sub>, except for the lack of parens around the parameter
list, lack of a need for a preceeding comma when included in a list, and
the fact that it cannot be given traits. If there are more differences
they should probably be mentioned.
> 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);
Ok, I see how it works now, but I still think the docs should be
clarified a bit.
-garrett
Okay, it is now mentioned (I think dev.perl.org is synched nightly).
Luke
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