Dear Monks,

I have created an entity, which applies the elements to another entity, but the incantation follows the path of the black arts. I have to call upon another Perl Daemon to grant it life. Please show me my wrongs.

For the less spiritual enlighted:
It is a function, which applies the elements of an array to another function. Like the javascript apply. Here it is:

sub repeat { my $n = shift; my $x = shift; my $array = []; for($i = 0; $i < $n; $i++){ push @{$array}, $x; } return $array; } sub apply { my ($func, $args) = @_; my $size = @{$args}; my $app = repeat($size, '(shift @{$x})'); eval 'sub _apply { my $f = shift; my $x = shift; $f->(' . join +(',', @{$app}) . '); }'; return _apply(\&$func, $args); }

It does its job, but I have understood, that when I call eval another perl interpreter will be started. This is of course not very efficient. Is there a better way to get around this?

I thought about generating a couple of static functions like apply1 apply2 apply3 etc and put it in a package. But I find this offending too.

Example:

sub test { my $x = shift; my $y = shift; my $z = shift; print $x + $y + $z; } # output 6 apply(\&test, [1,2,3]);

Thanks for your help!

In reply to Applying an array to a function by eklerks

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.