pid has asked for the wisdom of the Perl Monks concerning the following question:

Hi fellow monks!

Say for the sake of convenience I want to write a script that "symbolic-referencely" wraps Perl's built-in functions like this: &{$ARGV[0]};

Then I can call the script like this: $ ./wrapper chr 128

But it keeps getting me errors like this:
Undefined subroutine &main::chr called at ./wrapper line n.

I know there's such a node: "How do I get a reference to a builtin function?". But please...is there some black magic available that can get me there? I'll appreciate your kindness of sharing your deepest knowledge of Perl.

Ta!

Replies are listed 'Best First'.
Re: Builtin function and reference
by NetWallah (Canon) on Sep 01, 2011 at 05:03 UTC
    As mentioned in your referenced node on "How do I get a reference to a builtin function?", you cant.

    Here are alternatives:

    Wrap the function:

    >perl -e "my %f=(chr=>sub{chr(shift)}); print $f{$ARGV[0]}->($ARGV[1] +)" chr 35 # >perl -e "my %f=(chr=>sub{chr(shift)}); print $f{$ARGV[0]}->($ARGV[1] +)" chr 36 $
    Use eval (more general, but potentially unsafe):
    >perl -e "print eval qq|@ARGV|" sqrt 2 1.4142135623731 >perl -e "print eval qq|@ARGV|" chr 128 Ç

                "XML is like violence: if it doesn't solve your problem, use more."

Re: Builtin function and reference
by kcott (Archbishop) on Sep 01, 2011 at 05:02 UTC

    You've provided no code nor any context. I'll take a stab and suggest the black magic you're after is eval.

    $ perl -Mstrict -wE 'say eval qq{@ARGV}' chr 128 €

    Please read How do I post a question effectively?. A better question will get you a better answer.

    -- Ken

Re: Builtin function and reference
by ikegami (Patriarch) on Sep 01, 2011 at 05:44 UTC
    chr is an operator, not a subroutine, and thus doesn't exist in the symbol table, and thus won't be found by a symbolic reference.
Re: Builtin function and reference
by pid (Monk) on Sep 01, 2011 at 05:44 UTC

    Thanks for all the helpful answers.
    Thanks ikegami for pointing out the difference.
    I didn't post my code because it's just a single line: &{$ARGV[0]}($ARV[1]);.

    I think eval is the answer to my question. And thanks again, everybody.