Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a tool for extracting all variable, module, and function names from a Perl module file?

My apologies if this is a duplicate; I may not know the proper terms to search for.

I am tasked with analyzing a Perl module file (.pm) that is a fragment of a larger application. Is there a tool, app, or script that will simply go through the code and pull out all the variable names, module names, and function calls? Even better would be something that would identify whether it was declared within this file or is something external.

Does such a tool exist? I only get the one file, so this isn't something I can execute -- just some basic static analysis I guess.

like image 778
romandas Avatar asked Aug 27 '09 20:08

romandas


People also ask

What is INC perl?

@INC is a special Perl variable that is the equivalent to the shell's PATH variable. Whereas PATH contains a list of directories to search for executables, @INC contains a list of directories from which Perl modules and libraries can be loaded.

What is the difference between use and require in perl?

use is evaluated at compile-time, require at run-time. use implicitly calls the import method of the module being loaded, require does not. use excepts arguments in addition to the bareword (to be passed to import), require does not.

What is a perl namespace?

In perl namespaces are called "packages" and the package declaration tells the compiler which namespace to prefix to our variables and unqualified dynamic names.


1 Answers

Check out the new, but well recommended Class::Sniff.

From the docs:

use Class::Sniff;
my $sniff = Class::Sniff->new({class => 'Some::class'});

my $num_methods = $sniff->methods;
my $num_classes = $sniff->classes;
my @methods     = $sniff->methods;
my @classes     = $sniff->classes;

{
  my $graph    = $sniff->graph;   # Graph::Easy
  my $graphviz = $graph->as_graphviz();

  open my $DOT, '|dot -Tpng -o graph.png' or die("Cannot open pipe to dot: $!");
  print $DOT $graphviz;
}

print $sniff->to_string;
my @unreachable = $sniff->unreachable;
foreach my $method (@unreachable) {
    print "$method\n";
}

This will get you most of the way there. Some variables, depending on scope, may not be available.

like image 68
Robert P Avatar answered Oct 21 '22 04:10

Robert P