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

Please explain the difference between "exit" and "exit 0" and "exit 1". Is it the same in NT and unix?
if($variable = 1) { print "Exit the program\n"; exit 0; }
When should I use "exit" or "exit 0" or "exit 1"?

Replies are listed 'Best First'.
Re: Exit information needed
by Abigail-II (Bishop) on Jul 07, 2003 at 22:33 UTC
    As the manual page would have told you, exit and exit 0 are the same. The difference between exit 0 and exit 1 is that the former makes that the program has an exit value of 0, and the latter an exit value of 1.

    The convention is that an exit value of 0 indicates the program ran succesfully, while an exit value other than 0 means an error of some kind occurred.

    Abigail

Re: Exit information needed
by TVSET (Chaplain) on Jul 07, 2003 at 22:34 UTC
    With exit you can specify the return code for your program. Usually, 0 is used for successful execution, and positive/non-zero for indication of error. Read perldoc -f exit for more information.

    I don't know about Windows, but in Unix you can do something like:

    /path/to/myscript.pl || echo "Script failed big time"

    The above will execute the script and let me know if something went wrong and script did not finish successfully.

    Leonid Mamtchenkov aka TVSET

Re: Exit information needed
by Anonymous Monk on Jul 07, 2003 at 22:39 UTC

    You may want to say

    if ($variable == 1) # ^ # |

    or else your comparison is always true.

Re: Exit information needed
by hawtin (Prior) on Jul 08, 2003 at 08:37 UTC

    An excellent question. I was brought up on UNIX machines where you should always check the return code of any program you run. As has already been stated a return value of 0 indicates success, anything other value indicates something went wrong.

    On UNIX the value returned by system (and its colleagues) is signal_number+256*exit_value. This means that when we call an external program we should inspect the return and interpret the error if there was one. I have a routine to do that (below).

    I tested this out on Windows (running 5.005!) and it works the same there.

    In my programs I have a special version of system so when I want to run an external program I do:

    &do_system("perl exit1.pl");

    and I don't have to keep remembering to say:

    system("perl exit1.pl") || die "Failed to run exit1.pl\n";

    Anyway here is my old function for your edification…

    # This is Perl4 code, I leave the translation to Perl5 # to the reader sub do_system { local($cmd, $reportpattern) = @_; local($ret); print "# Do command $cmd\n" if($verbose); $ret = system($cmd); &interp_ret($cmd, $?, $reportpattern); return($ret); } sub interp_ret { local($cmd,$ret,$reportpattern) = @_; local($sig,$val,%sig_names,$sig_name); return if(!$ret); $sig = ($? & 0xff); $val = $? >> 8; return if ($reportpattern && ! eval($reportpattern)); # Not quite portable but gets close enough %sig_names = (1 ,"SIGHUP" ,2 ,"SIGINT" ,3 ,"SIGQUIT" ,4 ,"SIGILL" ,5 ,"SIG +TRAP" , 6 ,"SIGIOT" ,7 ,"SIGABRT",8 ,"SIGFPE" ,9 ,"SIGKILL" , 10,"SIGBUS" ,11,"SIGSEGV" ,12,"SIGSYS",13,"SIGPIPE",14,"SIGA +LRM", 15,"SIGTERM", 16,"SIGURG",17,"SIGSTOP",18,"SIGTSTP",19,"SIGC +ONT", 20,"SIGCHLD",21,"SIGTTIN",22,"SIGTTOU",23,"SIGIO",24,"SIGXCP +U", 25,"SIGXFSZ",26,"SIGVTALRM",27,"SIGPROF",28,"SIGWINCH",29,"S +IGLOST", 30,"SIGUSR1",31,"SIGUSR2",); print "\nIn script $0 the command \"$cmd\" seemed to fail\n"; $sig_name = $sig_names{$sig}; $sig_name = "Unknown SIG $sig" if(!$sig_name); if($sig && $val) { print "Signal $sig_name caused return $val\n"; } elsif($sig) { print "Signal $sig_name\n"; } else { print " Returned status $val\n"; } }
      Thanks for all the info!!!
Re: Exit information needed
by Mago (Parson) on Jul 09, 2003 at 03:34 UTC

    exit EXPR

    Evaluates EXPR and exits immediately with that value.
    Example:
    $ans = <STDIN>; exit 0 if $ans =~ /^[Xx]/;

    See also die. If EXPR is omitted, exits with 0 status. The only universally recognized values for EXPR are 0 for success and 1 for error; other values are subject to interpretation depending on the environment in which the Perl program is running. For example, exiting 69 (EX_UNAVAILABLE) from a sendmail incoming-mail filter will cause the mailer to return the item undelivered, but that's not true everywhere.
    Don't use exit to abort a subroutine if there's any chance that someone might want to trap whatever error happened. Use die instead, which can be trapped by an eval.
    The exit() function does not always exit immediately. It calls any defined END routines first, but these END routines may not themselves abort the exit. Likewise any object destructors that need to be called are called before the real exit. If this is a problem, you can call POSIX:_exit($status) to avoid END and destructor processing.