a list assignment (what you have in parentheses) in scalar context results in the number of elements on the right side of the assignmentThis has drawn a couple of private responses. Yes, it does actually use the right side of the assignment, not the left. Consider the idiom:
There are always 2 values on the left of the assignment there. list-context each() will produce 2 values also, until the end of the hash is reached; whereupon it will return an empty list. So the condition the while is checking will be 2 each time there is another key to process and 0 once there is not, terminating the loop.while (my ($key,$value) = each %foo) { }
Another idiom: example taken from perlfaq4 "How do I count the number of occurences of a substring within a string?":
Here, the left hand side of the list assignment has 0 elements, and the right has however many -\d+ sequences are in $string. $count is assigned that number of sequences.$count = () = $string =~ /-\d+/g;
In reply to list assignment in scalar context by ysth
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |