Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to die with an error but without a stack trace in perl?

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?

like image 527
Ryan C. Thompson Avatar asked Aug 29 '10 05:08

Ryan C. Thompson


1 Answers

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.

like image 112
Sean Avatar answered Sep 28 '22 00:09

Sean