in reply to Possible to change package of code reference?

Re eval sub body without the package and assign to $func.

But I don't understand why you need to do it, there is no package var in the shown code.

(Please don't use dumper for printing the text)

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

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

Replies are listed 'Best First'.
Re^2: Possible to change package of code reference?
by nysus (Parson) on Oct 21, 2018 at 18:42 UTC

    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

      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

        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