Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exit Codes when using die in Perl

I have overridden die in perl for my logging framework, so that it can log messages and print it on console.

Overridden code for die:

BEGIN{ *CORE::GLOBAL::die = sub { 
        my ($package, $filename, $line, $subroutine) = caller;
        untie *STDERR;
        my $message;
        foreach my $arg (@_) {
            $message = $message.$arg;
        }

        print STDERR $message;
        tie *STDERR, __PACKAGE__, (*STDERR);
        logmessage("die",$message,$filename, $line);
        #What exit code to pass?
        #exit CODE;
    }
}

I don't know what exit code to set while exiting the process as the normal die exits with an error code.

  • Is there any way I can find out what exit code to set when die is called?

  • Also It would be helpful if can know the list of error codes availabe in perl?

like image 243
Dinesh Gowda Avatar asked May 20 '26 22:05

Dinesh Gowda


1 Answers

The exit code is documented in die:

 exit $! if $!;              # errno
 exit $? >> 8 if $? >> 8;    # child exit status
 exit 255;                   # last resort

But as @amon noted, die doesn't exit, it throws an exception. Instead of overriding it, it might be clearer to wrap the whole thing into an eval { ... ; 1 } (or Try::Tiny's try) and log the exception in the or do or catch part.

like image 71
choroba Avatar answered May 23 '26 14:05

choroba