random_perler has asked for the wisdom of the Perl Monks concerning the following question:
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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Creating a reference to a core function (closures)
by tye (Sage) on Apr 28, 2008 at 05:49 UTC | |
|
Re: Creating a reference to a core function
by mscharrer (Hermit) on Apr 28, 2008 at 09:08 UTC | |
by random_perler (Initiate) on Apr 28, 2008 at 17:24 UTC | |
|
Re: Creating a reference to a core function
by Anonymous Monk on Apr 28, 2008 at 04:22 UTC |