ismail has asked for the wisdom of the Perl Monks concerning the following question:

I have a function call stored in a variable:

$function = '&PM::xyz::somefunc (named => param)';

I need to be able to eval this and return the actual values. Here is what I've tried:

my $return_value = eval {$function};

This effectively $return_value = $function;. HEP!

Your eval-non-pro,
ismail

Replies are listed 'Best First'.
Re: Eval tomfoolery...
by Fastolfe (Vicar) on Dec 13, 2000 at 00:02 UTC
    You probably want:
    my $return_value = eval $function;
    See the documentation for eval. Just be aware that this does compilation at run-time, so if you're doing a lot of these, your performance will suffer. Unless you need to be re-interpreting the string contained in $function to come up with different Perl code for each try, you should try to think of a way to do this without using eval.
Re: Eval tomfoolery...
by merlyn (Sage) on Dec 13, 2000 at 00:25 UTC
    I think your first problem to solve needs to be: "how the heck did I get a function call stored in a variable instead of as module or include file, and how do I fix that?".

    Down the eval path usually lie monsters.

    -- Randal L. Schwartz, Perl hacker

      it's like that because I'm writing an HTML templating scheme. I'd use the s00p0r regex to resolve it:

      $template =~ s {%% (.*?) %%} {exists ($fillings->{$1}) ? $fillings->{$1} : "" }gsex;

      ... but I need to be able to more gracefully handle errors than just putting a "" in. f.
        Please don't write Yet Another HTML Templating Scheme. I'd suggest Template Toolkit. Go hence, right away. Do not reinvent the wheel until you understand ALL THE OTHER WHEELS first. And asking basic questions like "how do I use eval?" already scares me about you.

        -- Randal L. Schwartz, Perl hacker

Re: Eval tomfoolery...
by ismail (Acolyte) on Dec 13, 2000 at 00:34 UTC
    Thanks..that's what I was looking for.