reading Re: Subroutines versus straight top to bottom scripts I become curious about how many lines I use normally on my perl subs, so I wrote this little script that generates some statistics about it

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

In reply to counting the lines on your subs by salva

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.