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

I saw some code that had
exit 6;
at the end of the subroutine. I was just wondering what's the difference between using exit 1; exit 0; or exit n where n is any number. my guess was that exit 0; is a false exit? and any value other than zero is a true exit...... am I close?

Replies are listed 'Best First'.
Re: exit EXPR
by abstracts (Hermit) on Aug 10, 2001 at 13:54 UTC
    Hello

    You got it backwards. In UNIX, exit(0) means success while any other exit value is false. Usually different status number indicate different errors. For example exit(1) if you cannot open a file, and exit(2) if network unreachable, and so on.

    Hope this helps,,,

    Aziz,,,

Re: exit EXPR
by busunsl (Vicar) on Aug 10, 2001 at 13:55 UTC
    The number given to exit will be the return code of your program, so that any calling script will have something to chew on.
    Whether 1 is true or false depends only on what the calling script makes of it.
    Some may interpret anything else than 0 as an error.
Re: exit EXPR
by faure (Sexton) on Aug 10, 2001 at 20:13 UTC
    Heard it explained like this once:

    As far as the system is concerned, 0 is an "uninteresting" exit... i.e. everything went fine.

    Non-zero exit codes are "interesting" b/c if something failed you'll (probably) want to know why.

    Which is something like perl's interesting/uninteresting system of true and false.

    But since since exceptions are also 'false' it's kind of counter-intuitive to have

    if ( chdir '/var/tmp' ) { print "Changed dirs successfully.\n"; } if ( system 'cd /var/tmp' ) { die "Couldn't change dirs: $!.\n"; }

    Somewhere LW says he wishes he would have reversed this.