in reply to Creating a reference to a core function

AFIK, you can't reference to core functions because there are build-ins, i.e. written in C, and not normal Perl functions. The only way to pass them around is to enclose them in an (anonymous) subfunction like shown by tye above. In general you should know that most build-in functions are using prototypes, e.g. pop awaits an array as first argument. So you would have to rebuild this prototypes in your subfunction to get the same usage syntax. I'm not sure if you can use prototypes with anonymous subfunctions, but you could still create a named one and reference it.

Replies are listed 'Best First'.
Re^2: Creating a reference to a core function
by random_perler (Initiate) on Apr 28, 2008 at 17:24 UTC
    The problem with anonymous subs is that it still messes up caller() information and will display the line where the sub was defined as the error source. And rebuilding the behavior with prototypes leaves me basically with the same problem.

    I had a look at the source of Carp to see how it deals with that, and it seems that most of the (evil) magic comes from goto jumps and tracking the callers package name to exclude calls from those that are considered 'internal' (i.e. Carp itself).
    And since this all has already been invented, written and debugged, I'll go back to loading Carp and referencing to either carp() or croak() (which works as intended).

    Thinking about it, I'd guess that core functions 'live' in a different memory space than functions and variables generated on compile / run. So it probably makes good sense that you can't take a reference to them.

    It's just a bit 'disappointing' to run into some kind of limit in a language that seems to have none ;)

    Thanks alot for the replies!