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

Is there a way for me to specify my own return number when I issue a "die" command?
By default die return code is 255. I would like to use a different number.

I know that exit # will work. I can't find a solution for die.

Thanks.

Replies are listed 'Best First'.
Re: Changing the return number for "die"
by dpuu (Chaplain) on Jul 20, 2004 at 23:21 UTC
    If you want to do it one-off, then you can simply set $! before you call die. If you want to set it more globally, then you might try:
    $SIG{__DIE__} = sub { $! = 42; die @_ };
    If you ever catch errors using eval, then you might want to be a bit more careful:
    $SIG{__DIE__} = sub { $! = 42 unless $^S; die @_ };
    --Dave
    Opinions my own; statements of fact may be in error.
Re: Changing the return number for "die"
by ambrus (Abbot) on Jul 20, 2004 at 22:11 UTC

    There's no direct way, but there's a kludge: in an END block you can change the exit code by changing $?. Thus, you can do this:

    $EXIT = ~0 END { $? and $? = $EXIT; } sub xdie { $EXIT = shift; die @_; } xdie 16, "argh";

    Update: See the other reply for a simpler way:

    sub xdie { $! = shift; die $@; } xdie 16, "argh";
Re: Changing the return number for "die"
by ccn (Vicar) on Jul 20, 2004 at 17:07 UTC

    see perldoc -f die

Re: Changing the return number for "die"
by radiantmatrix (Parson) on Jul 21, 2004 at 14:45 UTC
    You may wish to consider using an internal die.
    sub _die { my $err_no = shift; my @msg = @_; print STDERR join "\n",@msg; exit $err_no; }
    Alternatively, replace exit $err_no; with ($! = $err_no) && (die());.

    Then call _die($err_num, "This thing is dead.") when you want the alternate behavior.

Re: Changing the return number for "die"
by etcshadow (Priest) on Jul 20, 2004 at 17:37 UTC
    Or possibly look at exit $code.
    ------------ :Wq Not an editor command: Wq