Perl::...tements(3pm)

πŸ’… NAME πŸ’…

Okay so this is Perl::...tements(3pm), from the User Contributed Perl Documentation, bestie. The policy is Perl::Critic::Policy::Modules::ProhibitConditionalUseStatements, and its whole thing is: avoid putting conditional logic around compile-time includes. Periodt. ✨

🌸 AFFILIATION 🌸

This Policy is part of the core Perl::Critic distribution. Iconic behavior, no cap. πŸ‘‘

πŸ¦‹ DESCRIPTION πŸ¦‹

Modules included via use are loaded at compile-time. Placing conditional logic around the use statement has no effect on whether the module will be loaded. Doing so can also serve to confuse the reader as to the author's original intent.

If you need to conditionally load a module you should be using require instead.

bestie, this is the whole plot twist: use is doing its compile-time thing whether you wrap it in an if or not. The conditional vibe is giving nothing but confusion. 😭

This policy will catch the following forms of conditional use statements:

# if-elsif-else
if ($a == 1) { use Module; }
if ($a == 1) { } elsif ($a == 2) { use Module; }
if ($a == 1) { } else { use Module; }

# for/foreach
for (1..$a) { use Module; }
foreach (@a) { use Module; }

# while
while ($a == 1) { use Module; }

# unless
unless ($a == 1) { use Module; }

# until
until ($a == 1) { use Module; }

# do-condition
do { use Module; } if $a == 1;
do { use Module; } while $a == 1;
do { use Module; } unless $a == 1;
do { use Module; } until $a == 1;

# operator-do
$a == 1 || do { use Module; };
$a == 1 && do { use Module; };
$a == 1 or do { use Module; };
$a == 1 and do { use Module; };

# non-string eval
eval { use Module; };

Including a module via use in bare blocks, standalone do blocks, or string evals is allowed. Slay, those get a pass. 🫢

# bare block
{ use Module; }

# do
do { use Module; }

# string eval
eval "use Module";

πŸŽ€ CONFIGURATION πŸŽ€

This Policy is not configurable except for the standard options. That's it, bestie β€” no secret extra knobs, no hidden settings, just the standard options. πŸ’…

🌈 AUTHOR 🌈

Peter Guzis <pguzis@cpan.org> β€” actual legend. ⭐

Copyright (c) 2010-2011 Peter Guzis. All rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of this license can be found in the LICENSE file included with this module.


Source/version/date check, bestie: perl v5.42.0, 2024-10-23, and yes β€” Perl::...tements(3pm). Byeee, ily πŸ«ΆπŸ’–

πŸ“¦ where this page comes from