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.

Replies are listed 'Best First'.
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).