In a message dated Tue, 24 Sep 2002, Chip Salzenberg writes:
> According to Trey Harris:
> > According to Larry,
> > $a = (1,2,3);
> > is equivalent to
> > $a = [1,2,3];
> > because they're both equivalent to
> > $a = scalar(1,2,3)
>
> But that's the bit we're arguing about. If you allow
>
> $a = (1,2)
>
> then what about
>
> $a = (1)
>
> ? And if someone says that I have to write:
>
> $a = (1,)
>
> then I am going on the warpath. That Way Lay Python.
*shrug* Regardless of whether we like it, what Larry said is true unless
and until he invokes Rule 2. And unless he invokes Rule 2,
C<scalar(1,2,3)> is equivalent to C<[1,2,3]>.
My suggestion is merely that one-tuples lacking the comma cannot be
constructed with parens (round brackets) and that list-flattening context
has the effect of voiding top-level round parens (but not square
brackets).
So
$a = (7);
would assign 7 to $a, and
push @a, (7,3,2);
would push the elements 7, 3 and 2 to the end of @a, but
push @a, [7,3,2];
would push a single element containing the arrayref [7,3,2] onto the end
of @a.
I agree it's weird, but it seems to DWIM, doesn't it? I just don't think
a reduction that C<()> is equivalent to C<[]> is workable.
If you want to assign $a the value [1], then you write
$a = [7];
I would think that
$a = (7,);
would be legal as well, but only in order to handle the DWIM of
$a = (1,
2,
3,
);
where you kill the middle two lines--not as the preferred (and certainly
not as the only) way to create a one-tuple. Making
$a = (7);
create a single-element list is just unworkable. It requires that
programmers get precedence exactly right, and it badly violates the
principle of least surprise.
Trey