Greetings fellow monks!

I need to dynamically take a reference to a core function and assign it to my own (basically create an alias for it).

The background is that it should be possible to change how to handle module errors depending on import flags. The module has its own error() function, that is only called when module related errors happen, and that then either dies, or warns, or more or less prints the error message (depending on the mentioned import flags).

Modifying $SIG is not an option, since only module errors should be affected (all the 'normal' 'die's/'warn's that are unrelated to module logic should go untrapped). Calling 'die' or 'warn' from inside the function is not really an option either, since it messes up caller() information.
So I was thinking more of something like

sub import { ... no strict 'refs'; *{__PACKAGE__.'::error'} = $flag eq 'warn' ? .... # reference to ' +warn' ... } ... error('Something went wrong!') if $a != $b;

All the docs I've browsed only contain information on overloading core functions, and one of them even stated explicitly that you can't take a reference to core funcs through CORE / CORE::GLOBAL (that was the first thing I tried). Searching google and perlmonks had pretty much the same result.

So is it possible at all to create a direct reference to a core function? Or is there a better way?

Thanks in advance!

In reply to Creating a reference to a core function by random_perler

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.