in reply to Re: Returning undef: The point I would like Damian to reconsider
in thread Returning undef: The point I would like Damian to reconsider

Don't change subroutine results based on list or scalar context.

If I were trying to follow that advice, what should I do if a subroutine that returns a list in a list context gets called instead in a scalar or void context? "die"?

Coercing a strictly list returning function into returning a scalar would be wrong and could be treated as an error of the caller if detected.

But you caught me there, the subroutine would still need to be context-aware to detect that situation. The impact on the subroutine code could be kept at bay, though, if we tucked that piece of logic away, say, in a listonly convenience function. Is something like that already on CPAN? Hmmmm...

  • Comment on Re^2: Returning undef: The point I would like Damian to reconsider

Replies are listed 'Best First'.
Re^3: Returning undef: The point I would like Damian to reconsider
by merlyn (Sage) on Jun 23, 2007 at 18:13 UTC
    use Contextual::Return; use Carp qw(croak); ... sub foo { ... return LIST { some listy expression here } SCALAR { croak "please invoke me in a list context" } ; }