Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1043552]
[erix]: ha :) (confusion)++
[erix]: just trying to keep you alert ;)
[erix]: just FYI, that link also mentioned the interesting word Hellweg
[erix]: but that's probably a known word to you guys

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2017-12-12 16:32 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (335 votes). Check out past polls.