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

Re: Comma Operator

Thread Previous
February 9, 2004 01:53
Re: Comma Operator
Message ID:
Larry Wall writes:

> On Wed, Jan 21, 2004 at 08:51:33PM -0500, Joe Gottman wrote:
> : Great, so
> :     $x = foo(), bar();
> : means the same thing as
> :     $x = ( foo(), bar() );
> No, we haven't changed the relative precedence of assignment and
> comma.  I've been tempted to, but I always come back to liking the
> parens for visual psychological purposes.

But that does lead to propagating the myth that parens, rather than
commas, denote lists.

> Plus changing the precedence would break
>     loop $i = 0, $j = 0; $x[$i,$j]; $i++, $j++ { ... }

I think that matters less -- assigning lists is a much more common
operation that using a C-style C<for> loop, and is also something that's
a common source of errors with beginning Perl 5 programmers.  Most of
the C-style C<for> loops I see are unnecessary anyway:

  * The majority of them are iterating through array indices (which are
    then only used to look up elements in those arrays), something far
    more easily achived with a C<foreach> loop and letting Perl take
    care of the indices.

  * Many of the remainder are still iterating through consecutive
    integers, where C<for my $i (1 .. 10)> or whatever would much tidier
    than checking the boundary and increasing the iterator manually.

  * Even for situations which 'genuinely' need the C-style loop in Perl
    5 (such as looping through items in an array but also having the
    index number available) many of those in Perl 6 can use C<zip>.

So I think it's odd to optimize for convenience of a construct that has
so little use.

Especially when there are ways of writing that statement without any
commas in it:

  loop $i = $j = 0; $x[$i,$j]; do { $i++; $j++ } { ... }

While on the topic of C<loop> and parens, it hadn't occurred to me that
the former could now be written without the latter; I knew that C<for>,
C<if>, and C<while> could be, and now I look at it I can see that
C<loop> can be so-written unambiguously -- but having 'internal',
unbracketed, unquoted, unprotected semicolons scares me a little.

I'd feel more at ease if on seeing something like:

  loop $i = $j = 0;

I could be certain that that semicolon denoted the end of that
statement, without my having to know what C<loop> does.

Again, given how infrequently C<loop> is likely to be used, would
imposing such a rule be much of a hardship?  So a loop could look
something like:

  loop($i = $j = 0; $x[$i,$j]; do { $i++; $j++ }) { ... }

and it's blindingly obvious that the first semicolon couldn't possibly
be the end of the statement.


Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About