in reply to How do I detect what modules are not being used?

To start with, I'd grep the entire file for /\buse\s+([\w:]+)\b/ to find all the modules being used (at least via 'use' in any case).

Then for each module, search for /(?<!use\s+)$module\b/ to find all the OTHER uses of it in the file.

If the name of the module never appears anywhere else in the file, then it's almost certainly not used. :) Adam K

Replies are listed 'Best First'.
Re^2: How do I detect what modules are not being used?
by Limbic~Region (Chancellor) on Apr 07, 2005 at 13:04 UTC
    adamk,
    If the name of the module never appears anywhere else in the file, then it's almost certainly not used

    I assume you are thinking that if I do:

    use CGI; my $q = CGI->new();
    Then you will be able to tell if I have actually used that module? A huge portion of modules are not OO and export functions that do not need the module name to be invoked. Additionally, the module may be exporting constants.

    The best this method can do is attempt to confirm actual use. Even then it is subject to break since the module name may appear in comments or code as false positive. Even reducing the comments and POD with perl -MO=Deparse will not be fool proof. Your method will not be a good indicator of modules not actually being used and will be a poor indicator of modules that are being used.

    Cheers - L~R

      I certainly don't consider it a complete method for handling all cases, but modification of large groups of modules tends to go like this. Run some wide scans, fix the obvious cases, and then work your way down the curve of diminishing returns.

      A minor change to check for the use statement having params, or the module being called having an @EXPORT (compulsory export) would largely resolve your issues.

      The poster is not going to automagically modify the files, he's just looking for clues. If a report can identify the 25-75% of obvious cases, then it's a good high yield starting point.