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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With