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

Re: wantarray alternative

by tobyink (Canon)
on Jul 10, 2013 at 20:17 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-03-19 03:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found