[pugs] &?SUB and pointy subroutines

6 views
Skip to first unread message

Garrett Rooney

unread,
Mar 4, 2005, 8:19:07 PM3/4/05
to perl6-c...@perl.org
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.

Is there something wrong with my code, or is this an actual bug?

-garrett

Garrett Rooney

unread,
Mar 4, 2005, 8:37:47 PM3/4/05
to Luke Palmer, perl6-c...@perl.org
Luke Palmer wrote:

> 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

Luke Palmer

unread,
Mar 4, 2005, 8:46:56 PM3/4/05
to Garrett Rooney, perl6-c...@perl.org
Garrett Rooney writes:
> >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.

Okay, it is now mentioned (I think dev.perl.org is synched nightly).

Luke

Luke Palmer

unread,
Mar 4, 2005, 8:32:50 PM3/4/05
to Garrett Rooney, perl6-c...@perl.org
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 all
Reply to author
Forward
0 new messages