if(3perl)

✨ if(3perl) β€” the "use this module, but ONLY if" bestie ✨

okay so picture this: it's the if page from the Perl Programmers Reference Guide, and honestly? she's a baddie with one very specific job πŸ’…


πŸ’– NAME πŸ’–

if β€” the pragma that lets you "use" a Perl module if a condition holds. conditional module loading, but make it fashion ✨


🌸 SYNOPSIS 🌸

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

that's it. that's the whole vibe. πŸŽ€


πŸ¦‹ DESCRIPTION πŸ¦‹

πŸ’œ "use if" πŸ’œ

so the if module is your little matchmaker: it conditionally loads another module for you. the construct is:

use if CONDITION, "MODULE", ARGUMENTS;

and what it does is: it loads MODULE only if CONDITION evaluates to true. if CONDITION is false? it does absolutely nothing, no notes, no drama, zero effect. πŸ™ˆ

quick heads up bestie: the module name β€” assuming it contains at least one :: β€” must be quoted when use strict "subs"; is in effect. fr fr, don't forget the quotes.

and if the CONDITION does evaluate to true, then that line has exactly the same effect as:

use MODULE ARGUMENTS;

like, identical. no cap. πŸ‘‘

for example, the Unicode::UCD module's charinfo function will use two functions from Unicode::Normalize only if a certain condition is met β€” look how cute:

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

now, suppose you wanted ARGUMENTS to be an empty list β€” i.e. you want the effect of:

use MODULE ();

yeah... you can't do that with the if pragma, sorry babe 😭 BUT you can achieve exactly this effect, at compile time, with:

BEGIN { require MODULE if CONDITION }

slay. problem solved. πŸ’…


πŸŽ€ "no if" πŸŽ€

the no if construct is mainly used to deactivate categories of warnings when those categories would produce superfluous output under specified versions of perl. it's giving "turn off the noise for these perl versions specifically" ✨

for example, the redundant category of warnings was introduced in Perl-5.22. this warning flags certain instances of superfluous arguments to printf and sprintf. but if your code was running warnings-free on earlier versions of perl and you don't care about redundant warnings in more recent versions, you can call:

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

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

see how that works? iconic. 🌈

but okay real talk: the no if construct assumes that a module or pragma has correctly implemented an unimport() method β€” and most modules and pragmata have not. πŸ™ƒ that's exactly why no if is of limited applicability. she's niche. she knows. πŸ’€

πŸ’€ BUGS πŸ’€

the current implementation does not allow specification of the required version of the module. that's just... not a thing she does. noted. πŸ“


πŸ”₯ SEE ALSO πŸ”₯

  • Module::Requires β€” you can use this to conditionally load one or more modules, with constraints based on the version of the module. BUT β€” and this is the plot twist β€” unlike if, Module::Requires is not a core module. 😭
  • Module::Load::Conditional β€” provides a number of functions you can use to query what modules are available, and then load one or more of them at runtime. very "let me check first" energy. ✨
  • the provide module from CPAN β€” can be used to select one of several possible modules to load based on the version of Perl that is running. options, options, love that for you. 🫢

πŸ‘‘ AUTHOR πŸ‘‘

Ilya Zakharevich <mailto:ilyaz@cpan.org>. thank you for your service, legend. 🫑


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. periodt. πŸ’…


and that's the whole page bestie!! 🌸 this was if(3perl) from perl v5.44.0, dated 2026-10-17 β€” bookmark her, she's lowkey a lifesaver when you need a module only sometimes πŸŽ€βœ¨πŸ’–
πŸ“¦ where this page comes from
  • arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms