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
unlessstatement to check whether the directoryetceexists.
- Line 2: Since the directory
etcedoes not exist, thediestatement terminates the execution of the code and causes an error message to be thrown.