This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
'if' module: clarify documentation and test more thoroughly.
[perl5.git] / dist / if / if.pm
CommitLineData
cd16c24c
IZ
1package if;
2
1654584e 3$VERSION = '0.0608';
cd16c24c
IZ
4
5sub work {
6 my $method = shift() ? 'import' : 'unimport';
14735530
KW
7 unless (@_ >= 2) {
8 my $type = ($method eq 'import') ? 'use' : 'no';
9 die "Too few arguments to '$type if' (some code returning an empty list in list context?)"
10 }
cd16c24c 11 return unless shift; # CONDITION
a3e5cfd4
S
12
13 my $p = $_[0]; # PACKAGE
b9761643 14 (my $file = "$p.pm") =~ s!::!/!g;
2f761939 15 require $file; # Works even if $_[0] is a keyword (like open)
a3e5cfd4
S
16 my $m = $p->can($method);
17 goto &$m if $m;
cd16c24c
IZ
18}
19
20sub import { shift; unshift @_, 1; goto &work }
21sub unimport { shift; unshift @_, 0; goto &work }
22
231;
24__END__
25
26=head1 NAME
27
1654584e 28if - C<use> a Perl module if a condition holds
cd16c24c
IZ
29
30=head1 SYNOPSIS
31
1654584e
JK
32 use if CONDITION, "MODULE", ARGUMENTS;
33 no if CONDITION, "MODULE", ARGUMENTS;
cd16c24c
IZ
34
35=head1 DESCRIPTION
36
1654584e 37=head2 C<use if>
cd16c24c 38
1654584e 39The C<if> module is used to conditionally load another module. The construct:
cd16c24c 40
1654584e 41 use if CONDITION, "MODULE", ARGUMENTS;
cd16c24c 42
1654584e
JK
43... will load C<MODULE> only if C<CONDITION> evaluates to true; it has no
44effect if C<CONDITION> evaluates to false. (The module name, assuming it
45contains at least one C<::>, must be quoted when C<'use strict "subs";'> is in
46effect.) If the CONDITION does evaluate to true, then the above line has the
47same effect as:
cd16c24c 48
1654584e 49 use MODULE ARGUMENTS;
88da3e79 50
1654584e
JK
51For example, the F<Unicode::UCD> module's F<charinfo> function will use two functions from F<Unicode::Normalize> only if a certain condition is met:
52
53 use if defined &DynaLoader::boot_DynaLoader,
54 "Unicode::Normalize" => qw(getCombinClass NFD);
55
56Suppose you wanted C<ARGUMENTS> to be an empty list, I<i.e.>, to have the
57effect of:
16279618
KC
58
59 use MODULE ();
60
1654584e 61You can't do this with the C<if> pragma; however, you can achieve
16279618
KC
62exactly this effect, at compile time, with:
63
64 BEGIN { require MODULE if CONDITION }
65
1654584e 66=head2 C<no if>
88da3e79 67
1654584e
JK
68The C<no if> construct is mainly used to deactivate categories of warnings
69when those categories would produce superfluous output under specified
70versions of F<perl>.
88da3e79 71
1654584e
JK
72For example, the C<redundant> category of warnings was introduced in
73Perl-5.22. This warning flags certain instances of superfluous arguments to
74C<printf> and C<sprintf>. But if your code was running warnings-free on
75earlier versions of F<perl> and you don't care about C<redundant> warnings in
76more recent versions, you can call:
fae40608 77
1654584e
JK
78 use warnings;
79 no if $] >= 5.022, q|warnings|, qw(redundant);
d1d50e3e 80
1654584e
JK
81 my $test = { fmt => "%s", args => [ qw( x y ) ] };
82 my $result = sprintf $test->{fmt}, @{$test->{args}};
d1d50e3e 83
1654584e
JK
84The C<no if> construct assumes that a module or pragma has correctly
85implemented an C<unimport()> method -- but most modules and pragmata have not.
86That explains why the C<no if> construct is of limited applicability.
d1d50e3e 87
cd16c24c
IZ
88=head1 BUGS
89
1654584e
JK
90The current implementation does not allow specification of the required
91version of the module.
cd16c24c 92
88da3e79
NB
93=head1 SEE ALSO
94
95L<Module::Requires> can be used to conditionally load one or modules,
96with constraints based on the version of the module.
97Unlike C<if> though, L<Module::Requires> is not a core module.
98
99L<Module::Load::Conditional> provides a number of functions you can use to
100query what modules are available, and then load one or more of them at runtime.
101
1654584e
JK
102The L<provide> module from CPAN can be used to select one of several possible
103modules to load based on the version of Perl that is running.
88da3e79 104
cd16c24c
IZ
105=head1 AUTHOR
106
81495e8f 107Ilya Zakharevich L<mailto:ilyaz@cpan.org>.
cd16c24c 108
4b89cb47
KE
109=head1 COPYRIGHT AND LICENCE
110
111This software is copyright (c) 2002 by Ilya Zakharevich.
112
113This is free software; you can redistribute it and/or modify it under
114the same terms as the Perl 5 programming language system itself.
115
cd16c24c 116=cut