Perl::...meLine(3pm)

✨ NAME ✨

Perl::Critic::Policy::CodeLayout::ProhibitIfIfSameLine β€” bestie, please don't put an if right after an if on the same line πŸ’…

(also hi, this is a User Contributed Perl Documentation page, section 3pm, and the title got tragically truncated to Perl::...meLine(3pm) because man pages fear long names, iykyk πŸŽ€)


🌸 DESCRIPTION 🌸

Okay so this policy is part of the Perl::Critic::Pulp add-on. It is begging you β€” respectfully, lovingly β€” not to write an if statement on the same line as an if that came before it. Not the vibe. 🫢

if ($x) {
  ...
} if ($y) {       # bad
  ...
}

if ($x) {
  ...
} elsif ($y) {    # was "elsif" intended ?
  ...
}

The whole idea here? When an if sits in the exact same spot where an elsif would go, a human reader is gonna be like "wait… was that a typo or are you being mysterious 😭". Could be a genuine mistake, could just be confusing β€” either way, not it.

So on that basis, this policy lives under the bugs theme and is medium severity (see "POLICY THEMES" in Perl::Critic for the deets, bestie).


πŸ¦‹ Unless πŸ¦‹

An unless...if gets treated exactly the same way, no cap. Perl genuinely allows unless ... elsif, so the same potential elsif-layout confusion shows up. Same energy, same flag. πŸ’€

unless ($x) {
  ...
} if ($y) {       # bad
  ...
}

unless ($x) {
  ...
} elsif ($y) {    # maybe meant to be "elsif" like this ?
  ...
}

Now, whether unless ... elsif is a good idea at all is a whole other conversation πŸ™ˆ β€” sometimes it just suits a combination of conditions, and that's between you and your code.


πŸŽ€ Statement Modifiers πŸŽ€

Important: this policy only applies to a statement followed by a statement. An if used as a statement modifier? Not affected at all, we leave it alone. And honestly it's totally usual to put that one on the same line as the statement it's modifying β€” that's just how it goes.

do {
  ...
} if ($x);        # ok, statement modifier

⭐ All One Line ⭐

Two if statements written out on the same line? Yeah, that triggers the policy. Sorry, it's the rules. 🧚

if(1){one;}   if(2){two;}      # bad

Perhaps there could be an exception or an option for when both statements are entirely on the one line, or some such, for code that's trying to be all compact and cute. But as of right now? Nope. πŸ’œ


πŸ’– Disabling πŸ’–

As always β€” if you simply do not care about this, you can disable ProhibitIfIfSameLine from your .perlcriticrc (see "CONFIGURATION" in Perl::Critic for how that works):

[-CodeLayout::ProhibitIfIfSameLine]

πŸ‘‘ SEE ALSO πŸ‘‘

  • Perl::Critic::Pulp
  • Perl::Critic
  • Perl::Critic::Policy::ControlStructures::ProhibitCascadingIfElse
  • Perl::Critic::Policy::ControlStructures::ProhibitUnlessBlocks

🌈 HOME PAGE 🌈

<http://user42.tuxfamily.org/perl-critic-pulp/index.html>


Copyright 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde

Perl-Critic-Pulp is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version.

Perl-Critic-Pulp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.


okay that's the whole thing bestie, hope it slayed πŸ’– stay licensed, stay valid 🌸

this icon of a manual page was served up by perl v5.40.1 on 2025-10-21 β€” periodt. πŸŽ€

πŸ“¦ where this page comes from