Scroll to navigation

Module::Mask(3pm) User Contributed Perl Documentation Module::Mask(3pm)


Module::Mask - Pretend certain modules are not installed


    use Module::Mask;
        my $mask = new Module::Mask ('My::Module');
        eval { require My::Module };
        if ($@) {
            # ... should be called
        else {
            warn "require succeeded unexpectedly"
    # The mask is out of scope, this should now work.
    eval { require My::Module };
    # There's also an inverted version:
        my $mask = new Module::Mask::Inverted qw( Foo Bar );
        # Now only Foo and Bar can be loaded by require:
        eval {require Baz};


Sometimes you need to test what happens when a given module is not installed. This module provides a way of temporarily hiding installed modules from perl's require mechanism. The Module::Mask object adds itself to @INC and blocks require calls to restricted modules.

Module::Mask will not affect modules already loaded at time of instantiation.



    use Module::Mask @modules;

Globally masks modules. This can be used to block optional modules for testing purposes.

    perl -MModule::Mask=MyOptionalModule


    $obj = $class->new(@modules);

Returns a new instance of this class. Any arguments are passed to mask_modules.


    $obj = $obj->mask_modules(@modules)

Add the given modules to the mask. Arguments can be paths or module names, module names will be stored internally as relative paths. So there's no difference between the following statements:



    $obj = $obj->clear_mask()

Stops the object from masking modules by removing it from @INC. This is automatically called when object goes out of scope.


    $obj = $obj->set_mask()

Makes the object start masking modules by adding it to @INC. This is called by new().

This also has the effect of moving the object to the front of @INC again, which could prove useful if @INC has been manipulated since the object was first instantiated.

Calling this method on an object already in @INC won't cause multiple copies to appear.


    $bool = $obj->is_masked($module)

Returns true if the mask object is currently masking the given module, false otherwise.

Module::Mask::Inverted objects have the opposite behaviour.


    @modules = $obj->list_masked()

Returns a list of modules that are being masked. These are in the form of relative file paths, as found in %INC.


Implements the hook in @INC. See perldoc -f require


    $message = $obj->message($filename)

Returns the error message to be used when the filename is not found. This is normally "$filename masked by $class", but can be overridden in subclasses if necessary. Carp's shortmess is used to make this message appear to come from the caller, i.e. the "require" or "use" statement attempting to load the file.

One possible application of this would be to make the error message look more like perl's native "Could not find $filename in \@INC ...".


Because loaded modules cannot be masked, the following module are effectively never able to be masked as they are used my Module::Mask.

  • Module::Util
  • Scalar::Util
  • Carp

Plus some other core modules and pragmata used by these.


    perl -MModule::Mask -le 'print for keys %INC'

To see a definitive list.


perldoc -f require



Matt Lawrence <>

2021-10-20 perl v5.38.2