How to terminate a program in Perl - exit or die?

后端 未结 3 1481
隐瞒了意图╮
隐瞒了意图╮ 2021-02-05 02:08

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, a

3条回答
  •  天命终不由人
    2021-02-05 02:37

    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 -f or paginate it, and at those times writing to STDERR causes them pain (they have to redirect STDERR back into STDOUT with 2>&1, and not everyone knows how to do that). So which to use, die or print and exit, depends heavily on what type of program you are writing.

    There are other benefits/drawbacks to using die:

    • You can trap die with an eval, but not exit
    • You can run code when the program calls die by installing a signal handler for the __DIE__ signal
    • You can easily override the die function

    Each 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.

提交回复
热议问题