Skip to content

Instantly share code, notes, and snippets.

@arodland
Created December 11, 2013 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arodland/7917217 to your computer and use it in GitHub Desktop.
Save arodland/7917217 to your computer and use it in GitHub Desktop.
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