Acme::Devel::Hide::Tiny - Hide a perl module for testing, in one statement
version 0.002
# in 'foo.t', assume we want to hide Cpanel::JSON::XS # hide Cpanel::JSON::XS -> Cpanel/JSON/XS.pm use lib map { my $m = $_; sub { return unless $_[1] eq $m; die "Can't locate $_[1] in \@INC (hidden)\n"; } } qw{Cpanel/JSON/XS.pm};
The Devel::Hide and Test::Without::Module modules are very helpful development tools. Unfortunately, using them in your .t files adds a test dependency. Maybe you don't want to do that.
Instead, you can use the one-liner from the SYNOPSIS above, which is an extremely stripped down version of Devel::Hide.
Here is a more verbose, commented version of it:
# 'lib' adds its arguments to the front of @INC use lib # add one coderef per path to hide map { # create lexical for module my $m = $_; # construct and return a closure that dies for the module path to hide sub { # return if not the path to hide; perl checks rest of @INC return unless $_[1] eq $m; # die with the error message we want die "Can't locate $_[1] in \@INC (hidden)\n"; } } # input to map is a list module names, converted to paths; qw{Cpanel/JSON/XS.pm JSON/XS.pm} ; # end of 'use lib' statement
When perl sees a coderef in @INC, it gives the coderef a chance to provide the source code of that module. In this case, if the path is the one we want to hide, it dies with the message we want and perl won't continue looking at @INC to find the real module source. The module is hidden and dies with a message similar to the one that would happen if it weren't installed.
@INC
Please report any bugs or feature requests through the issue tracker at https://github.com/dagolden/Acme-Devel-Hide-Tiny/issues. You will be notified automatically of any progress on your issue.
This is open source software. The code repository is available for public review and contribution under the terms of the license.
https://github.com/dagolden/Acme-Devel-Hide-Tiny
git clone https://github.com/dagolden/Acme-Devel-Hide-Tiny.git
David Golden <dagolden@cpan.org>
Graham Knop <haarg@haarg.org>
This software is Copyright (c) 2016 by David Golden.
This is free software, licensed under:
The Apache License, Version 2.0, January 2004
To install Acme::Devel::Hide::Tiny, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Acme::Devel::Hide::Tiny
CPAN shell
perl -MCPAN -e shell install Acme::Devel::Hide::Tiny
For more information on module installation, please visit the detailed CPAN module installation guide.