rockneybot has asked for the wisdom of the Perl Monks concerning the following question:
User-defined subroutines to handle options Ultimate control over what should be done when (actually: each +time) an option is encountered on the command line can be achieved by + designating a reference to a subroutine (or an anonymous subroutine) + as the option destination. When GetOptions() encounters the option, +it will call the sub-routine with two or three arguments. The first a +rgument is the name of the option. For a scalar or array destination, + the second argument is the value to be stored. For a hash destinatio +n, the second arguments is the key to the hash, and the third argumen +t the value to be stored. It is up to the subroutine to store the val +ue, or do whatever it thinks is appropriate. A trivial application of this mechanism is to implement options + that are related to each other. For example: my $verbose = ''; # option variable with default value (f +alse) GetOptions ('verbose' => \$verbose, 'quiet' => sub { $verbose = 0 }); Here "--verbose" and "--quiet" control the same variable $verbo +se, but with opposite values. If the subroutine needs to signal an error, it should call die( +) with the desired error message as its argument. GetOptions() will c +atch the die(), issue the error message, and record that an error res +ult must be returned upon completion. If the text of the error message starts with an exclamantion ma +rk "!" it is interpreted specially by GetOptions(). There is current +ly one special command implemented: "die("!FINISH")" will cause GetOp +tions() to stop processing options, as if it encountered a double das +h "--".
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Getopt::Long subroutine usage
by atcroft (Abbot) on Jan 05, 2005 at 02:24 UTC | |
by rockneybot (Novice) on Jan 05, 2005 at 02:36 UTC | |
by graff (Chancellor) on Jan 05, 2005 at 06:02 UTC | |
by rockneybot (Novice) on Jan 05, 2005 at 08:21 UTC | |
by Solo (Deacon) on Jan 05, 2005 at 15:40 UTC | |
by rockneybot (Novice) on Jan 05, 2005 at 19:41 UTC | |
|
Re: Getopt::Long subroutine usage
by William G. Davis (Friar) on Jan 05, 2005 at 02:26 UTC | |
|
Re: Getopt::Long subroutine usage
by legato (Monk) on Jan 05, 2005 at 17:07 UTC |