Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by martin (Pilgrim)
on Jun 23, 2007 at 09:03 UTC ( #622965=note: print w/ replies, xml ) Need Help??


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
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" } ; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://622965]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (11)
As of 2014-09-17 17:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (92 votes), past polls