Problem with Interpolation

Problem with Interpolation

am 08.08.2006 13:52:45 von Jean-Louis Leroy

The manpage of Interpolation says:

It is posible to pass multiple (or no) arguments to your function.
There are two alternate syntaxes:

$interpolator{param1,param2}
$interpolator{param1}{param2}

The first syntax will pass both arguments in $_[0] joined by $;, so you
have to split them:

use Interpolation add => sub{@_ = split /$;/o, $_[0]; $_[0] + $_[1]};
print "3 + 4 = $add{3,4}\n";

The other syntax (used for example by builtin 'sprintf') requires quite
some magic, so you probably wouldn't want to be forced to write it
yourself. (See the source of this module if you want to know how
strange is the code. )

I try the second syntax, hoping to get all the arguments in @_:

use Interpolation sum => sub {
print "*** @_\n";
my $sum = 0;
$sum += $_ for @_;
$sum
};

Unfortunately I get:

print "sum is $sum{41}{2}{3}\n";
*** 41
sum is

Is this normal behavior?
--
Jean-Louis Leroy
Europerl
http://www.europerl.be

Re: Problem with Interpolation

am 08.08.2006 21:37:30 von John Bokma

Jean-Louis Leroy wrote:

> The other syntax (used for example by builtin 'sprintf') requires
> quite some magic,

^^^^^^^^^^^^^^^^

> so you probably wouldn't want to be forced to
> write it yourself. (See the source of this module if you want to
> know how strange is the code. )
>
> I try the second syntax, hoping to get all the arguments in @_:
>
> use Interpolation sum => sub {
> print "*** @_\n";
> my $sum = 0;
> $sum += $_ for @_;


I guess this is not the "quite some magic".


I read quickly the documentation, and guess:

'foo:$*->$ - ask for arbitrary number of scalar parameters
$foo{par1}{par2}{par3}{$;}

with {$;} "NOT required anymore."



use Interpolation 'sum:$*->$' => sub {

print "*** @_\n";
my $sum = 0;
$sum += $_ for @_;

return $sum;
}



should work I guess.


--
John Bokma Freelance software developer
&
Experienced Perl programmer: http://castleamber.com/