To find the subs it uses a simple regexp that seems to work for my code, but I'm pretty consistent indenting things and only use two kinds of subs:
sub foo { one line sub: accessors and the like } sub bar { multiline subs with proper indentation ... }
#!/usr/local/bin/perl use strict; # use warnings; our ($opt_d); use Getopt::Std; getopts('d:') or die "Usage:\n $0 [-d min_lines] file1 file2 ...\n\n"; use Scalar::Quote 'S'; undef $/; my @count; my $subs; while(<>) { for (/^sub\s[^\n]*{(?:\s*(?:#[^\n]*)?\n.*?^|[^\n]*)}\s*$/smg) { my @lines = split /\n/; $count[@lines]++; $subs++; defined $opt_d and @lines >= $opt_d and print S($_, 80), " at $ARGV has ".scalar(@lines)." lines\n"; } } unless ($subs) { print "no soubroutines found\n"; exit(1); } my ($t, $t2); for (1..@count) { if ($count[$_]) { printf("%d lines: %d subs (%4.1f%%)\n", $_, $count[$_], $count[$_]*100/$subs); $t += $_*$count[$_]; $t2 += $_*$_*$count[$_]; } } my $m = $t/$subs; printf("\nmedia: %4.1f, deviation: %4.1f\n", $m, sqrt($t2/$subs-$m*$m)); __END__ =head1 NAME sublines - how much lines per sub do you use? =head1 SYNOPSIS sublines [-d min_lines] file1 file2 ... =head1 DESCRIPTION sublines counts the number of lines on perl subroutines and reports some statistics. =head1 OPTIONS =over 4 =item 1 -d min_lines makes C<sublines> print any sub with more lines than C<min_lines>. =back =head1 COPYRIGHT This code is on the public domain. =cut
|
|---|