in reply to Template Toolkit 2 strange behaviour
This is one of those cases where TT is like so many other modules - trying to be as smart as possible. It's trying to handle lists of hashes, key-value pairs (in a hash), and many other things at the same time. And, like those other modules (I'm thinking of XML::Simple here (*)), sometimes it needs a nudge to get things right in certain cases. In this case, you want to make sure that TT always treats EXP's return as a list, so you have to give it sufficient metadata to ensure that is the case. When you return a list of two or more, it can figure out that it's a list, so all is fine. When you return a single hash ref, it can't tell that it's to be treated as a list when looping over it with the FOREACH, so it doesn't. The metadata that is missing is that which tells TT that this is still a list, even though there's only one item there. The easiest way to do that is to return a reference to a list. Turns out that this is generally faster, too, as you are only copying one reference around instead of (potentially) multiple objects (references).
My rule of thumb with TT is to always return array refs when I want my data treated as a list (looped in the template). I must have been bitten by this at some point, too. :-)
(*) XML::Simple does this in the reverse - when parsing out an XML file, it only creates array refs when a second item is found with the same name at the same level, i.e., only to avoid collisions in the hash. Sometimes it's more convenient to always treat something as an array ref, so if that's what you want, you have to tell XML::Simple that.