Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses


by mikfire (Deacon)
on Aug 01, 2000 at 19:29 UTC ( #25478=sourcecode: print w/replies, xml ) Need Help??
Category: text processing
Author/Contact Info Mik Firestone This code is provided as is. If it causes the end of the world as we know it, it is not
Description: A down and dirty little script that reads a file, looking for subroutine definitions. It extracts these and then parses through a whole bunch of other files looking for calls to those functions. It isn't perfect, but it works pretty well.

Usage: list_call source file [ ...]
where source is the file from which to extract the calls and file is the file to be searched.
#!/usr/bin/perl -w
use strict;
# This code is provided as is.  There is no implied warrenty
# and it may do anything from nothing to causing the end
# of the world as we know it.  I take no responsibilty for
# and such cause.
# It is under the Artistic license, but if you use it and
# fix it, I sure would appreciate the patch.
# Mik Firestone, Aug 1 2000

die "$0 <source> <target> ..." unless @ARGV > 1;
my %keyword = ();

my ( $source, @targets ) = @ARGV;

my $package = '';
# Try to extract the keywords, precompile the regex and store
open SRC, $source or die "Couldn't open $source : $!";
while( <SRC> ) {
    $package = $1 if ( ! $package && /^package\s+(.+);/ );
    next unless ( /^sub\s+(.+){\s*$/ );

    my $name;
    $name = $1;
    next if ( substr($name,0,1) eq "_" );

    $name =~ s/\s+$//;
    $keyword{$name} = qr/([\w:{}]+)->$name/;
close SRC;

for my $file ( @targets ) {
    my ( $line );

    open FILE, $file or die "Couldn't open $file : $!";

        while( $line = <FILE> ) {
            next LINE if ( $line =~ /^\s*#/ || $line =~ /^\s*$/ );
            last LINE if $line =~ /__END__/;

            for ( keys %keyword ) {
                my $regex = $keyword{$_};
                if ( $line =~ /$regex/ ) {
                    my $starts_at = $.;
                    my $lpack = $1 || '';

                    next LINE if ( $file eq $source ) && ( $line =~ /^
+sub/ );
                    next LINE if ( $line =~ /Usage/ );
                    next LINE if ( $lpack =~ /::/ && $lpack ne $packag
+e );

                    while( $line !~ /;/ && ! eof(FILE) ) {
                        $line .= <FILE>;
                        next LINE if $line =~ /^EOF/m;
                    $line =~ s/^\s*//;
                    printf "%s (%d) %s", uc $file, $starts_at, $line;
                    next LINE;
    close FILE;
Replies are listed 'Best First'.
RE: list_call
by mikfire (Deacon) on Aug 02, 2000 at 02:21 UTC
    Wouldn't you know it? Reading this over I noticed a hidden assumption in the code.

    I am assuming you have only one package statement per file. This isn't the safest, but it does reflect the way I almost always do my code. Let me think for a few minutes and I may post a fix.

    Mea culpa,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://25478]
[Corion]: Cool. Google makes it easier for me to wean myself off Google Keep. The app now insists on saving items online before displaying them in the offline view.
[Corion]: I should maybe set up a "public" instance of my clone just to manage feature suggestions ;)

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (12)
As of 2017-04-25 11:50 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (450 votes). Check out past polls.