Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Subject
- Posted on
- Charles T. Smith
June 23, 2012, 6:47 am
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
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
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);
Site Timeline
- » Alias (ancient module) doesn't build anymore
- — Next thread in » PERL Modules Announcements
- » Perl Developer
- — Previous thread in » PERL Modules Announcements
- » Updating the hash across the files
- — Newest thread in » PERL Modules Announcements
- » Anyone Using ESET NOD32??
- — The site's Newest Thread. Posted in » Anti-Virus Software