| 1 | package if; |
| 2 | |
| 3 | $VERSION = '0.0401'; |
| 4 | |
| 5 | sub work { |
| 6 | my $method = shift() ? 'import' : 'unimport'; |
| 7 | return unless shift; # CONDITION |
| 8 | |
| 9 | my $p = $_[0]; # PACKAGE |
| 10 | (my $file = "$p.pm") =~ s!::!/!g; |
| 11 | require $file; # Works even if $_[0] is a keyword (like open) |
| 12 | my $m = $p->can($method); |
| 13 | goto &$m if $m; |
| 14 | } |
| 15 | |
| 16 | sub import { shift; unshift @_, 1; goto &work } |
| 17 | sub unimport { shift; unshift @_, 0; goto &work } |
| 18 | |
| 19 | 1; |
| 20 | __END__ |
| 21 | |
| 22 | =head1 NAME |
| 23 | |
| 24 | if - C<use> a Perl module if a condition holds |
| 25 | |
| 26 | =head1 SYNOPSIS |
| 27 | |
| 28 | use if CONDITION, MODULE => ARGUMENTS; |
| 29 | |
| 30 | =head1 DESCRIPTION |
| 31 | |
| 32 | The construct |
| 33 | |
| 34 | use if CONDITION, MODULE => ARGUMENTS; |
| 35 | |
| 36 | has no effect unless C<CONDITION> is true. In this case the effect is |
| 37 | the same as of |
| 38 | |
| 39 | use MODULE ARGUMENTS; |
| 40 | |
| 41 | =head1 BUGS |
| 42 | |
| 43 | The current implementation does not allow specification of the |
| 44 | required version of the module. |
| 45 | |
| 46 | =head1 AUTHOR |
| 47 | |
| 48 | Ilya Zakharevich L<mailto:perl-module-if@ilyaz.org>. |
| 49 | |
| 50 | =cut |
| 51 | |