I wanted to ask if I misunderstood following sentences from "Learning Perl" book p.117:
"Now, when Perl evaluates each %hash, there are no more key-value pairs available so each has to return an empty list.‖ The empty list is assigned to ($key, $value), so $key gets undef, and $value also gets undef.But that hardly matters, because you’re evaluating the whole thing in the conditional expression of the while loop. The value of a list assignment in a scalar context is the number of elements in the source list—in this case, that’s 0. Since 0 is a false value, the while loop is done, and execution continues with the rest of the program."
In my opinion, after "($key, $value)" are assigned to "()" and list becomes "(undef, undef)", scalar context takes only the last element of the list, that means 2nd "undef". Am I wrong?while ( ($key, $value) = each %hash ) { print "$key => $value\n"; }
OUTPUT:use 5.010; say '[', scalar (3..4), ']'; say '[', scalar (undef, undef), ']'; say '[', scalar (undef, 1, undef, 5), ']'; @a = (3..4); @b = (undef, undef); @c = (undef, 1, undef, 5); say '[', scalar @a, ']'; say '[', scalar @b, ']'; say '[', scalar @c, ']'; say '[', scalar 3..4, ']';
[] [] [5] [2] [2] [4] [34]
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |