Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Changing every subroutine in many perl scripts

by toolic (Bishop)
on Jul 24, 2012 at 15:07 UTC ( #983434=note: print w/replies, xml ) Need Help??


in reply to Changing every subroutine in many perl scripts

PPI - Parse, Analyze and Manipulate Perl (without perl)

Here is an example which can get you started: How can I write a Perl script to extract the source code of each subroutine in a Perl package?

  • Comment on Re: Changing every subroutine in many perl scripts

Replies are listed 'Best First'.
Re^2: Changing every subroutine in many perl scripts
by nitin1704 (Sexton) on Jul 25, 2012 at 02:43 UTC
    Should I use PPI::Transform to edit the subroutines?

    From the CPAN page, I got that I could do something like this:

    my $transform = PPI::Transform->new( param1 => 'value1', param2 => 'value2', ); # Change a file in place $transform->file( 'Change.pm' );

    But I don't understand what are the params and values when creating the new transform object.

      Okay, I got it now, after going through the CPAN documentation a bit more.

      use strict; use warnings; use PPI; my $file = '/path/to/perl/script.pl'; my $Document = PPI::Document->new($file) or die "oops"; for my $sub ( @{ $Document->find('PPI::Statement::Sub') || [] } ) { unless ( $sub->forward ) { my @elements = $sub->children; for ( my $i = 0 ; $i < @elements ; $i++ ) { if ( ref $elements[$i] eq "PPI::Structure::Block" ) { $elements[$i]->start->add_content("my mars code"); } } } } $Document->save($file.".new");

        The loop would be better written:

        for my $sub (@{$Document->find('PPI::Statement::Sub') || []}) { next if $sub->forward; for my $child ($sub->children) { if (ref $child eq "PPI::Structure::Block") { $child->start->add_content("my mars code"); } } }

        The next if ... saves a level of indentation and is easier to understand (at least for me) than an unless.

        Using a Perl style for loop in place of the C style for loop is easier to read, easier to understand, more compact and much less likely to get wrong.

        True laziness is hard work

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://983434]
help
Chatterbox?
[Corion]: ambrus: Yeah, I read that, but it's somewhat vague as in what I really have to implement, and where/how my "other" mainloop should/needs to call AnyEvent
[Corion]: (or maybe I just work better from existing code that I munge until it works and I understand it rather than a short abstract text like "implement everything that's needed" ;) )
[ambrus]: Corion: I think in this case you can get away with only a stub for idle, one that always dies when you create it, because AnyEvent::HTTP doesn't use it, not even indirectly through AnyEvent::Handle or AnyEvent::Socket or AnyEvent::DNS.
[Corion]: The "and I understand it" part is optional.
[Corion]: ambrus: Yes but I also need to implement the file / IO watcher, because Prima has that (in Prima::File), and I need to supply the appropriate thing to make push_write etc. work with Prima
[ambrus]: Corion: yes, you need to implement the io watcher, which should be simple because Prima::File is basically that, and the timer watcher form Prima::Timer
[Corion]: ... or so I think. As I said, I'm somewhat vague on how to make AnyEvent cooperate with a callback-driven IO event loop that gives me callbacks when data is available or can be written
[ambrus]: what push_write thing? I don't think you need that. that's implemented generically by AnyEvent::Handle

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2016-12-08 12:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (141 votes). Check out past polls.