home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

3.2.27 die



Outside of an eval , this function prints the concatenated value of LIST to STDERR and exits with the current value of $! ( errno ). If $! is 0, it exits with the value of ($? >> 8) (which is the status of the last reaped child from a system , wait , close on a pipe, or `command`). If ($? >> 8) is 0, it exits with 255. If LIST is unspecified, the current value of the $@ variable is propagated, if any. Otherwise the string "Died" is used as the default.

Equivalent examples:

die "Can't cd to spool: $!\n" unless chdir '/usr/spool/news';

chdir '/usr/spool/news' or die "Can't cd to spool: $!\n"

(The second form is generally preferred, since the important part is the chdir .)

Within an eval , the function sets the $@ variable equal to the error message that would have been produced otherwise, and aborts the eval , which then returns the undefined value. The die function can thus be used to raise named exceptions that can be caught at a higher level in the program. See the section on the eval function later in this chapter.

If the final value of LIST does not end in a newline, the current script filename, line number, and input line number (if any) are appended to the message, as well as a newline. Hint: sometimes appending ", stopped" to your message will cause it to make better sense when the string "at scriptname line 123" is appended. Suppose you are running script canasta :

die "/etc/games is no good";
die "/etc/games is no good, stopped";

which produces, respectively:

/etc/games is no good at canasta line 123.
/etc/games is no good, stopped at canasta line 123.

If you want your own error messages reporting the filename and linenumber, use the __FILE__ and __LINE__ special tokens:

die '"', __FILE__, '", line ', __LINE__, ", phooey on you!\n";

This produces output like:

"canasta", line 38, phooey on you!

See also exit and warn .