1package if; 2 3$VERSION = '0.0608'; 4 5sub work { 6 my $method = shift() ? 'import' : 'unimport'; 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 } 11 return unless shift; # CONDITION 12 13 my $p = $_[0]; # PACKAGE 14 (my $file = "$p.pm") =~ s!::!/!g; 15 require $file; # Works even if $_[0] is a keyword (like open) 16 my $m = $p->can($method); 17 goto &$m if $m; 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 28if - C<use> a Perl module if a condition holds 29 30=head1 SYNOPSIS 31 32 use if CONDITION, "MODULE", ARGUMENTS; 33 no if CONDITION, "MODULE", ARGUMENTS; 34 35=head1 DESCRIPTION 36 37=head2 C<use if> 38 39The C<if> module is used to conditionally load another module. The construct: 40 41 use if CONDITION, "MODULE", ARGUMENTS; 42 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: 48 49 use MODULE ARGUMENTS; 50 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: 58 59 use MODULE (); 60 61You can't do this with the C<if> pragma; however, you can achieve 62exactly this effect, at compile time, with: 63 64 BEGIN { require MODULE if CONDITION } 65 66=head2 C<no if> 67 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>. 71 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: 77 78 use warnings; 79 no if $] >= 5.022, q|warnings|, qw(redundant); 80 81 my $test = { fmt => "%s", args => [ qw( x y ) ] }; 82 my $result = sprintf $test->{fmt}, @{$test->{args}}; 83 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. 87 88=head1 BUGS 89 90The current implementation does not allow specification of the required 91version of the module. 92 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 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. 104 105=head1 AUTHOR 106 107Ilya Zakharevich L<mailto:ilyaz@cpan.org>. 108 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 116=cut 117