Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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

by LanX (Saint)
on Jul 10, 2009 at 10:12 UTC ( [id://778870]=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

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 (Parson) 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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (8)
As of 2024-04-24 09:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found