I am writing a perl script, and in the part where I am checking the options that the user supplied on the command line, I want to exit with an error explaining what was wrong with the options. In this case, there is no need for a stack trace to go along with this error message. So what is the best way to die with an error message but no stack trace or debug information?
I've tried the following:
die "Invalid options";
which produces
Invalid options at myscript.pl line 49.
Then, I tried
use Carp;
...
croak "Invalid options";
which produces
Invalid options at myscript.pl line 47
main::prepare_output_directory() called at myscript.pl line 546
So how can I just die with Invalid options
and nothing else?
Bonus question: why does croak
tell me that the error is at line 47, 开发者_运维问答when the actual call to croak
is on line 49?
Just put a newline at the end of the die
string:
die "Invalid options\n";
That will prevent the location of the error from being appended to your message.
精彩评论