I always wonder why I must write
foreach my $x (@arr)
instead of
foreach my $x @arr
What is the purpose of the parentheses here?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Flippant answer: Because that’s the way Larry
likes itliked it when Perl 5 was created.More serious answer: It helps to disambiguate between “iterate over
@arr, putting each value into$x” (for $x (@arr)) and “iterate over$xand@arr, putting each value into$_” (for ($x, @arr)). And, yes, I realize that the extra comma in the latter version does make disambiguation possible even without the parens, but it’s less obvious to a human reader and I expect that relying on that alone would lead to more errors.