Multi-language line-number/keyword perl utility

Instructional HowTos, posted by users. No questions here please.

Moderators: AmigoJack, helios, bbadmin, Bob Hansen, MudGuard

Post Reply
User avatar
Cloink
Posts: 81
Joined: Thu Sep 09, 2004 1:44 pm

Multi-language line-number/keyword perl utility

Post by Cloink »

Code: Select all

#!perl

=head1 Copyright

*****************************************
*                                       *
*  Copyright (c) 2006, CloinkSoft       *
*                                       *
*  mailto:Cloink_Friggson@ntlworld.com  *
*                                       *
*****************************************

=head1 LineNos - What it does

Interrogates passed filename (appropriately qualified) for it's sub-routine word,
and list each with line-number, especially useful if called from TextPad as a tool
with a "Regexp to match output" of "^ *([0-9]+)", as this allows you to quickly
navigate to a particular function / sub / procedure / whatever match you desire,
by double-clicking the line shown in the Command Results window.

It expects the keyword to be at the beginning of the line, with optional leading spaces,
and it does a case-insensitive match to cater for as many languages as possible.

Note that JavaScript gets special treatment and finds both named functions, and
further methods of those functions, sorting the methods into alphabetical order.

e.g.
    function JS_Func(){ null; } // possibly a constructor func for an object
    JS_Func.meth1 = function(){ null; } // static meth but caters for .prototyped meths too
lists JS_Func AND JS_Func.method - the latter indented, and a new constructor gets a blank line
before it to help separate one from the next when you have a lot of methods.

I don't program in Java, but I imagine the JS special treatment will apply to that
and many other OO languages too.

=cut


$, = "" ; # between each argument to print ("field" separator)
$\ = "" ; # between each call to print    ("record" separator)

my ( $f, $prt, $l, $i, $nuc, $fl, $srch, $js, $t );
@meths; # methods for JS
%mlnos;

format STDOUT =
@>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$l, $prt
.

$f = $ARGV[0];

=head2 Specify your file types and search strings here

The search-string is bracketed so you can give a list of possible words

e.g. Oracle PL/SQL
    PROCEDURE|FUNCTION

The sub-routine name can be quoted - for me, this allows me to use the perl-script
against my Oracle table-scripts which call a sub-script prior to each table-definition

e.g. Ora script
    @TabIni "TABLE_NAME"

=cut

if( $f =~ /\.js$/ ){ # JavaScript
    $js = 1;
    $srch = 'function' ;
}
elsif( $f =~ /\.p[lm]$/ ){ # perl
    $srch = 'sub';
}
elsif( $f =~ /\.pk[gbdhs]$/ ){ # Oracle packages
    $srch = 'PROCEDURE|FUNCTION';
}
elsif( $f =~ /\.tab$/ ){ # Oracle table script
    $srch = 'START ~UD.TabIni';
}
else{
    die "Unknown file type.";
}

open IN, $f || die "Can't open $f" ;

while( <IN> ){

    if( $_ =~ /^\s*($srch)\s+['"]?(\w+)['"]?/i ){ # " # case-insensitive, optional quotes round word, which will rarely be required but doesn't harm to have 'em, and my tab scripts need it

        if( $js && $t ){ # switching from major func to another, but not first major func
            $i=-1;
            map { $l = $mlnos{$_}; $prt = " $_"; write; }
            sort { return( uc $a lt uc $b ? -1 : 1 ) } @meths ;
            @meths = ();
            @mlnos = ();
            print "\n" ; # so don't put a blank line at v top
        }

        $t = $2;
        $l = $.;
        $prt = $t;
        write;
    }

    if( $js && $_ =~ /^ *$t\.(prototype\.)?(\w+) *= *function/ ){
        push @meths, $2;
        $mlnos{$2} = $.;
    }
}

close IN;

1;
OnlyTextPad
Posts: 41
Joined: Sat May 20, 2006 9:10 pm
Location: Helsinki
Contact:

Wanderfull idea !!

Post by OnlyTextPad »

You could it also :

Ctrl + F5
note the space
find what: "sub "
in files: *.pl *.pm *.cgi
in folder: C:\Temp\MyFolderName

Of course this is finding all subs only in perl (and possible more ) on all your project files and your approach is cleaner for only one file ... and for many languages ....

Anyway with perl2exe I have already GoToSubs in my path ; ) Thanks !!!!
Post Reply