rsFalse has asked for the wisdom of the Perl Monks concerning the following question:
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]
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: each %hash: scalar context of list (undef, undef) is false
by choroba (Cardinal) on Nov 22, 2014 at 18:00 UTC | |
|
Re: each %hash: scalar context of list (undef, undef) is false
by Eily (Monsignor) on Nov 22, 2014 at 18:16 UTC | |
|
Re: each %hash: scalar context of list (undef, undef) is false
by LanX (Saint) on Nov 22, 2014 at 18:26 UTC | |
|
Re: each %hash: scalar context of list (undef, undef) is false
by ikegami (Patriarch) on Nov 22, 2014 at 20:09 UTC | |
|
Re: each %hash: scalar context of list (undef, undef) is false
by Anonymous Monk on Nov 22, 2014 at 18:31 UTC | |
by rsFalse (Chaplain) on Nov 22, 2014 at 18:40 UTC |