in reply to Re: Possible to change package of code reference?
in thread Possible to change package of code reference?

The function was generated by a config file with Perl code in it. The "run" commands in the subroutine are getting run by an object in the "Spin::Command::spin" class. But I want the commands to run under a method in a different class.

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

  • Comment on Re^2: Possible to change package of code reference?

Replies are listed 'Best First'.
Re^3: Possible to change package of code reference?
by LanX (Saint) on Oct 21, 2018 at 20:05 UTC
    I told you how, regex the new package into the text and eval it.

    $func= eval qq( sub {$newtext} );

    Another possibility is to temporarily patch &run before executing $func.

    local *Spin::Command::spin::run = sub { "new code" }

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      Ok, well, if you were suggesting that I modify the output of the deparser, that's what I did and was able to get things working. Thanks!

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
      $nysus = $PM . ' ' . $MCF;
      Click here if you love Perl Monks

        Be warned that this works just in your simple case.

        Any closure vars would lose their share and B::Deparse fails at some edge cases.

        It's not a fool save technique.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      Are you talking about re-evaluating the output from B::Deparse?

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
      $nysus = $PM . ' ' . $MCF;
      Click here if you love Perl Monks