Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: wantarray alternative (helper)

by tye (Sage)
on Jul 10, 2013 at 20:10 UTC ( #1043549=note: print w/replies, xml ) Need Help??


in reply to wantarray alternative

sub context { wantarray ? @_ : $_[0] } sub whatever { return context( map { ... } @_ ); }

- tye        

Replies are listed 'Best First'.
Re^2: wantarray alternative (helper)
by tqisjim (Beadle) on Jul 10, 2013 at 20:38 UTC

    This works, but why isn't this function built in somewhere? It seems most responders assume that this is perl's default behavior.

      Because that function is trivial to write and it isn't the only or even the obviously right choice.

      sub first_or_all { wantarray ? @_ : $_[0] } sub last_or_all { wantarray ? @_ : $_[-1] } sub list_or_aref { wantarray ? @_ : \@_ } sub aref_or_list { wantarray ? @{$_[0]} : $_[0] } sub pairs_or_href { wantarray ? @_ : {@_} } sub href_or_pairs { wantarray ? %{$_[0]} : $_[0] } sub all_or_concat { wantarray ? @_ : join $,, @_ } sub all_or_die { return @_ if wantarray; return $_[0] if @_ <= 1; require Carp; local $Carp::level = 2; my $sub = ( caller(1) )[3]; Carp::croak( "Don't call $sub() like that in a scalar context" ); }

      (to list just a few off the top of my head that I've heard people argue are wise choices)

      - tye        

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1043549]
help
Chatterbox?
[ambrus]: Corion: really? Does that mean the Prima watcher objects behave as handles that stop the watcher when they're destroyed, just like AnyEvent expects?
[ambrus]: "all the bugs" -- hehe, good luck
[ambrus]: Corion: what I don't understand is, if you make a one-shot AnyEvent timer with short timeout, does this code guarantee that the Prima timer can't queue two timeout events before invoking any of them?
[ambrus]: Because that could break AnyEvent code, which is why I added a guard.
[ambrus]: (It might be easier to use the callback scalar as a guard, as in $c and &$c(); $c = ();)

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2016-12-09 08:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (150 votes). Check out past polls.