die, warn and eval

die and warn

die prints a message to standard error and ends the program with a non-zero exit code. warn prints the same message and keeps going. Both add the file name and line number when the message does not end with a newline.

my $file = "missing.txt"; warn "optional file missing\n" unless -e $file; die "cannot continue: no data\n" unless -e $file; print "never reached\n";

perl demo.pl

optional file missing
cannot continue: no data

Nothing reaches the last line: the die ends the program on the spot. Neither message carries a file name or line number, because both end with a newline.

The trailing newline is a switch, not a style choice: with it, Perl trusts your message as complete; without it, Perl appends the location so you can find the culprit. Keep the newline in messages a user will read, and drop it for internal assertions.

The $! variable

When a system call fails, the operating system reason lands in $!. Interpolating it into a die message turns cannot open into a sentence that names the problem.

open my $fh, "<", "/root/secret" or die "open failed: $!\n";

perl demo.pl

open failed: Permission denied

In numeric context $! gives the raw errno value, which is useful in logs that a monitoring system will parse. In string context it gives the English text.

eval

eval runs a block and catches a die instead of letting it end the program. On success it returns the value of the block; on failure it returns undef and leaves the error message in $@.

my $result = eval { die "bad input\n" if !defined $ARGV[0]; $ARGV[0] * 2; }; if ($@) { warn "recovered: $@"; } else { print "result $result\n"; }

perl demo.pl

recovered: bad input

perl demo.pl 21

result 42

With no argument the block dies, the warn prints the caught message and the program carries on. Give it a number and the same block doubles it.

Two rules make eval safe to use. Check $@ immediately, before any other call can clear it, and keep the block small so you know exactly what it protects.

autodie

Writing or die after every open gets old. The autodie pragma makes the common system calls throw an exception on failure, so the happy path stays clean and one eval can catch everything.

use autodie; my $ok = eval { open my $fh, "<", "missing.txt"; my @lines = <$fh>; print scalar @lines, " lines\n"; 1; }; warn "read failed: $@" unless $ok;

perl demo.pl

read failed: Can't open 'missing.txt' for reading: 'No such file or directory' at demo.pl line 4

The exception carries the file name and line number, because the message autodie builds has no trailing newline of its own. The line number is the open call, the fourth line of the program above.

autodie is in the core since Perl 5.10.1, so there is no install step. It covers open, close, mkdir, unlink and the other IO and filesystem calls, which is where most of the unhandled failures in small scripts come from.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is the difference between die and warn?

    Answer

    die ends the program with a non-zero status; warn prints to standard error and continues

    Both write to standard error, but only die stops the program, which is why the line after it never runs.

  2. When does Perl add the file name and line number to a die or warn message?

    Answer

    Only when the message has no newline of its own

    A trailing newline tells Perl the message is complete, so the location is left out.

  3. What does $! hold after a failed open?

    Answer

    The operating system reason: the English text in string context, the raw errno in numeric context

    Interpolating $! into the die message turns "cannot open" into a sentence that names the problem.

  4. What does an eval block return when the block dies?

    Answer

    undef, with the message in $@

    On success the block value comes back; on failure the value is undef and $@ holds the message.

  5. Why must $@ be checked immediately after the eval?

    Answer

    Because another call that fails or succeeds can change it first

    A later system call can overwrite the error, so the check belongs directly after the block.

  6. Since which Perl release is autodie in the core?

    Answer

    5.10.1

    It has shipped with Perl since 5.10.1, so there is no install step and the pragma always works.

  7. What does this program print? my $x = eval { die "boom\n" }; print $@ ? "caught\n" : "not caught\n";

    Answer

    caught

    The eval catches the die, so the program continues and $@ is true.

  8. What does this program print? my $r = eval { 21 * 2 }; print $r, "\n";

    Answer

    42

    A block that does not die returns its last value, which the eval passes through.

  9. What does this program print on standard output? warn "heads up\n"; print "still here\n";

    Answer

    still here

    The warn goes to standard error, so the only line on standard output is the print.

  10. A program runs this with no eval around it. What does the reader see? die "fatal\n"; print "never\n";

    Answer

    fatal on standard error, and never is not printed

    The die ends the program at once, so the print after it never runs and the message goes to standard error.

Article author: Arthur Isaev