Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
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 taking refuge in the Monastery: (3)
As of 2015-07-05 08:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (61 votes), past polls