Is there a tool to extract all variable names, modules, and functions from a Perl module file?

My apologies if this is a duplicate; Perhaps I do not know the right conditions for the search.

I am tasked with analyzing the Perl module file (.pm), which is a fragment of a larger application. Is there a tool, application, or script that just goes through the code and pulls out all the variable names, module names, and function calls? Even better would be something to determine if it was declared in this file or something external.

Is there such a tool? I only get one file, so this is not something I can do - just some basic static analysis, I think.

+5
source share
6

, Class::Sniff.

:

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";
}

. variables, , .

+9

, Perl. PPI.

, :

#!/usr/bin/perl

use strict;
use warnings;

use PPI::Document;
use HTML::Template;

my $Module = PPI::Document->new( $INC{'HTML/Template.pm'} );

my $sub_nodes = $Module->find(
    sub { $_[1]->isa('PPI::Statement::Sub') and $_[1]->name }
);

my @sub_names = map { $_->name } @$sub_nodes;

use Data::Dumper;
print Dumper \@sub_names;

, :

     ...
     'new',
     'new',
     'new',
     'output',
     'new',
     'new',
     'new',
     'new',
     'new',
     ...

HTML/Template.pm . , PDOM .

+8

CPAN Class:: Inspector

use Class::Inspector;

# Is a class installed and/or loaded
Class::Inspector->installed( 'Foo::Class' );
Class::Inspector->loaded( 'Foo::Class' );

# Filename related information
Class::Inspector->filename( 'Foo::Class' );
Class::Inspector->resolved_filename( 'Foo::Class' );

# Get subroutine related information
Class::Inspector->functions( 'Foo::Class' );
Class::Inspector->function_refs( 'Foo::Class' );
Class::Inspector->function_exists( 'Foo::Class', 'bar' );
Class::Inspector->methods( 'Foo::Class', 'full', 'public' );

# Find all loaded subclasses or something
Class::Inspector->subclasses( 'Foo::Class' );

:: Sniff; .

+7

- , , PPI. Module::Use::Extract; , - PPI PerlDOM.

, , .

+3

, , " ".

Perl. , . main ( ):

for(keys %main::) { # alternatively %::
  print "$_\n";
}

My/Package.pm , , My::Package, %main:: %My::Package:: . . perldoc perlmod - , , , , ( Perl - , ).

+2

All Articles