Thank you, ikegami, but one more question on this,
I have seen the Util.pm code and I included only the reduce and sum part into the below code, now the code looks like this.
sub reduce (&@) {
my $code = shift;
no strict 'refs';
return shift unless @_ > 1;
use vars qw($a $b);
my $caller = caller;
local(*{$caller."::a"}) = \my $a;
local(*{$caller."::b"}) = \my $b;
$a = shift;
foreach (@_) {
$b = $_;
print ">$code<\n";
$a = &{$code}();
}
$a;
}
use vars qw($a $b);
sub sum (@) { reduce { $a + $b } @_ }
@arr = (0,0,1, 3,2,0, 1,1,1, 11,100,12);
push @Res, sum splice(@arr,0,3) while @arr;
print "@Res\n";
Now I see the magic part is '&{$code}()', could you please explain me about this magic part and tell me how this is better than eval?.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.