Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Sub::Context (Re: ... returned in scalar context)

by Ovid (Cardinal)
on Dec 02, 2003 at 17:12 UTC ( [id://311687]=note: print w/replies, xml ) Need Help??


in reply to What should be returned in scalar context?

One thing which I have been meaning to do, but as usual never quite found enough tuits for, was write Sub::Context. With this module, subroutines with context attributes would be expected to return an array or list, but in scalar context would automagically behave as the context attribute specifies without the programmer being forced to write the code for every case.

use Sub::Context qw/arrayref first iterator custom/; sub foo : arrayref { # return wantarray ? @results : \@results; } sub bar : first { # return wantarray ? @results : $results[0]; } sub baz : iterator { # return wantarray ? @results : get_iterator(@results); } sub quux : custom { # user-defined return behavior? }

I've had the sticky note for this module on my desk for about three months now. This seems like as good a place as any for asking for suggestions on the interface and behavior.

Update: chromatic just reminded me that not only is there a module named Sub::Context, which he wrote (and which I've seen, darn it), it does what I was looking for, but in a somewhat different fashion.

Cheers,
Ovid

New address of my CGI Course.

Replies are listed 'Best First'.
Re: Sub::Context (Re: ... returned in scalar context)
by theorbtwo (Prior) on Dec 08, 2003 at 05:06 UTC

    This sounds useful to me. OTOH, I think you're missing some. :last and :count you should have, if only for completeness. :uselessvoid should give warnings if called in void context. :mutatevoid would set $_[0] to the return if used in void context. :warnscalar would warn if called in scalar context. :loop would do the close equivlent of tilly's if @_>1: when called in list context, with more then one arg, just run through the sub for each argument.

    Upon reading the update: Oh. I have to say, I like your API better then chromatic's, but see how both could be good in different circumstances. Perhaps you should find another namespace for yours?

    Update: Fixed minor markup issue, thanks ysth.


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-24 19:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found