Graham Barr writes:
: On Mon, Apr 23, 2001 at 11:40:50AM -0700, Larry Wall wrote:
: > I do expect that @() and $() will be used for interpolating list and
: > scalar expressions into strings, and it is probably the case the $()
: > would be a synonym for scalar(). @() would then be a synonym for
: > the mythical list() operator. Which probably, in Perl 6, turns out
: > to be equivalent to [...] when used in a scalar context, and a no-op
: > in list context. That is, $() and @() would essentially be typecasts.
:
: Hm, I would expect @() in a scalar context to give the
: same result as
:
: @tmp = @(...); $x = @tmp;
:
: That is, yeild the number of elements in the list.
Well, it does, but it does it differently. After the assignment, $x
contains a pointer to @tmp. However, if you say $x where a number is
wanted, you still get the length. If you're worried about the delayed
operation, you can force numeric context with $x = +@tmp;, just as you
can force string context with a unary ~.
: What would be the benefit of it being the same as [...] ? It would be
: one more character.
It's not the same. It's a cast, not a composer. It only comes out the
same in scalar context by accident. In string context, it
interpolates. In list context, it does nothing. The behavior of
emulating [] in a scalar context really stems from the more basic
notion that lists in Perl 6 should behave more polymorphically,
emulating the behavior of Perl 5 where it can. So when you say
for ($i = 0, $j = 0; $i < 10; $i++, $j++) {
}
The C-style comma operator would actually no longer be defined quite
the same. Rather, it's notionally producing a list that gets discarded
in void context. In the rare case that you're really interested in the
last value of a list, use ()[-1] instead.
All subject to Rule 2, of course. :-)
Larry