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

Dear Monks,

Is there a way to know if some identifier is a Perl built-in operator without executing it?
I have tried with UNIVERSAL::can('CORE::GLOBAL', $identifier) but does not work:

$ perl -wde 0 main::(-e:1): 0 DB<1> use Memoize DB<2> p UNIVERSAL::can('Memoize', 'memoize') CODE(0x8402a10) DB<3> x UNIVERSAL::can('CORE', 'glob') 0 undef DB<4> x UNIVERSAL::can('CORE::GLOBAL', 'glob') 0 undef
Many thanks

Casiano

Replies are listed 'Best First'.
Re: CORE and CORE::GLOBAL operators
by ikegami (Patriarch) on Mar 04, 2008 at 10:56 UTC

    First of all, you're using the wrong tool even for memoize. can is for methods. exists(&subname) is for subs. But neither works for builtins. Keep in mind the builtins functions are closer to operators than to subs.

    What are you trying to do? If you want to check of a builtin is overrideable, you can use prototype('CORE::funcname').

    Otherwise, you'll probably have to make a list from the contents of perlfunc or the appropriate source file.

      Many thanks ikegami,

      I'm writing an AUTOLOAD function that will provide access to the CORE operators on a remote machine accessed via SSH. s.t. like:

      my $machine = GRID::Machine->new(host => $host); my $r = $machine->glob('*.c'); etc.
      I don't want to make a "blind" call. I want to know if the operator exists before building the stub.

      Many thanks

      Casiano

        prototype will do fine, then. It'll answer your question for every builtin that you don't need to handle specially anyway.

        The functions you need to handle specially are the functions like print, whose syntax cannot be duplicated in Perl (short of using source filters). For print, you'll have to create a redirector for print LIST and one for print FILE LIST.