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

Brethren, Is there a way to take a reference to a built-in perl function?

Replies are listed 'Best First'.
Re: ref to built-ins
by ambrus (Abbot) on Jun 08, 2005 at 14:41 UTC

    dave_the_m is right, there's no way to do that. This is clearly stated in perlsub:

    To unambiguously refer to the built-in form, precede the built-in name with the special package qualifier CORE::. For example, saying CORE::open() always refers to the built-in open(), even if the current package has imported some other subroutine called &open() from elsewhere. Even though it looks like a regular function call, it isn't: you can't take a reference to it, such as the incorrect \&CORE::open might appear to produce.
Re: ref to built-ins
by ikegami (Patriarch) on Jun 08, 2005 at 14:58 UTC

    You can create a wrapper to one, but you need to go through hoops to do it:

    $substr = sub { if (@_ == 2) { return substr($_[0], $_[1]); } elsif (@_ == 3) { return substr($_[0], $_[1], $_[2]); } elsif (@_ == 4) { return substr($_[0], $_[1], $_[2], $_[3]); } else { require Carp; Carp::croak("Bad arguments for substr"); } }; print($substr->('abc', 1), "\n");

    substr(@_)
    and
    substr($_[0], $_[1], $_[2], $_[3]) (unconditionally)
    won't work, because builtins have prototypes, and the
    &substr
    trick to bypass the prototype doesn't work on builtins.
    (&CORE::substr doesn't work either.)

Re: ref to built-ins
by dave_the_m (Monsignor) on Jun 08, 2005 at 14:34 UTC
    No, because built-ins aren't implemented in a way that would allow this.

    Dave.