Perl::Critic::Policy::ControlStructures::ProhibitUnreachableCode(3pm)

Perl::Critic::Policy::ControlStructures::ProhibitUnreachableCode3pm

Don't write code after an unconditional "die, exit, or next".

Affiliation

This Policy is part of the core Perl::Critic distribution.

Overview

This policy prohibits code following a statement which unconditionally alters the program flow. This includes calls to "exit", "die", "return", "next", "last" and "goto". Due to common usage, "croak" and "confess" from Carp are also included.

Code is reachable if any of the following conditions are true:

•

Flow-altering statement has a conditional attached to it

•

Statement is on the right side of an operator "&&", "||", "//", "and", "or", or "err".

•

Code is prefixed with a label (can potentially be reached via "goto")

•

Code is a subroutine

Examples

  # not ok
  exit;
  print "123\n";
  # ok
  exit if !$xyz;
  print "123\n";
  # not ok
  for ( 1 .. 10 ) {
      next;
      print 1;
  }
  # ok
  for ( 1 .. 10 ) {
      next if $_ == 5;
      print 1;
  }
  # not ok
  sub foo {
      my $bar = shift;
      return;
      print 1;
  }
  # ok
  sub foo {
      my $bar = shift;
      return if $bar->baz();
      print 1;
  }
  # not ok
  die;
  print "123\n";
  # ok
  die;
  LABEL: print "123\n";
  # not ok
  croak;
  do_something();
  # ok
  croak;
  sub do_something {}

Configuration

This Policy is not configurable except for the standard options.

Releases

10 releases since Debian 5.0 (Lenny); the text changed 9 times.

  1. 5.0
  2. 6.0
  3. 7
  4. 8
  5. 9
  6. 10
  7. 11
  8. 12
  9. 13
  10. Arch

Author

Peter Guzis <[email protected]>

From perl-perl-critic 1.156-3 in Arch Linux 2026-11-01. Parsed by carta from the page's man source.