http://qs321.pair.com?node_id=779002


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.