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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |