Assign method call to hash value?

Randal L. Schwartz merlyn at stonehenge.com
Wed Jan 30 03:43:32 GMT 2013


>>>>> "Jasper" == Jasper  <jaspermccrea at gmail.com> writes:

Jasper> On 29 January 2013 11:36, gvim <gvimrc at gmail.com> wrote:
>> 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.


Jasper> Is there any context in the braces? It's just a (albeit fat) comma
Jasper> separated list of stuff, isn't it?

In general, this is dangerous:

my $hashref = { foo => somefunc(), bar => otherfunc() };

You are invoking somefunc() and otherfunc() in *LIST* context.

They may or may not return a single scalar.  They more likely will
return a list, or empty.  And your hash will be messed up.

Better written as:

my $hashref = { foo => scalar somefunc(), bar => scalar otherfunc() };

Then you can be assured of a two-element hash.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn at stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion


More information about the london.pm mailing list