views:

102

answers:

1

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?

+14  A: 

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.

Sean
I guess they read the documentation. [perldoc -f die](http://perldoc.perl.org/functions/die.html)
davorg
I kind of wish there was a way to do the same with `croak`. My understanding is that semantically, `die` is for internal errors, while `croak` is for errors caused by something external. In this case, I want to indicate that the user entered invalid options, which is clearly a case of the latter. Yet I have to die instead of croak to suppress the backtrace.
Ryan Thompson