Devel::Hide - Forces the unavailability of specified Perl modules (for testing)


Devel-Hide documentation Contained in the Devel-Hide distribution.

Index


Code Index:

NAME

Top

Devel::Hide - Forces the unavailability of specified Perl modules (for testing)

SYNOPSIS

Top

    use Devel::Hide qw(Module/ToHide.pm);
    require Module::ToHide; # fails 

    use Devel::Hide qw(Test::Pod Test::Pod::Coverage);
    require Test::More; # ok
    use Test::Pod 1.18; # fails

Other common usage patterns:

    $ perl -MDevel::Hide=Module::ToHide Makefile.PL

    bash$ PERL5OPT=MDevel::Hide
    bash$ DEVEL_HIDE_PM='Module::Which Test::Pod'
    bash$ export PERL5OPT DEVEL_HIDE_PM
    bash$ perl Makefile.PL

outputs (like blib)

    Devel::Hide hides Module::Which, Test::Pod, etc.




DESCRIPTION

Top

Given a list of Perl modules/filenames, this module makes require and use statements fail (no matter the specified files/modules are installed or not).

They die with a message like:

    Can't locate Module/ToHide.pm (hidden)

The original intent of this module is to allow Perl developers to test for alternative behavior when some modules are not available. In a Perl installation, where many modules are already installed, there is a chance to screw things up because you take for granted things that may not be there in other machines.

For example, to test if your distribution does the right thing when a module is missing, you can do

    perl -MDevel::Hide=Test::Pod Makefile.PL

forcing Test::Pod to not be found (whether it is installed or not).

Another use case is to force a module which can choose between two requisites to use the one which is not the default. For example, XML::Simple needs a parser module and may use XML::Parser or XML::SAX (preferring the latter). If you have both of them installed, it will always try XML::SAX. But you can say:

    perl -MDevel::Hide=XML::SAX script_which_uses_xml_simple.pl

NOTE. This module does not use Carp. As said before, denial dies.

This module is pretty trivial. It uses a code reference in @INC to get rid of specific modules during require - denying they can be successfully loaded and stopping the search before they have a chance to be found.

There are three alternative ways to include modules in the hidden list:

*

setting @Devel::Hide::HIDDEN

*

environment variable DEVEL_HIDE_PM

*

import()

Optionally, you can propagate the list of hidden modules to your process' child processes, by passing '-from:children' as the first option when you use() this module. This works by populating PERL5OPT, and is incompatible with Taint mode, as explained in perlrun.

CAVEATS

There is some interaction between lib and this module

    use Devel::Hide qw(Module/ToHide.pm);
    use lib qw(my_lib);

In this case, 'my_lib' enters the include path before the Devel::Hide hook and if Module/ToHide.pm is found in 'my_lib', it succeeds.

Also for modules that were loaded before Devel::Hide, require and use succeeds.

Since 0.0005, Devel::Hide warns about modules already loaded.

    $ perl -MDevel::Hide=Devel::Hide -e ''
    Devel::Hide: Too late to hide Devel/Hide.pm




EXPORTS

Nothing is exported.

ENVIRONMENT VARIABLES

Top

DEVEL_HIDE_PM - if defined, the list of modules is added to the list of hidden modules

DEVEL_HIDE_VERBOSE - on by default. If off, supresses the initial message which shows the list of hidden modules in effect

PERL5OPT - used if you specify '-from:children'

SEE ALSO

Top

perldoc -f require

Test::Without::Module

BUGS

Top

Please report bugs via CPAN RT http://rt.cpan.org/NoAuth/Bugs.html?Dist=Devel-Hide.

AUTHORS

Top

Adriano R. Ferreira, <ferreira@cpan.org>

with contributions from David Cantrell <dcantrell@cpan.org>

COPYRIGHT AND LICENSE

Top


Devel-Hide documentation Contained in the Devel-Hide distribution.
package Devel::Hide;

use 5.006001;
use strict;
use warnings;

our $VERSION = '0.0008';

# blech! package variables
use vars qw( @HIDDEN $VERBOSE );

# a map ( $hidden_file => 1 ) to speed determining if a module/file is hidden
my %IS_HIDDEN;

# whether to hide modules from ...
my %HIDE_FROM = ( 
        children => 0, # child processes or not
);

