-2

So, I came across a PERL script while working on an already implemented project. Since its confidential, I cannot reveal much details but the code is somewhat like this :

if(defined($x)){
    exit(0);
}
#...........
#.some other code.
#...........

if(!defined($y)){
      print "ERROR !";
      exit(1);
}

I am assuming exit(1) is in error conditions and exit(0) is for success cases. But since we are already printing the error message, what is the difference here between exit(0) and exit(1)?

  • 2
    This is not limited to Perl. [Exit codes](https://en.wikipedia.org/wiki/Exit_status) are a general concept to have a program signal whether it could do what it was asked or whether something went wrong, so that other programs (usually the calling program) can react to it. – Robert Aug 14 '20 at 20:50
  • @CharlesDuffy, PERL and linux work the same way , is that what it is? – Ashlesha Sunil Agate Aug 14 '20 at 21:07
  • The name of the language is Perl, not PERL; it's not an acronym. – ikegami Aug 14 '20 at 21:16
  • @AshleshaSunilAgate, `exit` in perl is the same as exiting in C or any other language, so yes, normal operating system conventions apply as to the meaning of that exit status and how other programs are going to interpret it. – Charles Duffy Aug 14 '20 at 21:23
  • @AshleshaSunilAgate, ...whereas `exit 0` is how one writes "exit with a status of success" in shell, and `exit(0)` is how one writes it in perl, the meaning is the same both ways. – Charles Duffy Aug 14 '20 at 21:25

1 Answers1

6

The process that spawned the program can check what number you returned. 0 typically means "no error" (and it's the default exit status for Perl scripts). Other values are program specific.

For example, grep exits with

  • 0, when it found a match
  • 1, when it found no matches
  • 2, when an error occurred

so you can do

if grep -q foo file; then
   echo 'match'
else
   echo 'no match (or error)'
fi

You probably should have used die instead of print and exit. die("$msg\n") outside of an eval is roughly equivalent to

print STDERR "$msg\n";
exit( $! || ( $? >> 8 ) || 255 );

So it's simpler, the message is printed to STDERR (not STDOUT) as it should, and it's guaranteed to exit with a non-zero code (and hopefully one that is meaningful when debugging).

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • Can you explain the difference in what is printed or happens internally in my case ? – Ashlesha Sunil Agate Aug 14 '20 at 21:07
  • What do you mean by "your case", and difference between your case and what? – ikegami Aug 14 '20 at 21:15
  • @AshleshaSunilAgate: From the perspective of your code, there is no difference. But something is calling your program (whether that's another program or the user, via a command shell). If whatever is calling your program checks the return value, then it will get 0 in the first case and 1 in the second. It is a Unix tradition that 0 means the program ran successfully and any non-zero return values indicate errors. The meaning of specific non-zero return values is specific to the program in question. – Dave Cross Aug 16 '20 at 18:53