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.

In reply to Re^2: perl determine a subroutine name by given line by avo
in thread perl determine a subroutine name by given line by avo

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.