rubixibuc
rubixibuc

Reputation: 7397

Perl && do { last; };

In my book it uses something like this:

for($ARGV[0])
{
Expression && do { print "..."; last; };
...
}

Isn't the for-loop incomplete? Also, what's the point of the do, couldn't it just be { ... }, or does the do have some importance here?

Upvotes: 2

Views: 368

Answers (1)

Keith Thompson
Keith Thompson

Reputation: 263237

There are two forms of for statement in Perl. The one you're seeing here is often written as foreach, but for and foreach are synonyms. It normally iterates over a list, setting $_ to each element. In this case, the "list" is a single value, so it has the effect of setting $_ to $ARGV[0] for the body of the loop.

The do is needed to make the block { ... } into an expression, so it can be an operand of the && operator. (See what happens if you omit the word do.)

(And you were missing a semicolon; I've edited the question to fix that.)

Upvotes: 6

Related Questions