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

local(*{$caller."::a"}) = \my $a; local(*{$caller."::b"}) = \my $b;
Does this technique depend on the default "automatic declaration" of $a and $b as package-vars? (it's meant for sort {...})

In other words: using $c for map_triples wouldn't be as easy..(?)

Cheers Rolf

Replies are listed 'Best First'.
Re^3: Mini-Tutorial: Working with Odd/Even Elements
by duelafn (Vicar) on Jul 10, 2009 at 11:16 UTC

    It will be a problem if you are running under strict. However, if the caller sets up $c as a package variable it will work.

    sub map_triples(&@) { my $f = shift; my @res; no strict 'refs'; my $caller = caller; local(*{$caller."::a"}) = \my $a; local(*{$caller."::b"}) = \my $b; local(*{$caller."::c"}) = \my $c; push @res, $f->($a,$b,$c) while ($a, $b, $c) = splice @_, 0, 3; return @res; } use 5.010; use warnings; use strict; our $c; say for map_triples { $a + $b + $c } 1..12;

    Good Day,
        Dean

      hmm .. maybe an approach using the @_-Array (i.e. $_[0] instead $a and so on) is more scalable...

      Cheers Rolf