Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

Ovid
  (email not shown publicly)
http://publius-ovidius.livejournal.com/
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Tuesday December 11, 2007
07:05 AM

Using vim to find unecessary modules in tests

[ #35080 ]

I see a number of tests like this:

use This;
use That;
use The::Other::Thing;
use Foo::Bar;

And when looking at all of those, I wonder which we're actually using. So I added this quick hack to my .vimrc:

map ,ut :call ConvertUseToTest()<cr>
function! ConvertUseToTest()
    s/use \(.\+\);/&\rok exists $INC{'\1.pm'}, '\1 exists in %INC';/
    s/::/\//g
    nohl
endfunction

Position your cursor on, say, the "use Foo::Bar;" line, type ",ut" ("use test") and you get this:

use This;
use That;
use The::Other::Thing;
use Foo::Bar;
ok exists $INC{'Foo/Bar.pm'}, 'Foo/Bar exists in %INC';

It's not perfect, but it's a good start. Now I run the test and verify that Foo::Bar has been loaded. I then delete the use Foo::Bar line and rerun the tests. Assuming all tests pass except the new test we've just added, then I know that it's safe to leave the use Foo::Bar line out of the code.

(If the "exists in %INC" test passes, then something else is loading the Foo::Bar module and I may as well make the loading explicit, though this is an annoying code smell -- I have two hundred test programs to fix!)

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
    • It would certainly help, but it merely tells me what loaded what, not if that loaded item is being used. My little hack tells me that the module was really loaded and after I delete the 'use' line, if the other tests pass and I see the module is no longer used, it shouldn't be listed. Devel::TraceLoad can't really tell me that, though it can tell me where the module is really loaded. I should really use that more.

  • What about Devel::Unplug then? :) You could probably automate finding what your tests load and then systematically using Devel::Unplug to remove each module and see check that they still pass.