Come for the quick hacks, stay for the epiphanies. | |
PerlMonks |
Re^4: Preventing unintended list expansion inside hash literals.by Athanasius (Archbishop) |
on Jan 05, 2017 at 13:00 UTC ( [id://1179018]=note: print w/replies, xml ) | Need Help?? |
Hello Ken, No, you’re right: in the context of the OP’s example code, scalar works fine. I was looking to the more general question: What's the idiomatic way to guard against unintentionally expanding a non-singleton list into the bodies of your hashes? A “non-singleton list” can be an empty list (as in the example code), OR a list with more than one element. Use of scalar for the latter case returns the last element of the list, not the first. From the OP’s use of $array_ref->[0] as his example of “a single-element list,” I assumed that in this case he would want the first element of the list. But admittedly this is little more than a guess. :-( Sorry for the confusion. Cheers,
In Section
Seekers of Perl Wisdom
|
|