Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: Mini-Tutorial: Working with Odd/Even Elements

by Roy Johnson (Monsignor)
on Jul 10, 2009 at 18:45 UTC ( #779002=note: print w/ replies, xml ) Need Help??


in reply to Re: Mini-Tutorial: Working with Odd/Even Elements
in thread Mini-Tutorial: Working with Odd/Even Elements

Update: figured it out (simulthanks to Ikegami). Gotta reference the right package. Corrected solution:

sub map_pairs(&@) { my $fn = shift; my $pkg = caller; map { my $idx = $_ * 2; no strict 'refs'; my ($a, $b) = local (${$pkg.'::a'}, ${$pkg.'::b'}) = (@_[$idx,$idx+1]); $fn->($a, $b); } (0..$#_/2); }
Previous, erroneous solution follows.

I think you made this harder than it needs to be. Isn't this equivalent?

sub map_pairs(&@) { my $fn = shift; map { my $idx = $_ * 2; local ($a, $b) = @_[$idx,$idx+1]; $fn->($a, $b); } (0..$#_/2); }

Caution: Contents may have been coded under pressure.


Comment on Re^2: Mini-Tutorial: Working with Odd/Even Elements
Select or Download Code
Re^3: Mini-Tutorial: Working with Odd/Even Elements
by ikegami (Pope) on Jul 10, 2009 at 18:56 UTC

    That won't work if the callback was compiled into a different package than map_pairs. The parent's code fixes this (assuming the callback is always compiled into the same package as the caller).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2014-08-30 02:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (291 votes), past polls