Norbert.Lammers has asked for the wisdom of the Perl Monks concerning the following question:

In FAQ4
http://theoryx5.uwinnipeg.ca/CPAN/perl/pod/perlfaq4/How_do_I_expand_fu +nction_calls_i.html
I found an issue on expanding function calls from strings.
These makes me weep for my late Sinclair Spectrum's Basic.
It used to have a Function calles EVAL$ that could directly evaluate Functions entered by the user into a String.
The article above is on expanding an functions
existing somewhere in the current Perl file by using ${/ ...}.
Things like (Think of replacing "cos($i)" by <STDIN>..)

#!/usr/bin/perl use posix;# $i=3; $funk = "cos($i)"; print "Das ist ${\$funk}";

actually do'nt work properly.
Of cause I could generate an external Module on the fly and then reference to it. But it wold be a much deeper meditation on the special strength of interpreter languages to do it all inline.
**Ommmmm**

Edit: 2001-03-03 by neshura

Replies are listed 'Best First'.
Re: function inline evaluation (formated *gg*)
by Fastolfe (Vicar) on Jan 24, 2001 at 20:17 UTC
    I view the whole inline evaluation thing to be somewhat of a nasty hack. Generally you are far better off doing something like this:
    print "Das ist ", cos($i); # or my $funk = 'cos($i)'; # single quotes mean $i is as-is # as opposed to being interpolated print "Das ist ", eval $funk; # or my $coderef = sub { cos(shift) }; print "Das ist ", $coderef->($i);
Re: function inline evaluation (formated *gg*)
by tadman (Prior) on Jan 24, 2001 at 18:34 UTC
    Woah.

    The easiest way is to use an anonymous sub:
    $i = 3; $func = sub { cos ($_[0]) }; print "Das ist ",&$func($i),"\n";
    "cos" isn't a defined subroutine, it's a Perl thing, and AFAIK, you can't reference it as "\&cos" like you can for your own subs.

    The following code doesn't work because of the aforementioned reason:
    $i = 3; $func = "cos"; print "Das ist ",&{$func}($i),"\n";
    This produces "Undefined subroutine &main::cos called at evl line 7."
Re: function inline evaluation (formated *gg*)
by c-era (Curate) on Jan 24, 2001 at 18:40 UTC
    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.
      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.