On 21 Sep 2002, Smylers wrote:
> 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.
>
> 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.
Well, no.
sub process;
for <> -> $line {
# both $line and $_ are the current line here
given process $line {
# $_ is process $line; $line is, well, $line
}
}
I don't think this is confusing. $_ is I<always> the current topic. If
you have a C<given>, you have a different topic. Like the Apocalypse
says, you alias the outer ones, not the inner ones.
I used C<for>, because C<while> doesn't topicalize.
> 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
>
Luke