1if(3pm)                Perl Programmers Reference Guide                if(3pm)
2
3
4

NAME

6       if - "use" a Perl module if a condition holds
7

SYNOPSIS

9           use if CONDITION, "MODULE", ARGUMENTS;
10           no  if CONDITION, "MODULE", ARGUMENTS;
11

DESCRIPTION

13   "use if"
14       The "if" module is used to conditionally load another module.  The
15       construct:
16
17           use if CONDITION, "MODULE", ARGUMENTS;
18
19       ... will load "MODULE" only if "CONDITION" evaluates to true; it has no
20       effect if "CONDITION" evaluates to false.  (The module name, assuming
21       it contains at least one "::", must be quoted when 'use strict "subs";'
22       is in effect.)  If the CONDITION does evaluate to true, then the above
23       line has the same effect as:
24
25           use MODULE ARGUMENTS;
26
27       For example, the Unicode::UCD module's charinfo function will use two
28       functions from Unicode::Normalize only if a certain condition is met:
29
30           use if defined &DynaLoader::boot_DynaLoader,
31               "Unicode::Normalize" => qw(getCombinClass NFD);
32
33       Suppose you wanted "ARGUMENTS" to be an empty list, i.e., to have the
34       effect of:
35
36           use MODULE ();
37
38       You can't do this with the "if" pragma; however, you can achieve
39       exactly this effect, at compile time, with:
40
41           BEGIN { require MODULE if CONDITION }
42
43   "no if"
44       The "no if" construct is mainly used to deactivate categories of
45       warnings when those categories would produce superfluous output under
46       specified versions of perl.
47
48       For example, the "redundant" category of warnings was introduced in
49       Perl-5.22.  This warning flags certain instances of superfluous
50       arguments to "printf" and "sprintf".  But if your code was running
51       warnings-free on earlier versions of perl and you don't care about
52       "redundant" warnings in more recent versions, you can call:
53
54           use warnings;
55           no if $] >= 5.022, q|warnings|, qw(redundant);
56
57           my $test    = { fmt  => "%s", args => [ qw( x y ) ] };
58           my $result  = sprintf $test->{fmt}, @{$test->{args}};
59
60       The "no if" construct assumes that a module or pragma has correctly
61       implemented an "unimport()" method -- but most modules and pragmata
62       have not.  That explains why the "no if" construct is of limited
63       applicability.
64

BUGS

66       The current implementation does not allow specification of the required
67       version of the module.
68

SEE ALSO

70       Module::Requires can be used to conditionally load one or modules, with
71       constraints based on the version of the module.  Unlike "if" though,
72       Module::Requires is not a core module.
73
74       Module::Load::Conditional provides a number of functions you can use to
75       query what modules are available, and then load one or more of them at
76       runtime.
77
78       The provide module from CPAN can be used to select one of several
79       possible modules to load based on the version of Perl that is running.
80

AUTHOR

82       Ilya Zakharevich <mailto:ilyaz@cpan.org>.
83
85       This software is copyright (c) 2002 by Ilya Zakharevich.
86
87       This is free software; you can redistribute it and/or modify it under
88       the same terms as the Perl 5 programming language system itself.
89
90
91
92perl v5.30.2                      2020-03-27                           if(3pm)
Impressum