Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is Perl's scalar comma operator useful?

Is there any reason to use a scalar comma operator anywhere other than in a for loop?

like image 586
Eugene Yarmash Avatar asked Feb 04 '10 15:02

Eugene Yarmash


1 Answers

Since the Perl scalar comma is a "port" of the C comma operator, these comments are probably apropos:

Once in a while, you find yourself in a situation in which C expects a single expression, but you have two things you want to say. The most common (and in fact the only common) example is in a for loop, specifically the first and third controlling expressions. What if (for example) you want to have a loop in which i counts up from 0 to 10 at the same time that j is counting down from 10 to 0?

So, your instinct that it's mainly useful in for loops is a good one, I think.

like image 60
Jonathan Feinberg Avatar answered Nov 14 '22 22:11

Jonathan Feinberg