I don't quite understand what you mean when you say that the module is reloaded. Are you using Module::Reload or somesuch? A little more information about that would probably be helpful.
As for wrapping a subroutine you usually can use this format:
my $old = \&IWL::Script::setScript;
*IWL::Script::setScript = sub {
...;
goto &$old;
};
You assign to
*IWL::Script::setScript at runtime, after you've saved away the old code in
$old. Using
goto here is good as your wrapper subroutine will be transparent to the original subroutine and not confuse it (or e.g.
Carp). If you want to change a parameter you replace the element in
@_ using
push/
pop,
unshift/
shift or
splice or assign to
@_ = .... You generally don't want to do
$_[...] = ... because
@_ is aliased. In your case you might want to do
# Untested.
use Scalar::Util 'reftype';
use 5.010; # For ~~.
my $old = \&IWL::Script::setScript;
*setScript = sub {
my ($self, $param) = @_;
$param = $p2js->covert($param)
if reftype($param) ~~ 'CODE';
@_ = ($self, $param);
# or:
#splice @_, 1, 1, $p2js->convert($param)
# if reftype($param) ~~ 'CODE';
goto &$old;
};
Hope this helps,
lodin