Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Problems with map(function, array)

by tobyink (Abbot)
on Dec 10, 2012 at 22:41 UTC ( #1008177=note: print w/replies, xml ) Need Help??

in reply to Problems with map(function, array)

Nothing to do with map really. uc has magic!

In the following construct, uc is getting called with no parameters:

my @caps = map uc, @lower;

As per perlfunc, when called with no parameters, uc operates on $_. So you need to make your function act the same (as indeed you did in your second experiment).

From Perl 5.10 and above, there's a better technique to write such functions:

sub my_uc (_) { # note underscore in parentheses my $c = shift; return uc($c); }

And now my_uc has the same magic as uc.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Replies are listed 'Best First'.
Re^2: Problems with map(function, array)
by Redei (Initiate) on Dec 12, 2012 at 14:06 UTC

    Thank you very much. That is just what I needed. Now, instead of

    sub twice { 2 * (shift // $_) }

    I'll use something like

    sub twice(_) { 2 * shift }

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2016-10-27 02:27 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (352 votes). Check out past polls.