http://www.perlmonks.org?node_id=1054253

Gertjan has asked for the wisdom of the Perl Monks concerning the following question:

Hi!

We have a lot of smaller and larger Perl-scripts and applications. In the course of time, new functionality is added and old code gets refactored or removed. Often people forget to remove imported modules when they are no longer needed, partly because it can be hard to see if a module is actually used. This adds a lot of unnecessary dependencies to the software.

How do you guys check which imports are actually needed? Most of the times I find myself searching through the code for occurrences of the module, while checking CPAN to see which symbols are automatically imported. This however is a very cumbersome method, and prone to error.

Thanks!
Gertjan

  • Comment on How do I check if imports are actually used?

Replies are listed 'Best First'.
Re: How do I check if imports are actually used?
by Anonymous Monk on Sep 16, 2013 at 09:00 UTC
Re: How do I check if imports are actually used?
by LanX (Saint) on Sep 16, 2013 at 13:37 UTC
    2 possibilities come to mind

  • Use B::Xref to parse each source file potentially using your modules

  • extend deprecated functions to log the caller each time they are called.

    Attribute::Handlers could be used to build such wrappers.

    Update

  • you can also extend the importer() of your modules to check the importing files.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re: How do I check if imports are actually used? (script)
by tye (Sage) on Sep 16, 2013 at 16:28 UTC
Re: How do I check if imports are actually used?
by Your Mother (Archbishop) on Sep 16, 2013 at 13:48 UTC

    There's also Hook::LexWrap. Not really recommending but I think it could be made to work (with quite a bit of coding).

      Maybe overkill.

      I think goto  &sub should already be caller transparent.

      And the OP doesn't need post hooks.

      Cheers Rolf

      ( addicted to the Perl Programming Language)