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?
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.)
精彩评论