Use PPI to rewrite a subroutine in a package

Viewed 67

Is there a way to use PPI framework to rewrite the content of a subroutine?

Example (stripped to essentials):

  my $Document = PPI::Document->new( $file );
  my $subs = $Document->find( sub { $_[1]->isa('PPI::Statement::Sub') and $_[1]->name } );
  foreach my $sub ( @{ $subs } ){
    if ($sub->block()) {

      # here: if block() (actual code of the sub) meet some conditions,
      # I'd like to replace it with f.e. "return -1";

    } 
  }

The change will be then saved in the $file.

0 Answers
Related