Commit | Line | Data |
---|---|---|
cd16c24c IZ |
1 | package if; |
2 | ||
be5bca00 | 3 | $VERSION = '0.0603'; |
cd16c24c IZ |
4 | |
5 | sub work { | |
6 | my $method = shift() ? 'import' : 'unimport'; | |
6dfee1ec | 7 | die "Too few arguments to 'use if' (some code returning an empty list in list context?)" |
fae40608 | 8 | unless @_ >= 2; |
cd16c24c | 9 | return unless shift; # CONDITION |
a3e5cfd4 S |
10 | |
11 | my $p = $_[0]; # PACKAGE | |
b9761643 | 12 | (my $file = "$p.pm") =~ s!::!/!g; |
2f761939 | 13 | require $file; # Works even if $_[0] is a keyword (like open) |
a3e5cfd4 S |
14 | my $m = $p->can($method); |
15 | goto &$m if $m; | |
cd16c24c IZ |
16 | } |
17 | ||
18 | sub import { shift; unshift @_, 1; goto &work } | |
19 | sub unimport { shift; unshift @_, 0; goto &work } | |
20 | ||
21 | 1; | |
22 | __END__ | |
23 | ||
24 | =head1 NAME | |
25 | ||
26 | if - C<use> a Perl module if a condition holds | |
27 | ||
28 | =head1 SYNOPSIS | |
29 | ||
30 | use if CONDITION, MODULE => ARGUMENTS; | |
31 | ||
32 | =head1 DESCRIPTION | |
33 | ||
88da3e79 | 34 | The C<if> module is used to conditionally load another module. |
cd16c24c IZ |
35 | The construct |
36 | ||
37 | use if CONDITION, MODULE => ARGUMENTS; | |
38 | ||
88da3e79 NB |
39 | will load MODULE only if CONDITION evaluates to true. |
40 | The above statement has no effect unless C<CONDITION> is true. | |
41 | If the CONDITION does evaluate to true, then the above line has | |
42 | the same effect as: | |
cd16c24c IZ |
43 | |
44 | use MODULE ARGUMENTS; | |
45 | ||
88da3e79 NB |
46 | The use of C<< => >> above provides necessary quoting of C<MODULE>. |
47 | If you don't use the fat comma (eg you don't have any ARGUMENTS), | |
48 | then you'll need to quote the MODULE. | |
49 | ||
50 | =head2 EXAMPLES | |
51 | ||
52 | The following line is taken from the testsuite for L<File::Map>: | |
53 | ||
54 | use if $^O ne 'MSWin32', POSIX => qw/setlocale LC_ALL/; | |
55 | ||
56 | If run on any operating system other than Windows, | |
57 | this will import the functions C<setlocale> and C<LC_ALL> from L<POSIX>. | |
58 | On Windows it does nothing. | |
59 | ||
60 | The following is used to L<deprecate> core modules beyond a certain version of Perl: | |
61 | ||
62 | use if $] > 5.016, 'deprecate'; | |
63 | ||
64 | This line is taken from L<Text::Soundex> 3.04, | |
65 | and marks it as deprecated beyond Perl 5.16. | |
66 | If you C<use Text::Soundex> in Perl 5.18, for example, | |
67 | and you have used L<warnings>, | |
68 | then you'll get a warning message | |
69 | (the deprecate module looks to see whether the | |
70 | calling module was C<use>'d from a core library directory, | |
71 | and if so, generates a warning), | |
72 | unless you've installed a more recent version of L<Text::Soundex> from CPAN. | |
fae40608 | 73 | |
cd16c24c IZ |
74 | =head1 BUGS |
75 | ||
76 | The current implementation does not allow specification of the | |
77 | required version of the module. | |
78 | ||
88da3e79 NB |
79 | =head1 SEE ALSO |
80 | ||
81 | L<Module::Requires> can be used to conditionally load one or modules, | |
82 | with constraints based on the version of the module. | |
83 | Unlike C<if> though, L<Module::Requires> is not a core module. | |
84 | ||
85 | L<Module::Load::Conditional> provides a number of functions you can use to | |
86 | query what modules are available, and then load one or more of them at runtime. | |
87 | ||
88 | L<provide> can be used to select one of several possible modules to load, | |
89 | based on what version of Perl is running. | |
90 | ||
cd16c24c IZ |
91 | =head1 AUTHOR |
92 | ||
81495e8f | 93 | Ilya Zakharevich L<mailto:ilyaz@cpan.org>. |
cd16c24c IZ |
94 | |
95 | =cut | |
96 |