S04 mentions that statement modifiers behave as for perl5 (excpet that you can have both an conditional modifier and a looping modifier on a single statement.

Both then it gives this example, with be modifiers being operators within an expression, not as modifiers of a statement.

  line 260:
  @evens = ($_ * 2 if .odd for 0..100);

I had originally thought that this was a typo: surely those parens shouldn't be there! But on #perl6, Larry suggested that this is for list-comprehension expressions. We don't seem to have any tests for it, and it's not implemented in either Rakudo or Pugs.

Because these are operators within an expression (and not strictly statement modifiers), a question that occurred to me was one of nesting:

  @triangle = ( ( $_ for 1..$_ ) for 1..10 )

Larry suggested this would work if we could rename the loop variable to avoid ambiguity. But S04 seems to say that this is unecessary:

  line 492:
  When used as statement modifiers, C<for> and C<given> use a private
  instance of C<$_> for the left side of the statement.  The outer C<$_>
  can be referred to as C<$OUTER::_>.  (And yes, this implies that the
  compiler may have to retroactively change the binding of <$_> on the
  left side.  But it's what people expect of a pronoun like "it".)

The left-to-right ordering seems to make that unnecessary. To my mind,

  @triangle = ( ( $_ for 1..$OUTER::_ ) for 1..10 )

is confusing because the inner $_ should exist only on the LHS of that inner "for" loop. So perhaps the original should work.

Reply via email to