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

module, perl, static-analysis

Solution

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.

Problem

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.

Original source