# how to do a reference to a func in Math::Trig

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Hi,

I'm experimenting with Math::Trig and would like to be able to pass the
function in as a parameter:

\$f = \$opts || &sin;

...
\$f(\$w);

Undefined subroutine &main::sin called at (eval
9)[/usr/lib/perl5/5.14.2/perl5db.pl:640] line 2.

I have also tried:

\$f = &Math::Trig::sin;
Undefined subroutine &Math::Trig::sin called at (eval 10)...
\$f = \&sin;            # just delays the problem

etc.

Can anyone tell me how to take a reference to func in a module?

TIA
cts

## Re: how to do a reference to a func in Math::Trig

Math::Trig does not define sin() or cos() as they are already defined
in core Perl.

Here is how you would do it with sin()
\$f=sub{return sin(\$_[0])};
print &\$f(\$x),"\n";

and here is an example of what you specifically asked for (a function
from Math::Trig)

use Math::Trig;
\$f=sub{return Math::Trig::asin(\$_[0])};
\$x=1;
print &\$f(\$x),"\n";

On 6/23/12 2:47 AM, Charles T. Smith wrote:

## Re: how to do a reference to a func in Math::Trig

On 06/24/12 03:27, Adam Russell wrote:

Oh, that's cool, how you used an anonymous function to do that.  I tried
that, too, but couldn't make it work.

But there's another answer that I got on the beginners@perl.com:

> The cos sub is defined in Math::Complex, which Math::Trig loads.
>
> Try this:
>
> use strict;
> use Math::Trig;
>
> my \$cos = \&Math::Complex::cos;
> print \$cos->(0);

## Re: how to do a reference to a func in Math::Trig

Incidently, this is what I wrote back:

On 06/24/12 12:49, Charles T. Smith wrote: