What is the die statement in Pearl?

Overview

The die statement is used to terminate the execution of a code when an error occurs. It causes the code execution to stop and display an error message when an error occurs in the code.

Syntax

The syntax of the die statement is given below:

die('error message')
Syntax of the die statement in Pearl

Example

unless(chdir("/etce")) {
die "Error: Can't change directory - $!";
}

  • Line 1: We use the unless statement to check whether the directory etce exists.
  • Line 2: Since the directory etce does not exist, the die statement terminates the execution of the code and causes an error message to be thrown.

Free Resources