wackattack has asked for the wisdom of the Perl Monks concerning the following question:

EDIT: I solved my own problem. Sorry to bother you.

I'm trying to create a -help option on a perl script. When -help is used it prints out the instructions without running the program. Right now I'm using die but it always prints out this after exiting:

at ./eigenvector_setup.pl line 8.
is there a way to exit the program without looking like it's a bug?

EDIT: nevermind I just found it. I just end with exit().

Replies are listed 'Best First'.
Re: Exiting a program
by shigetsu (Hermit) on Apr 17, 2007 at 20:45 UTC

    Quoting die:

  • die LIST

    If the last element of LIST does not end in a newline, the current script line number and input line number (if any) are also printed, and a newline is supplied.

    But you probably want to use exit here.

Re: Exiting a program
by graff (Chancellor) on Apr 18, 2007 at 03:05 UTC
    Other information you might find useful:

    • If you include "\n" at the end of a message passed to the "die" or "warn" function, perl will not include "at {scriptname} line {N}" after the given message. That extra information only gets added to STDERR output when the string passed to die or warn does not end with "\n".

    • You really should take a look at Pod::Usage -- I think it's one of the "core" modules (comes with perl); the examples in its man page should give you a nice idiom you'll want to use over and over.

    • Pod::Usage works very well in combination with Getopt::Long, which is also a core module, and also well worth getting acquainted with, if you haven't used it yet.

    Before I got hooked on Pod::Usage, my default template for command-line perl scripts included a line like:

    my $Usage = "Usage: $0 \n";
    and I would always put option flags, file args, etc, into that string, making it multi-line where appropriate, and if my arg checking indicated a need to give up (e.g. Getopt::Long returned false or some ad-hoc condition failed) that would lead to  die $Usage;

    But Pod::Usage is better, especially if you are in the (very good) habit of providing a decent POD man page in your command-line scripts, and you want to make it easy for users to see the man page.

    With my old habit, I usually regretted having to repeat the content of my "$Usage" variable in the "SYNOPSIS" section of POD. With Pod::Usage, I just have the SYNOPSIS section in POD (no redundant "$Usage" variable).