Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hey LanX,

Recently I updated one of my modules to use PPI, so this was fresh in my memory. This won't get you all the way, but it's a sane way to remove all POD and comment elements, as well as providing you an array of the sub names in the order they are currently listed in the file you're working on.

use warnings; use strict; use PPI; my $file = 'Test.pm'; my $outfile = 'Nopod.pm'; my $ppi_doc = PPI::Document->new($file); $ppi_doc->prune('PPI::Token::Pod'); $ppi_doc->prune('PPI::Token::Comment'); my @subs; for (@{ $ppi_doc->find('PPI::Statement::Sub') }){ push @subs, $_->name; } $ppi_doc->save($outfile); print "$_\n" for @subs;

Hope this helps!

Update: added code to catch the original order of subs in the file.

Update2: I monkeyed around with one of my own modules Devel::Examine::Subs (v1.56+) that does subroutine maintenance, and with some trickery (as a PoC), wrote a script that strips POD and comments, gets the order of the sub names, removes all subs from each file, and inserts them back in (starting at the first line where the first sub used to start in each file) in the order taken from the first argument. Just pass the script the two files, starting with the one who's sub order you want to keep.

use warnings; use strict; use Devel::Examine::Subs; use PPI; # unneeded, technically due to above use if (@ARGV != 2){ print "Usage: ./script.pl Good.pm Old.pm\n"; exit; } my @files = @ARGV; my @order; for my $file (@files){ my $outfile = "$file.new"; my $ppi_doc = PPI::Document->new($file); $ppi_doc->prune('PPI::Token::Pod'); $ppi_doc->prune('PPI::Token::Comment'); my @subs; for (@{ $ppi_doc->find('PPI::Statement::Sub') }){ push @subs, $_->name; } # set the order in the global var @order = @subs if ! @order; $ppi_doc->save($outfile); my ($des, $sub_obj_hash); { $des = Devel::Examine::Subs->new(file => $outfile); $sub_obj_hash = $des->objects(objects_in_hash => 1); } $ppi_doc = PPI::Document->new($outfile); $ppi_doc->prune('PPI::Statement::Sub'); $ppi_doc->save($outfile); $des = Devel::Examine::Subs->new(file => $outfile); my $first_line = $sub_obj_hash->{$subs[0]}->start; my @code; for (@order){ push @code, @{ $sub_obj_hash->{$_}->code }; } $des->inject(line_num => $first_line, code => \@code); }

I know others have provided solutions already, but I'm curious to see how far this actually gets you. It assumes both files have the same subroutines, and the data is written to the same files passed in, with a ".new" extension.

Update: if you've read this far, you see what I'm trying to do. I'm going to take the CPAN namespace Devel::Examine::File so I can extend what I've already done, and make it officially file-based. The want that OP desired is something I've wanted to expand into anyways, and it will alleviate some of the hacks I have in the current module that it just shouldn't be doing.


In reply to Re: Tool for diff'ing/anaylyzing two branches of same Perl code? by stevieb
in thread Tool for diff'ing/analyzing two branches of same Perl code? by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-23 16:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found