Preventing modules from loading

drhyde on 2007-04-25T14:41:03

$ perl -Ilib -MNo=Data::Dumper -e 'use Data::Dumper'
Data::Dumper isn't allowed to load.
Compilation failed in require.
BEGIN failed--compilation aborted.

This is actually useful. Imagine you've written some code which, if some optional module is present, will do different stuff. You need to test both with and without the optional module before releasing your code. The usual way to do that is to monkey around in your perl libs and temporarily rename the appropriate .pm file or something like that so that perl can't "see" it. That sucks.

With this cunning trick, simply use No qw(Some::Random::Module) in a test file and the effect will be as if it wasn't there.


Test::Without::Module

rjbs on 2007-04-25T15:21:14

http://search.cpan.org/dist/Test-Without-Module/

Re:Test::Without::Module

drhyde on 2007-04-26T13:18:37

I did consider playing games with subroutines in @INC like that module does, but my version will also "infect" child processes, by creating dummy modules and fiddling with PERL5LIB.

Re:Test::Without::Module

rjbs on 2007-04-26T14:19:31

I have a branch of TWM, which I keep meaning to release (I have comaint, I believe) that lets you fiddle with INC in a less annoying way, by creating temp dirs with unloadable versions in it. That's needed to support old perls anyway, before coderefs in INC.

Anyway, I look forward to seeing your solution, too.

Re:Test::Without::Module

drhyde on 2007-05-02T14:07:06

It's here. Mostly untested.

Re:Test::Without::Module

drhyde on 2007-10-01T09:29:42

And it's also now in the latest dev release of Devel::Hide.

Also, for testing without XS

dagolden on 2007-04-25T18:03:39

Test::NoXS -- for testing with modules that fall-back to pure perl if XS isn't available.