Created
December 11, 2013 19:51
-
-
Save arodland/7917217 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package if::loaded; | |
sub work { | |
my $method = shift() ? 'import' : 'unimport'; | |
my $module = shift; | |
my $option = shift; | |
my $alternative = shift; | |
unless ($module && $alternative && $option eq '-else') { | |
die "Usage: use if::loaded 'Some::Module', -else => 'Alternative::Module' [, \@args ]" | |
} | |
(my $file = "$module.pm") =~ s[::][/]g; | |
unless ($INC{$file}) { | |
$module = $alternative; | |
($file = "$module.pm") =~ s[::][/]g; | |
require $file; | |
} | |
my $m = $module->can($method); | |
if ($m) { | |
unshift @_, $module; | |
goto &$m; | |
} | |
} | |
sub import { shift; unshift @_, 1; goto &work } | |
sub unimport { shift; unshift @_, 0; goto &work } | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment