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