http://qs321.pair.com?node_id=479355

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

Great studious and wise monks I am guilty of the sin of typing the same thing into my code more than once.

I feel I must have been negligent in my study of the great cannon of wisdom. My code uses Getopts::Long to set a bunch of switches. When the switches are initialised their use is documented in comments. GetOptions then checks the command line using command line switch names almost the same as the $variable names (easy to make the same). Then I have a usage sub which again lists all these switches and reproduces the same info as the #comments.

A Super Search drowned me in links but did not turn up anything that look relevant.

Is there a simple way to write this information once and have both GetOptions, variable initialisation and the usage sub grab it from the one place ? Is there an existing module ? Do people have their own ad-hoc ways to do this. Should I be thinking POD here ?

Here is an example chunk of code exhibiting this sin. It even exhibits the other problem this causes, available switches do not match usage documentation (slowdown option works but not documented)

###################################################################### +########## our $debug = 0; # switch on debuging our $verbose = 0; # important info to STDOUT my $help = 0; # show usage my $force = 0; # run even if less than 5 since last run my $slowdown = 0; # run slow (useful to test locking) my $test = 0; # send no events my $clean_cache = 0; # clean ep cache of obsolete entries found my $max_probs = 150; # max number ep problems my $sched = 11; # schedule frequency my $monitor_freq = 5; # frequency of string script monitor my $Options_OK= GetOptions ("debug" => \$debug, "verbose" => \$verbose, "help" => \$help, "force" => \$force, "clean" => \$clean, "slow" => \$slowdown, "test" => \$test, "probs=i" => \$max_probs, "schedule=i" => \$sched, ); unless ($Options_OK) { print "Sorry you gave a bad command line option\n"; usage (); exit 1; } if ($help) { usage (); exit 0; } sub usage { print "\nusage: ".SCRIPTNAME."[-debug|-verbose] [-help] [-force] [ +-clean] [-test] [-probs] [-schedule]\n"; print " -debug = switch on debuging\n"; print " -help = show this usage\n"; print " -verbose = important infor to STDOUT\n"; print " -force = run even if less than 5 mins since last + run\n"; print " -clean = clean obsolete entries found in ep cach +e\n"; print " -test = send no events to T/EC\n"; print " -probs = max number ep problems before complaini +ng\n"; print " -schedule = frequency that script is being run in m +inutes\n"; print "\n"; print "NOTE: any of the command line options can be abbreviated to + the\n"; print "shortest unique string, ie -h for help is good, but if we h +ad\n"; print "options hex & help then you would use -hex and -hel as mini +mum\n\n"; }

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!