in reply to function inline evaluation (formated *gg*)

Copied from function inline evaluation (so that node can be deleted).

You have a couple of problems, first you need to have the () around the the sub. The second problem is that you just have a variable for your sub. Below is two ways to make this work.

use posix;# $i=3; $funk = "cos($i)"; # We eval $funk print "Das ist ${\(eval $funk)}"; # We write out our sub here print "Das ist ${\(cos($i))}";
Remember that you are actually writing a sub (pretend it isn't in double quotes), so if you want to run a piece of code that is in $funk, you need to eval it.

Replies are listed 'Best First'.
Re: Re: function inline evaluation (formated *gg*)
by mr.nick (Chaplain) on Jan 24, 2001 at 19:38 UTC
    In c-era's example, he has
    $funk = "cos($i)";
    This assumes that the variable $i is alrady defined. When $funk is first assigned, $i is resolved to it's value (3 in the example); so $funk actually equals "cos(3)".

    If you want it dynamic, so that you can change $i and not restuff $funk, you should write it like:

    $funk="cos(\$i)";
    That way you can reassign $i and just eval($funk) again to recompute it's cosine.