I would like to pretend that certain modules are not available.

I'm doing something like this for the next version of Regexp::Assemble. In trying to improve the coverage of my test suite, I handled it slightly differently.

I use eval to load modules in the module, and set package variables to 1 or 0 according to whether I succeed or not. From there, I have conditional code that runs one way or another in the module, according to that result.

In the test suite, I can simply use local to override the variable, and the module then thinks that the module is loaded one instant, and then isn't the next, all in the same run.

In the main module:

use vars '$have_Storable'; $have_Storable = do { eval { require Storable; import Storable 'dclone'; }; $@ ? 0 : 1; }; # ... # code predicated on the setting of $have_Storable

Now in the test suite, I test stuff as usual, and to ensure I test both code paths exercised according to whether Storable is loaded or not, I just do something like this:

{ local $Regexp::Assemble::have_Storable = 0; my $orig = Regexp::Assemble->new->add( qw/ dig dug dog / ); my $clone = $orig->clone; is_deeply( $orig, $clone, 'clone path' ); }

If someone else installs my module and they don't have Storable installed, they'll just exercise the same codepath twice. I don't really care about that. But as the module author, I'll see if I mess up when my coverage statistics decline. That's good enough for me.

Another example of the dictum "any computer problem can be solved with another level of indirection".

- another intruder with the mooring in the heart of the Perl


In reply to Re: Simulating the absence of a module by grinder
in thread Simulating the absence of a module by Thilosophy

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.