Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

Can any one enlighten me why the exit values are different in below code snippet? One is exit(-1) & other is exit(EBADF).

user@ubuntu:~$ perl -e 'die "Hello"';echo $? Hello at -e line 1. 255 user@ubuntu:~$ perl die "Hello" Hello at - line 1. user@ubuntu:~$ echo $? 9

Replies are listed 'Best First'.
Re: perl die & exit values
by Corion (Patriarch) on Feb 26, 2018 at 14:55 UTC

    See the documentation of die:

    If an uncaught exception results in interpreter exit, the exit code is determined from the values of $! and $? with this pseudocode:
    exit $! if $!; # errno exit $? >> 8 if $? >> 8; # child exit status exit 255; # last resort

    And likely, $! is set from some other operation that your script (or Perl) performed. So you can't rely on a specific exit code if your script dies.

    If you need something really specific, you could install your own handler via either an END block or a $SIG{__DIE__} handler (perlvar).

      > you can't rely on a specific exit code if your script dies

      What about setting $! just before you die?

      $ perl; echo $? $!=42; die "Hello" __END__ Hello at - line 1. 42

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,