Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: wantarray alternative

by tobyink (Abbot)
on Jul 10, 2013 at 20:17 UTC ( #1043552=note: print w/ replies, xml ) Need Help??

in reply to wantarray alternative

This is quite a concise way of doing things:

sub lowercase { wantarray ? (map { lc } @_) : lc(shift); }

This is an insane way of doing things:

sub lowercase { ( wantarray ? sub{@_} : sub{shift} )->( map { lc } @_ ); }

Update: Here's another solution using my module returning that allows you to create additional keywords that act like return...

#!/usr/bin/env perl use v5.14; use returning { lreturn => sub { wantarray ? @_ : $_[0] } }; sub lowercase { lreturn map { lc; } @_; warn "this line never executes"; } say for lowercase('Alice', 'Bob'); say scalar lowercase('Carol', 'Dave');
package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Comment on Re: wantarray alternative
Select or Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2015-11-26 00:43 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (695 votes), past polls