[Catalyst] html-widget bug or design?

A. Pagaltzis pagaltzis at gmx.de
Wed Mar 1 19:00:18 CET 2006


* Carl Franks <fireartist at gmail.com> [2006-03-01 13:45]:
>If there's only one by that name, return it. If there's more
>that one, return a list in array context, an array-ref in scalar
>context.

Please don’t. Every time I have to use such an API it makes me
cry. :-( Making the type of the return value dependent on its
value is a breathtaking pain in the rear end. It causes code
that’s gunked up with gobs of `ref $foo eq 'ARRAY'` checks.

Be consistent.

If you want to return an array ref in scalar context, then do it
regardless of how many elements it will have. It’s a bit of a
pain to have `->[0]` everywhere, not to mention the visual noise,
but it’s a lot less nasty than forcing people to save the return
value and check its type to make sure the code does not
unexpectedly break.

Or do the simplest thing that could possibly work and just return
a list of matching elements, regardless of context, leaving it
up to the caller how many elements they want to keep. I’d favour
this. The fewer clever special cases, the better.

Regards,
-- 
Aristotle Pagaltzis // <http://plasmasturm.org/>



More information about the Catalyst mailing list