develooper Front page | perl.perl6.language | Postings from February 2002

Re: proposal: when-blocks, and binding $_

Thread Previous | Thread Next
From:
Austin Hastings
Date:
February 27, 2002 08:02
Subject:
Re: proposal: when-blocks, and binding $_
Message ID:
20020227160208.49175.qmail@web12307.mail.yahoo.com
It's amazing what a night will do. See bottom.

--- Allison Randal <al@shadowed.net> wrote:
> On Tue, Feb 26, 2002 at 02:20:48PM -0800, Brent Dax wrote:
> > Austin Hastings:
> > #
> > # Which, then, would you like:
> > #
> > # To implicitly localize $_, losing access to an outer version,
> > # or to have to change between implicit and explicit operations?
> 
> Well, I like the idea of having C<when> and the C<s///> operate on
> the
> same thing. But I don't really want C<when> to either localize or
> clobber $_, I want it to leave the information structure alone.
> That's
> why I'd alias $_ at the C<given> or the C<for>, just like I would
> now.
> 

BTW, C<for> doesn't alias $_ always. That's why things like the example
below are possible. 

> > # for @A {
> > #   for @B -> $x {
> > #     when /a/ { s/x/y/; }
> 		s/x/y/;
> > #   }
> > # }
> > #
> > # What should that do?
> 
> Even if we give C<when> aliasing powers, it is still confusing,
> because
> you jump back and forth between the $_ within the C<when> block and
> the
> $_ between C<when> blocks.

Hmm. Suppose we force C<when> to alias $_, but give the coder one
chance to "save" the value:

for @A {
  for @B -> $x {
    when /a/ $_ -> $a { s/a/b/; ... $a ...; }
  }
}

Once we get inside the curlies, $_ is aliased to the localized var for
the C<when> (in this case, $x).

And if you've been sufficiently verbose elsewhere,

for @A -> $y {
  for @B -> $x {
    when /a/ { s/a/b/; ... $y ... ; }
  }
}

there's no need.

=Austin

__________________________________________________
Do You Yahoo!?
Yahoo! Greetings - Send FREE e-cards for every occasion!
http://greetings.yahoo.com

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About