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 "--".
In reply to Getopt::Long subroutine usage by rockneybot
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |