Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i count the respective lines for each sub in my perl code?

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

like image 374
Mithaldu Avatar asked Dec 12 '22 20:12

Mithaldu


2 Answers

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;
like image 90
Pedro Silva Avatar answered Dec 15 '22 09:12

Pedro Silva


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.

like image 39
Ether Avatar answered Dec 15 '22 08:12

Ether