in reply to Possible to change package of code reference?
I'd be willing to bet this is an XY Problem... could you explain why you need this, preferably with an SSCCE?
If you want the code that $func points to to call a different run, you can do the following (locally redefine &Spin::Command::spin::run), although this is kinda hacky too <update> because it just changes the call to run, it doesn't affect anything else in $func, like package variables or other function calls. </update> The "real" solution would be to modify $func at its origin.
use warnings; use strict; { package Spin::Command::spin; sub run { print "Bar<<".shift.">>\n"; } sub func { run("Foo:".shift); } } my $func = \&Spin::Command::spin::func; use B::Deparse; my $deparse = B::Deparse->new; print "<<",$deparse->coderef2text($func),">>\n"; sub other_run { print "Quz[[".shift."]]\n"; } my $func_other_run = sub { no warnings 'redefine'; local *Spin::Command::spin::run = \&main::other_run; $func->(@_); }; $func->("Hello"); # prints "Bar<<Foo:Hello>>" $func_other_run->("World"); # prints "Quz[[Foo:World]]"
Update 2: Of course you can also just clobber the original func:
{ no warnings 'redefine'; *Spin::Command::spin::func = $func_other_run; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Possible to change package of code reference?
by nysus (Parson) on Oct 21, 2018 at 21:08 UTC | |
by haukex (Archbishop) on Oct 22, 2018 at 08:48 UTC | |
by nysus (Parson) on Oct 22, 2018 at 10:03 UTC |