This is probably much more rudimentary than the other responses, but I thought I'd throw it out there. I wrote such a parser a few years ago, and this morning, updated my CPAN module Devel::Examine::Subs to calculate the line numbers where each sub starts and ends.

It's rudimentary because it relies on the fact that each sub must start at the first column of the file, and the closing brace must be in the same position (this means that subs created within other blocks won't get caught).

Here's a script that uses the module, and a snip of some example output:

#!/usr/bin/perl use warnings; use strict; use Devel::Examine::Subs; my $des = Devel::Examine::Subs->new(); my $file = '../business-isp/lib/Business/ISP/Object.pm'; my $subs = $des->sublist({ file => $file }); for my $sub (@$subs){ my $name = $sub->name(); my $start = $sub->start(); my $end = $sub->stop(); my $count = $sub->count(); print "Sub: $name\n\tStart line: $start\n" . "\tEnd line: $end\n\tTotal lines: $count\n\n"; }

Output:

Sub: build_stack Start line: 363 End line: 392 Total lines: 29 Sub: schema Start line: 455 End line: 511 Total lines: 56 Sub: new Start line: 117 End line: 207 Total lines: 90 Sub: DESTROY Start line: 767 End line: 771 Total lines: 4 Sub: string_to_date Start line: 659 End line: 686 Total lines: 27 Sub: configure Start line: 208 End line: 300 Total lines: 92

-stevieb


In reply to Re: Retrieving start and end line numbers of a subroutine in a Perl file by stevieb
in thread Retrieving start and end line numbers of a subroutine in a Perl file by anilfunde

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.