Here is some code that you can run that will certainly help you decide if a module is being used.

There are many bugs in this code. You will notice if you run it on itself: it does not think that Data::Dumper is being used. This is because Dumper is not being explicitly imported. And if the code has "C"."G"."I"->new() in it this program will not find it.

I assume that for most of the programs that you have this little program will say that all of the modules are being used, and then you can spend your time looking at the programs that have modules that may not be being used.

use strict; our $exclude = { map({ $_ => $_ } qw ( strict constant warnings )), }; use Data::Dumper; if (0) { print Dumper $exclude; } while (my $file = shift) { if (!open(FILE, "<$file")) { print("Could not open $file\n"); next; } print "Checking $file\n"; my @imported = (); my @modules = (); my $module = {}; my $imports = {}; while (my $line = <FILE>) { chomp $line; if ($line =~ /^\s*use\s+/) { if (my ($package, $list) = ($line =~ /^\s*use\s+([^\s]+)\s ++(.*);/)) { my @export; eval { @export = eval('('. $list . ')'); }; if ($@) { print "Could not evaluate $list\n"; } next if ($exclude->{$package}); if (defined($module->{$package})) { printf("Package $package `use'd more than once\n"); } else { push(@modules, $package); $module->{$package} = 0; } for my $key (@export) { if (defined($imports->{$key})) { printf("Subroutine $key imported more than once +.\n"); } $imports->{$key} = $package; } push(@imported, @export); } elsif (my ($package) = ($line =~ /^\s*use\s+([^\s]+)\s*; +/)) { next if ($exclude->{$package}); if (defined($module->{$package})) { printf("Package $package `use'd more than once\n"); } else { push(@modules, $package); $module->{$package} = 0; } } else { print "Could not parse '$line'\n"; next; } } print "I don't handle requires yet\n" if $line =~ /^\s*require +/; } seek(FILE, 0, 0); # I have a dual personality, so I wrote a 2 pass checker. unless (@modules || @imported) { print "No modules in $file\n"; next; } my $temp = join('|', grep({ !$exclude->{$_} } @modules)); my $reg1 = $temp ? qr/\b($temp)[^:]/ : undef; my $temp = join('\|', grep({ !$exclude->{$_} } @imported)); my $reg2 = $temp ? qr/($temp)/ : undef; while (my $line = <FILE>) { chomp $line; next if $line =~ /^\s*use\s+.*;/; # don't look at the use; next if $line =~ /^\s*require\s+.*;/; # don't look at the u +se; if (defined($reg1) && (my ($module_name) = ($line =~ $reg1))) +{ # print "$line [$module_name]\n"; $module->{$module_name}++; } if (defined($reg2) && (my ($import) = ($line =~ $reg2))) { if (my $module_name = $imports->{$import}) { $module->{$module_name}++; # print "$line ($import) [$module_name]\n"; } else { die; } } } for my $key (keys %$module) { if ($module->{$key}) { printf "Module $key used %d times.\n", $module->{$key}; } else { print "Module $key not used.\n" unless $module->{$key}; } } print "\n"; }
Even more bugs or things todo:
  • __END__ is not detected
  • comments are not removed
  • the regular expresions are fragile and likely wrong.
  • False positives are a much bigger problem, but including a module that is not used is not horrible.
    -- gam3
    A picture is worth a thousand words, but takes 200K.

    In reply to Re: How do I detect what modules are not being used? by gam3
    in thread How do I detect what modules are not being used? by FatDog

    Title:
    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.