in reply to What should be returned in scalar context?

To me, it depends A LOT on what the function is being used for. The inconsistency within built in perl functions demonstrates this sort of dwimmery in action.

I tend to prefer the second option, though (return wantarray ? @ret : \@ret;) because I don't like the idea of discarding any data when scalar context is forced. The reference still behaves nicely in boolean context, but it contains all the information of the original array."



Who is Kayser Söze?

Replies are listed 'Best First'.
Re: Re: What should be returned in scalar context?
by tilly (Archbishop) on Dec 02, 2003 at 14:59 UTC
    I used the second option for a while because I ran across some advocacy from someone who thought that it was nice to give people who wanted the option of a slight performance increase a chance to get it.

    I have since then drifted away from using it, and the experience of extending the test suite for Text::xSV (which used that interface) has convinced me that it is a complete PITA to work with unless you only want one scalar value.

    (Yeah, yeah. I know how to use appropriately placed parens to force list context. To me tricks like that exemplify what people dislike about Perl.)

Re: Re: What should be returned in scalar context?
by Anonymous Monk on Dec 02, 2003 at 14:56 UTC
    The reference still behaves nicely in boolean context, but it contains all the information of the original array.
    What if the array is empty, how is the reference useful then in boolean context (or do you return undef in that case)?