- The die command... well... dies.
- In fact, the command not only dies, but the entire Perl program
being executed is immediately terminated with extreme prejudice.
- The status returned from the perl program to the shell is nonzero
(the traditional Unix shell "I had an error" return).
- You can use another magic variable with die, the "$!"
variable, which contains the most recent operating system error.
- Example:
# Open the unix password file for reading
$theFile = "/etc/passwd";
unless ( open ( PASSWORD, $theFile ) ) {
die "I can't seem to open $theFile.\nError was $!\n";
}
- Example:
# Open the unix password file for reading
$theFile = "/etc/passwd";
open ( PASSWORD, $theFile ) || die "Unable to open $theFile.\nError was $!\n";
- If the string argument to die does not include a trailing "\n",
then the output message will include the perl script name and the line
number where the "die" was invoked. If you include a trailing "\n",
then this information is not included.
- You can also make it a near death experience, but not actually
kill the program, with the warn directive.
- It does everything the same as the die directive, but
program execution continues.
- Example:
# Open the unix password file for reading
$theFile = "/etc/passwd";
unless ( open ( PASSWORD, $theFile ) ) {
warn "I can't seem to open $theFile. Continuing anyway.\n";
}