in reply to Re: perl determine a subroutine name by given line
in thread perl determine a subroutine name by given line

Ok, I am using SciTe (http://www.scintilla.org/SciTE.html) text editor for my coding. I wrote a small script which I can call from SciTe to give me a list of my subroutines. I am able to pass the current line number as well. I need the script to also tell me what is the current subroutine the text editor is currently in. In a simple case when the subroutines are all together and there is no other code in between, it is an easy thing and I've done it. I just want to make it have better logic.

Please have a look at my code so far:
use strict; my $filename = $ARGV[0]; my $current_line = $ARGV[1]; my (%subs,$linecnt, %types, %indents); open (IN,$ARGV[0]); my $previous_sub; my $previous_sub_line = 0; my $current_sub_name; while (<IN>) { my $line = $_; chop($line); $linecnt ++; if ($line =~ /([\s|\t]*)(sub|def|class)[\s]+(\w+)/) { $subs{$3} = $linecnt; $types{$3} = $2; $indents{$3} = $1 if ($2 ne 'sub'); if ($current_line >= $previous_sub_line && $current_line < $li +necnt && ! $current_sub_name) { $current_sub_name = $previous_sub; } $previous_sub_line = $linecnt; $previous_sub = $3; } } close IN; $current_sub_name = $previous_sub if (!$current_sub_name && $previous_ +sub); if ($current_sub_name) { print "\n>> CURRENTLY IN:\n"; print ">> $indents{$current_sub_name}$types{$current_sub_name} [$c +urrent_sub_name] at $filename line $subs{$current_sub_name}\n"; } else { print "\n>> \n"; print ">> *** Not in a function / class block ***\n"; } print "\n"; print "FUNCTION LIST:\n"; foreach my $sub (sort {$a cmp $b} keys %subs) { print "$indents{$sub}$types{$sub} [$sub] at $filename line $subs{$ +sub}\n"; }

When editing with SciTe a file which looks like this:
sub test0 { print "blabla1" } sub test1 { print "blabla" }
The output is for example:
>perl C:\temp\showsubs.pl delme.pl 7 >> CURRENTLY IN: >> sub [test1] at delme.pl line 6 FUNCTION LIST: sub [test0] at delme.pl line 2 sub [test1] at delme.pl line 6 >Exit code: 0 Time: 0.213

SciTe will then allow you to click on a line (say "sub test0 at delme.pl line 2") and will position the cursor in the relevant line in your code.
In order to test this you need SciTe and the following lines in your perl.properties file:
command.name.4.$(file.patterns.perl)=List Routines command.4.$(file.patterns.perl)=perl C:\temp\showsubs.pl $(FileNameExt +) $(SelectionStartLine)

By having this you can press CTRL+4 in SciTe and get a function list as the above, as well as current function / sub you are in based on the current line of the editor.

Replies are listed 'Best First'.
Re^3: perl determine a subroutine name by given line
by bart (Canon) on Aug 05, 2011 at 11:10 UTC
    Padre depends on PPI to analyze Perl script sources. I suggest you do the same.
Re^3: perl determine a subroutine name by given line
by Anonymous Monk on Aug 05, 2011 at 09:31 UTC
Re^3: perl determine a subroutine name by given line
by dont_you (Hermit) on Aug 06, 2011 at 06:51 UTC
    Check Geany, a lightweight IDE based on scintilla which already list functions and variables and is very similar to SciTe. I use SciTe as my primary editor, but I must admit that Geany is getting better as time passes by.