I have inherited about 900 perl and shell scripts. A lot of code was cut/pasted over the last 5 years and my boss has asked me to try and clean things up.

One issue is this:

use Getopt::Long; use Net::SSH qw(sshopen2); use Net::Ping; use Net::FTP; ...
This script DOES use sshopen2, but not Ping or FTP.

What could I do (from another perl script scanning through *.pl files) to report that this file includes but does not use Ping or FTP?

I have tried using >perl -MO=Xref,-d test.pl to generate a cross-reference list of variables, but this does not identify packages that are included, but not used.

I thought about taking test.pl and creating copies like "a.pl", "b.pl", "c.pl", "d.pl".. with the difference:

a.pl - comments out the "use Getopt::Long" line

b.pl - comments out the "use Net::SSH qw(sshopen2)" line ..

Then doing a perl -c on each copy to see if I could identify useless "use" statements if things tested correctly. But this does not catch things.

I tried using

perl -MO=Deparse test.pl
Hoping that this would expand:

&sshopen2() into

&NET::SSH::sshopen2()

And I could grep the expanded output to find the packages that ARE used and do my own bookeeping, but Deparse does not fully-qualify function calls.

So I am kind of stumpped. Any thoughts?


In reply to 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.