I am using syntax checking to see whether my Perl script is being used in the correct way. If the syntax is not correct, I display a message saying what the correct syntax is, and then end the execution of the program.
Between:
print "Please use the following syntax: ...";
exit 1;
and:
die("Please use the following syntax: ...");
Which one should I use? I know die would have been the answer if exception handling was in place, but that is not the case.
Which one is the better choice for program termination without exception handling? Please also say why.
It depends on what you want to have happen with STDERR and STDOUT. I prefer to send error and warning type messages to STDERR so that when someone is trying to redirect output to a file they still see the error messages; However, there are times though when STDOUT is used to communicate status to the user so he or she can
tail -for paginate it, and at those times writing to STDERR causes them pain (they have to redirect STDERR back into STDOUT with2>&1, and not everyone knows how to do that). So which to use,dieorprintandexit, depends heavily on what type of program you are writing.There are other benefits/drawbacks to using
die:diewith aneval, but not exitdieby installing a signal handler for the__DIE__signaldiefunctionEach of those has times where it is handy to be able to do them, and times when it is a pain that they can be done.