Assign method call to hash value?

William Blunn bill+london.pm at blunn.org
Tue Jan 29 13:51:34 GMT 2013


On 29/01/2013 11:36, gvim wrote:
> On 29/01/2013 02:33, Mike Stok wrote:
>> Have you tried reading
>>
>>    perldoc -f scalar
>>
>> Hope this helps,
>>
>> Mike
>>
>
> Yes, I'm aware of the scalar function but still not clear why 
> assigning $r->method as a hash value doesn't invoke a scalar context 
> in the first place.

The code

return { passed => $passed, valid => $r->valid, missing => $r->missing, invalid => $r->invalid, unknown => $r->unknown };

is equivalent to this

return { 'passed', $passed, 'valid', $r->valid, 'missing', $r->missing, 'invalid', $r->invalid, 'unknown', $r->unknown };

The bit inside the curly braces is not any kind of magical "hash 
constructor" but a list, plain and simple.

None of the values in that list "know" that they will eventually become 
a hash key or a hash value.

Everything in there is just in list context. So your method calls are in 
list context.

The curly braces then take the elements of that list, in pairs, and then 
treat each pair as (key, value) to build a hash.

(This links to a common discussion (linked in turn with the old 'return 
undef;' vs. 'return;' argument) about whether or not functions should 
behave differently depending on scalar and list context.)

Regards,

Bill


More information about the london.pm mailing list