xref: /openbsd-src/gnu/usr.bin/perl/dist/if/if.pm (revision eac174f2741a08d8deb8aae59a7f778ef9b5d770)
1898184e3Ssthenpackage if;
2*eac174f2Safresh1use strict;
3*eac174f2Safresh1our $VERSION = '0.0610';
4898184e3Ssthen
5898184e3Ssthensub work {
6898184e3Ssthen  my $method = shift() ? 'import' : 'unimport';
7b8851fccSafresh1  unless (@_ >= 2) {
8b8851fccSafresh1    my $type = ($method eq 'import') ? 'use' : 'no';
9b8851fccSafresh1    die "Too few arguments to '$type if' (some code returning an empty list in list context?)"
10b8851fccSafresh1  }
11898184e3Ssthen  return unless shift;		# CONDITION
12898184e3Ssthen
13898184e3Ssthen  my $p = $_[0];		# PACKAGE
14898184e3Ssthen  (my $file = "$p.pm") =~ s!::!/!g;
15898184e3Ssthen  require $file;		# Works even if $_[0] is a keyword (like open)
16898184e3Ssthen  my $m = $p->can($method);
17898184e3Ssthen  goto &$m if $m;
18898184e3Ssthen}
19898184e3Ssthen
20898184e3Ssthensub import   { shift; unshift @_, 1; goto &work }
21898184e3Ssthensub unimport { shift; unshift @_, 0; goto &work }
22898184e3Ssthen
23898184e3Ssthen1;
24898184e3Ssthen__END__
25898184e3Ssthen
26898184e3Ssthen=head1 NAME
27898184e3Ssthen
289f11ffb7Safresh1if - C<use> a Perl module if a condition holds
29898184e3Ssthen
30898184e3Ssthen=head1 SYNOPSIS
31898184e3Ssthen
329f11ffb7Safresh1    use if CONDITION, "MODULE", ARGUMENTS;
339f11ffb7Safresh1    no  if CONDITION, "MODULE", ARGUMENTS;
34898184e3Ssthen
35898184e3Ssthen=head1 DESCRIPTION
36898184e3Ssthen
379f11ffb7Safresh1=head2 C<use if>
38898184e3Ssthen
399f11ffb7Safresh1The C<if> module is used to conditionally load another module.  The construct:
40898184e3Ssthen
419f11ffb7Safresh1    use if CONDITION, "MODULE", ARGUMENTS;
429f11ffb7Safresh1
439f11ffb7Safresh1... will load C<MODULE> only if C<CONDITION> evaluates to true; it has no
449f11ffb7Safresh1effect if C<CONDITION> evaluates to false.  (The module name, assuming it
459f11ffb7Safresh1contains at least one C<::>, must be quoted when C<'use strict "subs";'> is in
469f11ffb7Safresh1effect.)  If the CONDITION does evaluate to true, then the above line has the
479f11ffb7Safresh1same effect as:
48898184e3Ssthen
49898184e3Ssthen    use MODULE ARGUMENTS;
50898184e3Ssthen
519f11ffb7Safresh1For 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:
526fb12b70Safresh1
539f11ffb7Safresh1    use if defined &DynaLoader::boot_DynaLoader,
549f11ffb7Safresh1        "Unicode::Normalize" => qw(getCombinClass NFD);
556fb12b70Safresh1
569f11ffb7Safresh1Suppose you wanted C<ARGUMENTS> to be an empty list, I<i.e.>, to have the
579f11ffb7Safresh1effect of:
586fb12b70Safresh1
599f11ffb7Safresh1    use MODULE ();
606fb12b70Safresh1
619f11ffb7Safresh1You can't do this with the C<if> pragma; however, you can achieve
629f11ffb7Safresh1exactly this effect, at compile time, with:
636fb12b70Safresh1
649f11ffb7Safresh1    BEGIN { require MODULE if CONDITION }
656fb12b70Safresh1
669f11ffb7Safresh1=head2 C<no if>
676fb12b70Safresh1
689f11ffb7Safresh1The C<no if> construct is mainly used to deactivate categories of warnings
699f11ffb7Safresh1when those categories would produce superfluous output under specified
709f11ffb7Safresh1versions of F<perl>.
71898184e3Ssthen
729f11ffb7Safresh1For example, the C<redundant> category of warnings was introduced in
739f11ffb7Safresh1Perl-5.22.  This warning flags certain instances of superfluous arguments to
749f11ffb7Safresh1C<printf> and C<sprintf>.  But if your code was running warnings-free on
759f11ffb7Safresh1earlier versions of F<perl> and you don't care about C<redundant> warnings in
769f11ffb7Safresh1more recent versions, you can call:
77b8851fccSafresh1
789f11ffb7Safresh1    use warnings;
799f11ffb7Safresh1    no if $] >= 5.022, q|warnings|, qw(redundant);
80b8851fccSafresh1
819f11ffb7Safresh1    my $test    = { fmt  => "%s", args => [ qw( x y ) ] };
829f11ffb7Safresh1    my $result  = sprintf $test->{fmt}, @{$test->{args}};
839f11ffb7Safresh1
849f11ffb7Safresh1The C<no if> construct assumes that a module or pragma has correctly
859f11ffb7Safresh1implemented an C<unimport()> method -- but most modules and pragmata have not.
869f11ffb7Safresh1That explains why the C<no if> construct is of limited applicability.
87b8851fccSafresh1
88898184e3Ssthen=head1 BUGS
89898184e3Ssthen
909f11ffb7Safresh1The current implementation does not allow specification of the required
919f11ffb7Safresh1version of the module.
92898184e3Ssthen
936fb12b70Safresh1=head1 SEE ALSO
946fb12b70Safresh1
95*eac174f2Safresh1L<Module::Requires> can be used to conditionally load one or more modules,
966fb12b70Safresh1with constraints based on the version of the module.
976fb12b70Safresh1Unlike C<if> though, L<Module::Requires> is not a core module.
986fb12b70Safresh1
996fb12b70Safresh1L<Module::Load::Conditional> provides a number of functions you can use to
1006fb12b70Safresh1query what modules are available, and then load one or more of them at runtime.
1016fb12b70Safresh1
1029f11ffb7Safresh1The L<provide> module from CPAN can be used to select one of several possible
1039f11ffb7Safresh1modules to load based on the version of Perl that is running.
1046fb12b70Safresh1
105898184e3Ssthen=head1 AUTHOR
106898184e3Ssthen
107898184e3SsthenIlya Zakharevich L<mailto:ilyaz@cpan.org>.
108898184e3Ssthen
109b8851fccSafresh1=head1 COPYRIGHT AND LICENCE
110898184e3Ssthen
111b8851fccSafresh1This software is copyright (c) 2002 by Ilya Zakharevich.
112b8851fccSafresh1
113b8851fccSafresh1This is free software; you can redistribute it and/or modify it under
114b8851fccSafresh1the same terms as the Perl 5 programming language system itself.
115b8851fccSafresh1
116b8851fccSafresh1=cut
117