Perl Scripts ·

Perl::Critic: Why Not Declare Variables Conditionally

By Manu Mathew, Technical Lead Engineer · Published 28 January 2016 · Last updated 5 October 2026

Isometric illustration of an empty box set out first, with a value placed in it only when a blue switch is on

Perl::Critic’s ProhibitConditionalDeclarations policy flags a line that looks harmless. Here is why you shouldn’t declare variables conditionally, and what to write instead.

Quick answer

Code like my $rate = $discount if $member; declares a variable with a statement modifier. Perl documents the result as undefined behaviour, so Perl::Critic flags it. Declare the variable first, then assign it conditionally. Or use the ternary operator: my $rate = $member ? $discount : 0;.

Why is it a problem?

perlsyn says the behaviour of a my, state or our modified with a statement modifier conditional or loop construct is undefined. In practice, when the condition is false, the variable may keep an old value from a previous call instead of starting as undef. It is a hard bug to spot.

Since Perl 5.30, the most common trick based on this, my $x if 0;, is a fatal error:

This use of my() in false conditional is no longer allowed

What should I write instead?

use strict;
use warnings;

my ($use_discount, $discount_rate) = (1, 0.1);

# Not ok: my $rate = $discount_rate if $use_discount;

# Ok: declare first, then assign conditionally
my $rate;
$rate = $discount_rate if $use_discount;

# Also ok: one declaration with an explicit fallback
my $rate2 = $use_discount ? $discount_rate : 0;

If you need a value that persists between calls, use state (with use feature 'state') on its own line, rather than relying on this side effect.

Frequently asked questions

Does Perl::Critic run my code?

No. Perl::Critic reads the source and checks it against a set of policies without running it, so it is safe to use on code that talks to live systems. Run it with the perlcritic command, starting at the gentlest severity level, 5.

How do I silence one Perl::Critic warning I disagree with?

Add ## no critic (Variables::ProhibitConditionalDeclarations) at the end of the line, with a short comment saying why. For a rule your team never wants, turn it off in a .perlcriticrc file instead, so the decision lives in one place.

What is the difference between my and state?

my creates a fresh variable each time the code runs. state, enabled with use feature 'state' or use v5.10 and later, creates it once and keeps its value between calls. It is the clear way to get what the conditional-declaration trick was once used for.

Does the rule apply to our variables too?

Yes. perlsyn treats my, state and our the same way here: declaring any of them with a statement modifier such as if or for gives undefined behaviour. Declare the variable on its own line, then assign it conditionally.

Related notes: checking whether a Perl variable is a number; Perl hashes; breaking out of loops in Perl; hiring a remote Perl developer.

Need help with Perl code?

If you have Perl code that needs fixing, extending or upgrading, I’m happy to take a look. I work remotely from Kozhikode, Kerala, with teams across India and elsewhere; Perl development and maintenance explains how I help. If you are deciding how to bring someone in, Hiring a remote Perl developer is a short checklist.

You can reach me through the contact section or email me at manu@manu.co.in. A few lines about the system and what you need is a good start.

An earlier version of this note reproduced the Perl::Critic policy documentation. This version is rewritten in my own words, with new examples.

Sources: Perl::Critic::Policy::Variables::ProhibitConditionalDeclarations; perlsyn: Statement Modifiers.

Back to the blog