[Melbourne-pm] scalar(keys %foo)

Andrew Pam andrew at sericyb.com.au
Mon Jun 1 20:10:26 PDT 2020

On 2/6/20 1:06 pm, Mathew Robertson wrote:
> Does "scalar(keys %foo)" materialise the list of keys, grabs its length,
> then throw away the memory?  .. or is Perl smart enough to see that
> context, to understand that it just needs to count the keys ?

I'm pretty sure that "scalar" causes the "keys" function to be called in
scalar context, and it therefore only returns the number of keys.

You can do the same in your own functions, for example by checking
"wantarray", though that isn't usually necessary.

Hope that helps,

More information about the Melbourne-pm mailing list