sub _to_filename {
    my $pm = shift;
    $pm =~ s|::|/|g;
    $pm .= '.pm';
    return $pm;
}

sub _as_filenames {
    return map { /^(\w+::)*\w+$/ ? _to_filename($_) : $_ } @_;
}

BEGIN {

    unless ( defined $VERBOSE ) { # unless user-defined elsewhere, set default
        $VERBOSE
            = defined $ENV{DEVEL_HIDE_VERBOSE} ? $ENV{DEVEL_HIDE_VERBOSE} : 1;
    }

}

# Pushes a list to the set of hidden modules/filenames
# warns about the modules which could not be hidden
# and about the ones that were successfully hidden (if $VERBOSE)
#
# It works as a batch producing warning messages
# at each invocation (when appropriate).
#
sub _push_hidden {

    return unless @_;

    my @too_late;
    for ( _as_filenames(@_) ) {
        if ( $INC{$_} ) {
            push @too_late, $_;
        }
        else {
            $IS_HIDDEN{$_}++;
        }
    }
    if ( $VERBOSE && @too_late ) {
        warn __PACKAGE__, ': Too late to hide ', join( ', ', @too_late ), "\n";
    }
    if ( $VERBOSE && keys %IS_HIDDEN ) {
        warn __PACKAGE__, ' hides ', join( ', ', sort keys %IS_HIDDEN ), "\n";
    }
}

# $ENV{DEVEL_HIDE_PM} is split in ' '
# as well as @HIDDEN it accepts Module::Module as well as File/Names.pm

BEGIN {

    # unless @HIDDEN was user-defined elsewhere, set default
    if ( !defined @HIDDEN && $ENV{DEVEL_HIDE_PM} ) {
        _push_hidden( split q{ }, $ENV{DEVEL_HIDE_PM} );

        # NOTE. "split ' ', $s" is special. Read "perldoc -f split".
    }
    else {
        _push_hidden(@HIDDEN);
    }

    # NOTE. @HIDDEN is not changed anymore

}

# works for perl 5.8.0, uses in-core files
sub _scalar_as_io8 {
    open my $io, '<', \$_[0]
        or die $!;    # this should not happen (perl 5.8 should support this)
    return $io;
}

# works for perl >= 5.6.1, uses File::Temp
sub _scalar_as_io6 {
    my $scalar = shift;
    require File::Temp;
    my $io = File::Temp::tempfile();
    print {$io} $scalar;
    seek $io, 0, 0;    # rewind the handle
    return $io;
}

BEGIN {

    *_scalar_as_io = ( $] >= 5.008 ) ? \&_scalar_as_io8 : \&_scalar_as_io6;

    # _scalar_as_io is one of the two sub's above

}

sub _dont_load {
    my $filename = shift;
    my $oops;
    my $hidden_by = $VERBOSE ? 'hidden' : 'hidden by ' . __PACKAGE__;
    $oops = qq{die "Can't locate $filename ($hidden_by)\n"};
    return _scalar_as_io($oops);
}

sub _is_hidden {
    my $filename = shift;
    return $IS_HIDDEN{$filename};
}

sub _inc_hook {
    my ( $coderef, $filename ) = @_;
    if ( _is_hidden($filename) ) {
        return _dont_load($filename);    # stop right here, with error
    }
    else {
        return undef;                    # go on with the search
    }
}

use lib ( \&_inc_hook );

sub _core_modules {
    require Module::CoreList;    # XXX require 2.05 or newer
    return Module::CoreList->find_modules( qr/.*/, shift );
}

# _append_to_perl5opt(@to_be_hidden)
sub _append_to_perl5opt {

    $ENV{PERL5OPT} = join( ' ',
        defined($ENV{PERL5OPT}) ? $ENV{PERL5OPT} : (),
        'MDevel::Hide=' . join(',', @_)
    );

}

sub import {
    shift;
    if( @_ && $_[0] eq '-from:children' ) {
        $HIDE_FROM{children} = 1;
        shift;
    }
    if (@_) {
        _push_hidden(@_);
        if ($HIDE_FROM{children}) {
            _append_to_perl5opt(@_);
        }
    }

}

# TO DO:
# * write unimport() sub
# * write decent docs
# * refactor private function names
# * RT #25528

1;

__END__