Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Unary dot and custom control

4 views
Skip to first unread message

Luke Palmer

unread,
Sep 20, 2004, 9:25:14 AM9/20/04
to Language List
I came across this slight annoyance working in Perl 5 today:

sub preserve(&) {...}

sub foo {
preserve {
$_[0]->bar;
}
}

That didn't call "bar" on the invocant of "foo", but rather on "undef",
because preserve's block was a hiding sub.

Perl 6 is making it easier to define custom block constructs like this
one. I worry about:

method foo () {
preserve {
.bar;
}
}

This one's a tad more subtle. Normally preserve's block would be 0-ary.
But in this case it's unary, since it implicitly references $_.

Is there a way to declare preserve so that its block will always be
parsed 0-ary? "if" is certainly able to do this (consider the above
example with s/preserve/if $baz/).

Luke

Larry Wall

unread,
Sep 20, 2004, 11:33:34 AM9/20/04
to Language List
On Mon, Sep 20, 2004 at 07:25:14AM -0600, Luke Palmer wrote:
: Perl 6 is making it easier to define custom block constructs like this

: one. I worry about:
:
: method foo () {
: preserve {
: .bar;
: }
: }
:
: This one's a tad more subtle. Normally preserve's block would be 0-ary.
: But in this case it's unary, since it implicitly references $_.
:
: Is there a way to declare preserve so that its block will always be
: parsed 0-ary? "if" is certainly able to do this (consider the above
: example with s/preserve/if $baz/).

Such a block is parsed as 1-ary, but with an implicit argument that
defaults to the caller's $_, something like:

preserve sub ($_ = $CALLER::_) {
.bar;
}

Then it effectively becomes a run-time decision by "preserve" whether
to treat it as 0-ary or 1-ary.

Larry

0 new messages