<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package if;
use strict;
our $VERSION = '0.0610';

sub work {
  my $method = shift() ? 'import' : 'unimport';
  unless (@_ &gt;= 2) {
    my $type = ($method eq 'import') ? 'use' : 'no';
    die "Too few arguments to '$type if' (some code returning an empty list in list context?)"
  }
  return unless shift;		# CONDITION

  my $p = $_[0];		# PACKAGE
  (my $file = "$p.pm") =~ s!::!/!g;
  require $file;		# Works even if $_[0] is a keyword (like open)
  my $m = $p-&gt;can($method);
  goto &amp;$m if $m;
}

sub import   { shift; unshift @_, 1; goto &amp;work }
sub unimport { shift; unshift @_, 0; goto &amp;work }

1;
__END__

=head1 NAME

if - C&lt;use&gt; a Perl module if a condition holds

=head1 SYNOPSIS

    use if CONDITION, "MODULE", ARGUMENTS;
    no  if CONDITION, "MODULE", ARGUMENTS;

=head1 DESCRIPTION

=head2 C&lt;use if&gt;

The C&lt;if&gt; module is used to conditionally load another module.  The construct:

    use if CONDITION, "MODULE", ARGUMENTS;

... will load C&lt;MODULE&gt; only if C&lt;CONDITION&gt; evaluates to true; it has no
effect if C&lt;CONDITION&gt; evaluates to false.  (The module name, assuming it
contains at least one C&lt;::&gt;, must be quoted when C&lt;'use strict "subs";'&gt; is in
effect.)  If the CONDITION does evaluate to true, then the above line has the
same effect as:

    use MODULE ARGUMENTS;

For example, the F&lt;Unicode::UCD&gt; module's F&lt;charinfo&gt; function will use two functions from F&lt;Unicode::Normalize&gt; only if a certain condition is met:

    use if defined &amp;DynaLoader::boot_DynaLoader,
        "Unicode::Normalize" =&gt; qw(getCombinClass NFD);

Suppose you wanted C&lt;ARGUMENTS&gt; to be an empty list, I&lt;i.e.&gt;, to have the
effect of:

    use MODULE ();

You can't do this with the C&lt;if&gt; pragma; however, you can achieve
exactly this effect, at compile time, with:

    BEGIN { require MODULE if CONDITION }

=head2 C&lt;no if&gt;

The C&lt;no if&gt; construct is mainly used to deactivate categories of warnings
when those categories would produce superfluous output under specified
versions of F&lt;perl&gt;.

For example, the C&lt;redundant&gt; category of warnings was introduced in
Perl-5.22.  This warning flags certain instances of superfluous arguments to
C&lt;printf&gt; and C&lt;sprintf&gt;.  But if your code was running warnings-free on
earlier versions of F&lt;perl&gt; and you don't care about C&lt;redundant&gt; warnings in
more recent versions, you can call:

    use warnings;
    no if $] &gt;= 5.022, q|warnings|, qw(redundant);

    my $test    = { fmt  =&gt; "%s", args =&gt; [ qw( x y ) ] };
    my $result  = sprintf $test-&gt;{fmt}, @{$test-&gt;{args}};

The C&lt;no if&gt; construct assumes that a module or pragma has correctly
implemented an C&lt;unimport()&gt; method -- but most modules and pragmata have not.
That explains why the C&lt;no if&gt; construct is of limited applicability.

=head1 BUGS

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

=head1 SEE ALSO

L&lt;Module::Requires&gt; can be used to conditionally load one or more modules,
with constraints based on the version of the module.
Unlike C&lt;if&gt; though, L&lt;Module::Requires&gt; is not a core module.

L&lt;Module::Load::Conditional&gt; provides a number of functions you can use to
query what modules are available, and then load one or more of them at runtime.

The L&lt;provide&gt; module from CPAN can be used to select one of several possible
modules to load based on the version of Perl that is running.

=head1 AUTHOR

Ilya Zakharevich L&lt;mailto:ilyaz@cpan.org&gt;.

=head1 COPYRIGHT AND LICENCE

This software is copyright (c) 2002 by Ilya Zakharevich.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
</pre></body></html>