On Sat, Sep 21, 2002 at 10:05:50AM -0000, 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.
> 
> 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>.

I don't see what is different from perl 5.

> 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 {

while $STDIN -> $line {

or

for $STDIN -> $line {

as the input should be read lazily.

> 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.

As I wrote it $_ is the same as $line until some other construct claims
the $_.

I think that if your loop is so long that you are having trouble keeping
track of what $_ refers to then you probably need to explicitly name it
anyway, and maybe put some of the loop in a sub too.

> 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.

Allison Randall gave a really good talk on this at YAPC::Europe.  I
think she gave it at YAPC::NA too.

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net

Reply via email to