in reply to Preventing unintended list expansion inside hash literals.
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:
key1 => scalar(some_key($bar))
If you don't want undef values and are using perl v5.10, or higher, you can do something like these:
key1 => (some_key($bar) // 0) key1 => (some_key($bar) // [])
If none of those suit, then what you're currently doing ("use an intermediate scalar variable") seems like a perfectly fine solution.
— Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Preventing unintended list expansion inside hash literals.
by Athanasius (Archbishop) on Jan 05, 2017 at 09:55 UTC | |
by kcott (Archbishop) on Jan 05, 2017 at 10:38 UTC | |
by Athanasius (Archbishop) on Jan 05, 2017 at 13:00 UTC |