Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Problems with map(function, array)

by tobyink (Canon)
on Dec 10, 2012 at 22:41 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-19 00:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found