Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Hard time to make use of PPI

by PetaMem (Priest)
on Apr 20, 2008 at 09:30 UTC ( #681760=perlquestion: print w/ replies, xml ) Need Help??
PetaMem has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

we've recently acquired a significant perl codebase that clearly has been growing "organically" for some time... As we need to integrate it into our existing systems and need to make sure we can maintain it long term, work to clean-up/refactoring has commenced.

One of the tasks is the "unused subroutine cleanup" (seems common - see here and here) or at least an identification of potential cruft. We thought that instead of a regexp massacre, we'd like to go for PPI

PPI however seems no tame beast. Documentation is terse and maybe outdated. At least when trying to set up some examples according to documentation, we fail (bitterly). From the docs in PPI::Document:

# Find all the named subroutines my @subs = $Document->find( sub { $_[1]->isa('PPI::Statement::Sub') and $_[1]->name } );

Eh - well - no sir. This will put some ARRAYREF into the first element of @subs and that's it. Dumpering that ARRAYREF reveals a monstrous PPI parse tree. Basically equivalent of the Perl DOM we had in first place. Of course, if we let print out $_[1]->name within the find sub, there the names are, but unfortunately you cannot simply return the name. So for now, the only workaround to get PPI to DWIM, would be to save the names within the find sub into some global variable. Like so:

my @subs; $document->find( sub { if($_[1]->isa('PPI::Statement::Sub') && $_[1]->na +me()) { push @subs, $_[1]->name(); } });

That does the trick, but seems quite hacky, quite nonconformat to what the documentation says (but the docs seem to be wrong in that case anyway).

So the question is - are we missing some general concept of PPI, or are the docs wrong - or both?

Bye
 PetaMem
    All Perl:   MT, NLP, NLU

Comment on Hard time to make use of PPI
Select or Download Code
Re: Hard time to make use of PPI
by brian_d_foy (Abbot) on Apr 20, 2008 at 15:18 UTC

    Don't try to do it in one step. The find selects elements for the PDOM. Once you have the right elements, go through them to pull out the bit that you want.

    use PPI; my $Document = PPI::Document->new( $ARGV[0] ); my $subs_ref = $Document->find( sub { $_[1]->isa('PPI::Statement::Sub') }); my @sub_names = map { $_->name } @$subs_ref; print "@sub_names\n";

    The docs are fine. The find does the selection, but you're trying to force it to do everything else too. :)

    Good luck,

    Update: If you're just stealing snippets from the docs, then you are doing it wrong. I think you're talking about the snippet in the top-level PPI::Document docs, but you're assuming a lot about what it is saying. You need to read about the find method in PPI::Node, where the docs are quite clear. If a method doesn't do what you think it does, read the docs on that method. :)

    Update 2: I've made the example in PPI::Document better.

    --
    brian d foy <brian@stonehenge.com>
    Subscribe to The Perl Review

      Thanks for your clarification. However, it's not "the docs" and "me", its only "the docs". What I presented was code from "the docs". So either they are fine, or they try to force to do everything else.

      Considering the fact that your given code differs from that in "the docs" and works, "me"'d be careful to give "the docs" a general absolution. ;-)

      Anyway, our unused subroutines identifier started just breathing.

      Bye
       PetaMem
          All Perl:   MT, NLP, NLU

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://681760]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2014-12-20 12:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (95 votes), past polls