1

How can I get a perl program to print the POD contents when an incorrect argument or number of arguments are passed in?

Jonas
  • 121,568
  • 97
  • 310
  • 388
Dave
  • 4,184
  • 8
  • 40
  • 46
  • The classic Unix convention is to give a simple 'usage' message when something goes wrong, not run `man cmdname`. If someone needs the full manual, they can request it separately. (You might include 'For more information, see "perldoc cmdname"' in the usage since it might not be obvious that it is a Perl script and that `man cmdname` is probably incorrect.) A command that insisted on producing its manual (probably to standard output, so the manual goes down the pipeline I'm trying to run) would be less than helpful. That's also a reason the usage must go to standard error, not standard output. – Jonathan Leffler Nov 11 '12 at 16:47

3 Answers3

8

I'd use the Pod::Usage module

The Getopt::Long module has a good example on its usage.

Joel Berger
  • 20,180
  • 5
  • 49
  • 104
FishMonger
  • 77
  • 1
-1

You can call exec("perldoc", $0); when arguments is wrong. So user will get man-like help.

Galimov Albert
  • 7,269
  • 1
  • 24
  • 50
  • There is no need to shell out for this purpose. Also some Linux distros come crippled without the `perldoc` command, so its not good (nor necessary) to depend on its existence. – Joel Berger Nov 11 '12 at 15:33
  • Sure, asker can open `perldoc` source and see he can use `Pod::Perldoc` but why not? Since program is supposed to run manually - its very simple to fix that if it will needed at all. Just use simple solutions to simple questions. – Galimov Albert Nov 11 '12 at 15:50
  • Because Pod::Usage is made for this purpose, its simple and it doesn't involve shelling out. – Joel Berger Nov 11 '12 at 16:15
  • Note that I'm not arguing that you are incorrect. I wouldn't have even commented if the OP hadn't already pick this answer as correct. Its not wrong, nor incorrect, but its not the best, and I wanted to be sure that the OP and future readers saw that. – Joel Berger Nov 11 '12 at 16:17
-1

If it about your program, I think you have to use:

sub usage {
  print<<help
  Usage $0: description of your script
help
}

if (($#ARGV+1) != $count_args) { usage; }
edem
  • 3,222
  • 3
  • 19
  • 45