Larry Wall wrote:
> On 20 Sep 2002, Aaron Sherman wrote:
>
> : Does that mean that I can't
> :
> : for $x -> $_ {
> : for $y -> $z {
> : print "$_, $z\n";
> : }
> : }
> :
> : And expect to get different values?
>
> That's correct. Name the outer topic explicitly, not the inner one.
That makes sense for nested C<for> loops: avoid confusion by always
having the 'unlabelled' entity being the thing being dealt with right
now.
Is it as helpful for file input? Many Perl programs use C<$_> to mean
'the current line'. 'A2' gives the Perl 6 syntax for this as:
while $STDIN {
That C<while> loop may be quite long. Maybe somewhere in the middle of
it, it's necessary to have a C<for> loop iterating over something else.
Or we want to switch on some state:
given $who
{
when $us { push %line{us}, $_ }
when $them { $num_them++ }
default { warn "$_\n" if $DEBUG }
}
I think it could surprise people if the variable holding 'the current
line' no longer holds that inside the C<for> or C<given>.
Having to alias C<$current_line> to C<$_> around inner loops and
switches is messy, and potentially confusing -- the same value now has
different names in different places.
Presumably something like this will be valid to use a different name for
the current line:
while defined my $line = $STDIN {
By C<$line> is just an ordinary variable in there, not a topic. So we
lose the benefits of being able to perform matches and substitutions
directly on the current line without having to name it or type the C<=~>
operator explicitly.
So I'm unconvinced that having an explicitly named topic always also
clobbering C<$_> is a good idea. But if it is, then we need a simple
syntax for reading file input lines into an explicitly named topic.
Smylers