| 1 |
root |
1.1 |
package if; |
| 2 |
|
|
|
| 3 |
|
|
our $VERSION = '0.03'; |
| 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 or die; |
| 12 |
|
|
|
| 13 |
|
|
my $m = $p->can($method); |
| 14 |
|
|
goto &$m if $m; |
| 15 |
|
|
} |
| 16 |
|
|
|
| 17 |
|
|
sub import { shift; unshift @_, 1; goto &work } |
| 18 |
|
|
sub unimport { shift; unshift @_, 0; goto &work } |
| 19 |
|
|
|
| 20 |
|
|
1; |
| 21 |
|
|
__END__ |
| 22 |
|
|
|
| 23 |
|
|
=head1 NAME |
| 24 |
|
|
|
| 25 |
|
|
if - C<use> a Perl module if a condition holds |
| 26 |
|
|
|
| 27 |
|
|
=head1 SYNOPSIS |
| 28 |
|
|
|
| 29 |
|
|
use if CONDITION, MODULE => ARGUMENTS; |
| 30 |
|
|
|
| 31 |
|
|
=head1 DESCRIPTION |
| 32 |
|
|
|
| 33 |
|
|
The construct |
| 34 |
|
|
|
| 35 |
|
|
use if CONDITION, MODULE => ARGUMENTS; |
| 36 |
|
|
|
| 37 |
|
|
has no effect unless C<CONDITION> is true. In this case the effect is |
| 38 |
|
|
the same as of |
| 39 |
|
|
|
| 40 |
|
|
use MODULE ARGUMENTS; |
| 41 |
|
|
|
| 42 |
|
|
=head1 BUGS |
| 43 |
|
|
|
| 44 |
|
|
The current implementation does not allow specification of the |
| 45 |
|
|
required version of the module. |
| 46 |
|
|
|
| 47 |
|
|
=head1 AUTHOR |
| 48 |
|
|
|
| 49 |
|
|
Ilya Zakharevich L<mailto:perl-module-if@ilyaz.org>. |
| 50 |
|
|
|
| 51 |
|
|
=cut |
| 52 |
|
|
|