Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I take a reference to `sin`?

I can define a subroutine and take a reference to it like this

sub F { q(F here) }
$f = \&F;
print &$f           # prints “F here”

But how can I do the same with, e.g., sin?

$f = \&sin;
print &$f           # error: Undefined subroutine &main::sin called

That sounds as though I should be able to use \&MODULE::sin; evidently cos is not in main, but which module is it in? I do not see that documented anywhere.

like image 629
xebtl Avatar asked Sep 22 '15 09:09

xebtl


1 Answers

sin is not in your current package. You need to call it from the CORE:: namespace. CORE:: is where all the built-in functions reside. It is imported automatically.

my $f= \&CORE::sin;
print $f->(1);

Output:

0.841470984807897

Knowing about CORE::foo is mostly usefull if you want to call the original built-in after a function was overwritten.

use Time::HiRes 'time';

say time;
say CORE::time;

This outputs:

1442913293.20158
1442913293
like image 53
simbabque Avatar answered Sep 21 '22 23:09

simbabque