Search code examples
perlfunctionsymbolic-referencesperl-core

Is it possible to symbolically reference a Perl core module?


I know I could easily do something like

sub sin {
    sin($_[0]);
}

and symbolically reference that for every function I need to symb ref, but I'd just like to know if there's a way to do something like

{$foo}(123);

vs.

&{$foo}(123);

which works, but not for core functions.

Thanks.


Solution

  • AFAIK no, you can't do it. For performance reasons, CORE functions never look at the symbol table UNLESS an equivalent CORE::GLOBAL function has been declared at compile time. Unfortunately, you have to write that CORE::GLOBAL function and get it just right to simulate the calling conventions of the real function. Some CORE functions cannot be entirely reproduced without massive hacks, print and open for example. Since CORE::GLOBAL is global an effects all your code and all library code you have to be sure to get it exactly right or cause very hard to debug errors. Some modules, such as autodie, have to go to great lengths to wrap around core functions.

    But here, let me show you where the gun locker and ammo are...

    my @return = eval "$function(\@args)";
    

    ...of course, this is a massive security and maintainability hole. Don't do it.