views:

74

answers:

2

I am refactoring a rather large body of code and a sort of esoteric question came to me while pondering where to go on with this. What this code needs in large parts is shortening of subs.

As such it would be very advantageous to point some sort of statistics collector at the directory, which would go through all the .pm, .cgi and .pl files, find all subs (i'm fine if it only gets the named ones) and gives me a table of all of them, along with their line count.

I gave PPI a cursory look, but could not find anything directly relevant, with some tools that might be appropiate, but rather complex to use.

Are there any easier modules that do something like this?

Failing that, how would you do this?

Edit:

Played around with PPI a bit and created a script that collects relevant statistics on a code base: http://gist.github.com/514512

+10  A: 
my $document = PPI::Document->new($file);

# Strip out comments and documentation                                                                                                                                              
$document->prune('PPI::Token::Pod');
$document->prune('PPI::Token::Comment');

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

print map { sprintf "%s %s\n", $_->name, scalar split /\n/, $_->content } @$sub_nodes;
Pedro Silva
Man, i was making that a lot more complicated in my mind than it needed to be. Thanks a lot.
Mithaldu
+3  A: 

I'm dubious that simply identifying long functions is the best way to identify what needs to be refactored. Instead, I'd run the code through perlcritic at increasing levels of harshness and follow the suggestions.

Ether
I'm currently just trying to improve the structure of the code. Against the atrocities that are happening there, the cosmetic improvements of critic can wait, since critic basically tells me: "All of this sucks!" (One module has a 300 line sub that reaches indention level 12 three times over its body.)
Mithaldu
@mithaldu: sounds like you already have a good idea of what to tackle first :) When you run out of ideas, perlcritic is great for helping, but that might not be for a while.
Ether
No worries, Critic is the first thing I'll throw at this once it's somewhat under control (tests written, roughly restructured, etc.). After all, i did write this: Dist::Zilla::Plugin::ProgCriticTests :)
Mithaldu