I am writing a Perl module that will be loaded from the main script. This module uses routines that are defined in the main script (where I am not an maintainer).
For one of the routines, the main script requires an extension, but I do not want to fix the main script. Instead, I want to override the function in my module and save the link to the original routine. If an override is called, I want to call the original subroutine, and then do some extra processing (if required).
character module code
my $referenceToOriginalSub; sub inititialize() { $referenceToOriginalSub = \&originalSub; undef &originalSub; *originalSub = \&overrideSub; } sub overrideSub() { #call original within mainscript &$referenceToOriginalSub(@_);
This does not work because it ends with infinite recursion. Obviously, the link to originalSub also points to its substitution.
So, could you point me in the right direction, how to avoid infinite recursion?
source share