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

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'


Comment on Re: Problems with map(function, array)
Select or Download Code
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2014-07-13 15:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (250 votes), past polls