Can anyone explain the rules of placeholder attachment?  i.e., in the
example in Perl6::Placeholder's manpage,

  grep { $data{$^value} } 1..10;

C<$^value> is clearly intended to attach to the outer closure C<{
$data{$^value} }>, not the inner closure C<{$^value}>.  But how does the
compiler know?  What is the general rule?

It's easy to just say "don't nest placeholder-using closures," but that
doesn't seem workable in practice since every block is a closure, unless
placeholders are forbidden from all but the most trivial cases.  Absurdly
trivial, it seems.  How about

  $sub = { if $^a { $^b = $^a } };

?  Are there two C<$^a>'s, one masking the other?  Or just one?  If two,
then the code should fail at runtime for attempted assignment to an
undefined lvalue (because $^b isn't set) or more likely at compile-time
for a missing required parameter.  If there's just one C<$^a>, wouldn't
C<$^b> get set to the topic of the C<if>, i.e. C<$^a> (or is the topic of
an C<if>'s first closure always C<true>?), leading to the expression being
equivalent to

  $sub = ( if $^a { $^a = $^a } };

?  Or will Perl DWIM and attach both $^a and $^b to the outer sub?  If so,
how did it know to do that, and not attach it to whatever sub contains the
assignment to $sub?

I'm probably just confused here, but I'd appreciate some straightening
out, as it's relevant to something I'm working on.  Apo 6 seems to be
silent on this (which perhaps indicates that I'm making this a lot harder
than it is).

Trey

Reply via email to