good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
Re: Preventing unintended list expansion inside hash literals.by Athanasius (Archbishop) |
on Jan 05, 2017 at 08:08 UTC ( [id://1178989]=note: print w/replies, xml ) | Need Help?? |
Hello gregory-nisbet, IIUC, a straightforward way to get the desired behaviour is to change this line:
— which, if %$hash_ref is undefined may return either undef or () (the empty list), depending on whether the subroutine is called in scalar or list context — to this:
BTW, I get the same results with this line:
commented-out. Why do you think it’s useful here? Anyway, hope that helps, Update 1: It seems I missed the point of your question, which is “What's the idiomatic way to guard against unintentionally expanding a non-singleton list into the bodies of your hashes?” I think your own solution, to “use an intermediate scalar variable” is probably the best way. Update 2: Alternatively, you could combine your suggestion of using the first element with a test for definedness:
In Section
Seekers of Perl Wisdom
|
|