I'm staring at this code in perl LWP::Protocol.pm and I don't understand how the loop would ever exit:
while ($content = &$collector, length $$content) {
$content_size += length($$content);
# more here
}
In my interpretation, I am thinking this is a list of 2 elements in scalar context, so the result that the while loop is testing is the length of the list, which is always 2 and never false. How does this really work?
Please have a look at the operator precedence in the documentation. There you'll see that = has a higher precedence than ,. This means first it does $content = &$collector due to the higher precedence of = compared to ,. Then it does length $$content.
Continuing with the documentation about the Comma Operator one will find:
Binary "," is the comma operator. In scalar context it evaluates its left argument, throws that value away, then evaluates its right argument and returns that value.
Thus the result in this case will be the right one, i.e. length $$content. This will then be used as the condition inside while.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With