Syntactic Confectionery Delight | |
PerlMonks |
Re: Preventing unintended list expansion inside hash literals.by kcott (Archbishop) |
on Jan 05, 2017 at 09:38 UTC ( [id://1179000]=note: print w/replies, xml ) | Need Help?? |
G'day gregory-nisbet, "What's the idiomatic way to guard against unintentionally expanding a non-singleton list into the bodies of your hashes?" I don't know if there's an idiom for that specific case; however, as a general case, you can use scalar to force scalar context. So, if you're happy to have undef values in your hash:
If you don't want undef values and are using perl v5.10, or higher, you can do something like these:
If none of those suit, then what you're currently doing ("use an intermediate scalar variable") seems like a perfectly fine solution. — Ken
In Section
Seekers of Perl Wisdom
|
|