in reply to Retrieving start and end line numbers of a subroutine in a Perl file

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

Replies are listed 'Best First'.
Re^2: Retrieving start and end line numbers of a subroutine in a Perl file
by anilfunde (Initiate) on Jun 26, 2015 at 06:34 UTC
    thanks stevieb. Number of lines is icing on the cake.

      The original reason I wrote this module is because I was creating a very large multi-module application, and wanted a quick way to search through all functions/methods to see what needed tending to. I'd always be consistent like #bug 135 or #FIXME: blah. This method of use of the module will print out all sub names that contain a particular search term.

      Here's an example of that and some sample output. I ran it something like this:

      $ ./des.pl search_term

      Output:

      business-isp/lib/Business/ISP/Ledger.pm: invoice_number _gledger_write_legacy business-isp/lib/Business/ISP/User.pm: add_notes business-isp/lib/Business/ISP/Conversion.pm: plans_inf_to_db business-isp/lib/Business/ISP/Reports.pm: renewal_notices business-isp/lib/Business/ISP/GUI/Accounting.pm: home process_purchase perform_find show_plan setup confirm_purchase

      Code:

      #!/usr/bin/perl use warnings; use strict; use 5.18.0; use Devel::Examine::Subs; use File::Find; my $des = Devel::Examine::Subs->new(); my $search = $ARGV[0]; find({ wanted => \&check_subs, no_chdir => 1 }, 'business-isp/lib/Business/ISP' ); sub check_subs { if (! -f or ! /(?:\.pm|\.pl)$/){ return; } my $file = "$File::Find::name"; my @has = $des->has({file => $file, search => $search}); return if ! @has; say "\n$file:" ; say "\t$_" for @has; }

      -stevieb