Perl::...ations(3pm)

πŸ“¦
version 1.0.4 Β· 4 versions
, in debian trixie, ubuntu resolute, ubuntu stonking

Perl::...ations(3pm) πŸ’…

okay bestie, welcome to Perl::...ations(3pm), from the User Contributed Perl Documentation shelf β€” yes the title is literally abbreviated like that, it's giving mysterious, we love a short king ✨


✨ NAME ✨

Perl::Critic::Policy::Community::ConditionalDeclarations β€” don't declare variables conditionally πŸŽ€


🌸 DESCRIPTION 🌸

so here's the tea, bestie. you can slap a postfix condition onto a variable declaration, like my $foo = $bar if $baz. cute, right? WRONG. 😭 it's unclear β€” and literally undefined β€” whether that variable actually gets declared when the condition isn't met. the vibe check fails. it's giving "maybe a variable, maybe a ghost" πŸ’€

instead, do the responsible main character thing: declare the variable first, then assign to it conditionally. or use the ternary operator to assign a value conditionally. slay.

my $foo = $bar if $baz;           # not ok
my ($foo, $bar) = @_ unless $baz; # not ok
our $bar = $_ for 0..10;          # not ok
my $foo; $foo = $bar if $baz;     # ok
my $foo = $baz ? $bar : undef;    # ok

and the lore: this policy is a subclass of the core policy Perl::Critic::Policy::Variables::ProhibitConditionalDeclarations, and it performs the same function but in the "community" theme. same energy, different aesthetic πŸ¦‹


πŸ‘‘ AFFILIATION πŸ‘‘

this policy is part of Perl::Critic::Community. iykyk 🫢


πŸŽ€ CONFIGURATION πŸŽ€

this policy is not configurable except for the standard options. no notes, no knobs to fiddle with, it said what it said πŸ’…


⭐ AUTHOR ⭐

Dan Book, dbook@cpan.org 🌈


Copyright 2015, Dan Book.

this library is free software; you may redistribute it and/or modify it under the terms of the Artistic License version 2.0. fr fr, go wild (within the license, bestie) πŸ“


πŸ¦‹ SEE ALSO πŸ¦‹

  • Perl::Critic ✨

perl v5.40.1 Β· 2025-03-22 β€” that's the version and the date, babe, and that's the whole page πŸ’œ bye bestie, go declare your variables unconditionally like the icon you are πŸ§šβ€β™€οΈπŸŒΈ

πŸ“¦ where this page comes from