On 24/06/2012 06:59, Charles Smith wrote:
Thank you, that was the clue I needed!  This now works for me:

...
$f         = $opts{f} || "sin";
...
     $f = "Math::Complex::$f";
...
     print eval ($amplitude) * (&$f (2 * pi * $i/$n) + eval ($dc))."\n";

and so I can use this, my "siggen" pgm with gnuplot:

plot   "<(./siggen  -f cos  -p 2 -n66  )" lc 1

to get a nice curve.

Unfortunately, though, I was only able to get it to work as symbolic reference. 
 First I tried:

     $f = \&Math::Complex::$func;

Scalar found where operator expected at ./siggen line 194, near 
"&Math::Complex::$func"
         (Missing operator before $func?)
syntax error at ./siggen line 194, near "&Math::Complex::$func"

I also tried:

     $f = eval (\&Math::Complex::$func)

but that didn't help.

But that's all unimportant, because my pgm works now,
thank you.

Hi Charles

The problem is that Math::Trig imports all trig functions into the
current packe except for 'sin' and 'cos' which are provided by the CORE
package. So

    my $func = 'tan';
    my $f = \&$func;

works fine, but

    my $func = 'sin';
    my $f = \&$func;

does not.

You can get around this either by check the name of the function and
prefixing 'CORE::' if it is 'sin' or 'cos':

    use strict;
    use warnings;

    use Math::Trig;

    for (qw/ sin cos tan /) {
      my $func = /^(?:sin|cos)$/ ? "CORE::$_" : $_;
      my $f = \&$func;
      print $f->(pi/4), "\n";
    }

or by aliasing the current package's 'sin' and 'cos' subroutines with the CORE functions:

    use strict;
    use warnings;

    use Math::Trig;

    BEGIN {
      no warnings 'once';
      *sin = \&CORE::sin;
      *cos = \&CORE::cos;
    }

    for my $func (qw/ sin cos tan /) {
      my $f = \&$func;
      print $f->(pi/4), "\n";
    }

which IMO is the tidiest, and certainly the most efficient.

HTH,

Rob

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to