I'm having an issue with the following scenario. I'm writting a class that uses AUTOLOAD
somtimes to call functions from another module (not mine). This other module use a few functions where prototypes are defined. One example is this:
sub external_sub (\@$) { ... }
Those functions work correctly when imported directly from that module, with calls like the following:
my @arr = (1..10);
external_sub(@arr, 'something else');
Now, the problem I have when requiring that external module from my class at run time and importing that function, is that I can't find a way to pass the right arguments to it.
Inside my AUTOLOAD
function, I count on @_
only, and I don't know if there's even a way in Perl to tell apart any array passed as first argument to AUTOLOAD
. So, inside AUTOLOAD
, the only options I can think so far to redirect the call are:
no strict 'refs';
my $sym = *{"ExternalModule\::$called_sub"};
*$AUTOLOAD = $sym;
goto &$AUTOLOAD;
...or something like:
no strict 'refs';
return &{"ExternalModule\::$called_sub"}(@_);
or several similar things using the symbol table. However, the problem is how I pass the arguments to that call. If in my code I have:
package main;
use strict;
use MyModule qw(:some_external_subs); # This will import *names only but will decide later from which modules to take the actual code refs
# Here I have imported the sub 'external_sub' as symbol but it won't be actually loaded until MyModule::AUTOLOAD decides which external module will actually use to import the code for that function:
my @arr = ('some', 'values');
my $result = external_sub(@arr, 'other_argument');
Then, that's the point where AUTOLOAD
in my module will require an external module and pass the call to the actual prototyped sub external_sub(\@$)
. The problem is it will pass the received arguments as @_
, where @arr
and 'other_argument'
are now all part of a single list.
Is there any way to resolve a situation like this? Is there a way to detect what the original arguments were before becoming @_
?
Keep in mind I don't have control over the external module and the fact that it uses a prototyped function.
Thanks in advance for any comments!