/usr/share/perl5/Test/Without/Module.pm is in libtest-without-module-perl 0.17-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | package Test::Without::Module;
use strict;
use Carp qw( croak );
use vars qw( $VERSION );
$VERSION = '0.17';
use vars qw( %forbidden );
sub get_forbidden_list {
\%forbidden
};
sub import {
my ($self,@forbidden_modules) = @_;
my $forbidden = get_forbidden_list;
for (@forbidden_modules) {
$forbidden->{$_} = $INC{ module2file($_) };
};
# Scrub %INC, so that loaded modules disappear
for my $module (@forbidden_modules) {
scrub( $module );
};
unshift @INC, \&fake_module;
};
sub fake_module {
my ($self,$module_file,$member_only) = @_;
# Don't touch $@, or .al files will not load anymore????
my $forbidden = get_forbidden_list;
my $modulename = file2module($module_file);
# Deliver a faked, nonworking module
if (exists $forbidden->{$modulename}) {
my @faked_module = ("package $modulename;","0;");
return sub { defined ( $_ = shift @faked_module ) };
};
};
sub unimport {
my ($self,@list) = @_;
my $module;
my $forbidden = get_forbidden_list;
for $module (@list) {
if (exists $forbidden->{$module}) {
if (defined $forbidden->{$module}) {
$INC{ module2file($module) } = delete $forbidden->{$module};
} else {
delete $forbidden->{$module};
};
} else {
croak "Can't allow non-forbidden module $module";
};
};
};
sub file2module {
my ($mod) = @_;
$mod =~ s!/!::!g;
$mod =~ s!\.pm$!!;
$mod;
};
sub module2file {
my ($mod) = @_;
$mod =~ s!::|'!/!g;
$mod .= ".pm";
$mod;
};
sub scrub {
my ($module) = @_;
for my $key (keys %INC) {
my $fn = file2module($key);
if ($fn eq $module) {
delete $INC{$key};
};
};
};
1;
=head1 NAME
Test::Without::Module - Test fallback behaviour in absence of modules
=head1 SYNOPSIS
=for example begin
use Test::Without::Module qw( My::Module );
# Now, loading of My::Module fails :
eval { require My::Module; };
warn $@ if $@;
# Now it works again
eval q{ no Test::Without::Module qw( My::Module ) };
eval { require My::Module; };
print "Found My::Module" unless $@;
=for example end
=head1 DESCRIPTION
This module allows you to deliberately hide modules from a program
even though they are installed. This is mostly useful for testing modules
that have a fallback when a certain dependency module is not installed.
=head2 EXPORT
None. All magic is done via C<use Test::Without::Module LIST> and
C<no Test::Without::Module LIST>.
=head2 Test::Without::Module::get_forbidden_list
This function returns a reference to a copy of the current hash of forbidden
modules or an empty hash if none are currently forbidden. This is convenient
if you are testing and/or debugging this module.
=cut
=head1 ONE LINER
A neat trick for using this module from the command line
was mentioned to me by NUFFIN and by Jerrad Pierce:
perl -MTest::Without::Module=Some::Module -w -Iblib/lib t/SomeModule.t
That way, you can easily see how your module or test file behaves
when a certain module is unavailable.
=head1 BUGS
=over 4
=item *
There is no lexical scoping
=back
=head1 CREDITS
Much improvement must be thanked to Aristotle from PerlMonks, he pointed me
to a much less convoluted way to fake a module at
L<http://www.perlmonks.org/index.pl?node=192635>.
I also discussed with him an even more elegant way of overriding
CORE::GLOBAL::require, but the parsing of the overridden subroutine
didn't work out the way I wanted it - CORE::require didn't recognize
barewords as such anymore.
NUFFIN and Jerrad Pierce pointed out the convenient
use from the command line to interactively watch the
behaviour of the test suite and module in absence
of a module.
=head1 AUTHOR
Copyright (c) 2003-2009 Max Maischein, E<lt>corion@cpan.orgE<gt>
=head1 LICENSE
This module is released under the same terms as Perl itself.
=head1 SEE ALSO
L<Devel::Hide>, L<Acme::Intraweb>, L<PAR>, L<perlfunc>
=cut
__END__
|