.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Getargs::Long 3pm" .TH Getargs::Long 3pm "2023-08-17" "perl v5.36.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Getargs::Long \- Named subroutine arguments, with optional type checking .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& use Getargs::Long; # case sensitive \& use Getargs::Long qw(ignorecase); # case insensitive \& \& # Simple, args mandatory \& my ($val, $other) = getargs(@_, qw(val other)); \& \& # Simple, args optional (in [] means optional) \& my ($val, $other) = getargs(@_, [qw(val other)]); \& \& # Simple with typechecking, args mandatory \& my ($val, $other) = getargs(@_, qw(val=Class::X other=ARRAY)); \& \& # Simple with typechecking, args optional \& my ($val, $other) = getargs(@_, [qw(val=Class::X other=ARRAY)]); \& \& # Faster version, building dedicated argument parsing routine \& my ($val, $other) = cgetargs(@_, qw(val other)); \& \& # Other cases, use full specs: \& my ($x, $y, $z, $a, $b, $c) = xgetargs(@_, \& \& # Non\-mandatory, defaults to undef unless specified otherwise \& \*(Aqx\*(Aq => [\*(Aqi\*(Aq], # integer, no default \& \*(Aqy\*(Aq => [\*(AqARRAY\*(Aq, [\*(Aqa\*(Aq, \*(Aqb\*(Aq]], # Has a default \& \*(Aqz\*(Aq => [], # No typecheck, can be anything \& \& # Mandatory arguments \& \*(Aqa\*(Aq => \*(Aqi\*(Aq, # integer (scalar) \& \*(Aqb\*(Aq => \*(AqTYPE\*(Aq, # TYPE or any heir of TYPE \& \*(Aqc\*(Aq => undef, # unspecified type but mandatory \& ); \& \& # Extract remaining unparsed args in @extra \& my ($val, $other, @extra) = getargs(@_, { \-strict => 0 }, qw(val other)); \& \& # Alter behaviour of the getargs() routines via switches in hashref \& my ($val, $other) = getargs(@_, \& { \& \-strict => 1, # unknown switches are fatal \& \-ignorecase => 1, # override package\*(Aqs global \& \-inplace => 1, # edit @_ inplace: remove parsed args \& \-extra => 0, # suppress return of extra arguments \& }, \& qw(val other) \& ); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" The \f(CW\*(C`Getargs::Long\*(C'\fR module allows usage of named parameters in function calls, along with optional argument type-checking. It provides an easy way to get at the parameters within the routine, and yields concise descriptions for the common cases of all-mandatory and all-optional parameter lists. .PP The validation of arguments can be done by a structure-driven routine \&\fBgetargs()\fR which is fine for infrequently called routines (but should be slower), or via a dedicated routine created and compiled on the fly the fist time it is needed, by using the \fBcgetargs()\fR family (expected to be faster). .PP The \f(CW\*(C`Log::Agent\*(C'\fR module is used to report errors, which leaves to the application the choice of the final logging method: to a file, to \&\s-1STDERR,\s0 or to syslog. .SH "EXAMPLES" .IX Header "EXAMPLES" Before going through the interface specification, a little example will help illustrate both caller and callee sides. Let's write a routine that can be called as either: .PP .Vb 3 \& f(\-x => 1, \-y => 2, \-z => 3); # \-switch form \& f(x => 1, y => 2, z => 3); # concise form (\- are optional) \& f(y => 1, x => 2); # order changed, z may be omitted .Ve .PP Since we have an optional parameter \fIz\fR but mandatory \fIx\fR and \fIy\fR, we can't use the short form of \fBgetargs()\fR and must therefore use \fBxgetargs()\fR: .PP .Vb 8 \& sub f { \& my ($x, $y ,$z) = xgetargs(@_, \& \-x => \*(Aqi\*(Aq, # mandatory, integer \& \-y => \*(Aqi\*(Aq, # mandatory, integer \& \-z => [\*(Aqi\*(Aq, 0], # optional integer, defaults to 0 \& ); \& # code use $x, $y, $z \& } .Ve .PP That's quite simple and direct if you think of [] as \*(L"optional\*(R". Note that we pass \fBxgetargs()\fR a \fIreference\fR to \f(CW@_\fR. .PP If we had all arguments mandatory and wished to nonethless benefit from the named specification at call time to avoid having the caller remember the exact parameter ordering, we could write: .PP .Vb 4 \& sub f { \& my ($x, $y ,$z) = getargs(@_, qw(x=i y=i z=i)); \& # code of f \& } .Ve .PP Without parameter type checking, that would be even more concise. Besides, if f() is frequently called, it might be more efficient to build a routine dynamically to parse the arguments rather than letting \fBgetargs()\fR parse the same data structures again and again: .PP .Vb 4 \& sub f { \& my ($x, $y ,$z) = cgetargs(@_, qw(x y z)); # \*(Aqc\*(Aq for cached/compiled \& # code of f \& } .Ve .PP If you call f() with an improper argument, \fBlogcroak()\fR will be called to issue an exception from the persepective of the caller, i.e. pointing to the place f() is called instead of within f() at the \fBgetargs()\fR call, which would be rather useless. .PP Here are some more examples: .PP Example 1 \*(-- All mandatory: .PP .Vb 4 \& sub f { \& my ($port, $server) = getargs(@_, \& qw(port=i server=HTTP::Server)); \& } \& \& f(\-server => $server, port => 80); # or \-port, since \- is optional \& f(port => 80, server => $server); \& f(server => $server); # WRONG: missing mandatory \-port \& f(server => 80, port => 80); # WRONG: \-server not an HTTP::Server \& f(server => undef, port => 80); # WRONG: \-server cannot be undef .Ve .PP Example 2 \*(-- All optional .PP .Vb 3 \& sub cmd { \& my ($a, $o) = getargs(@_, [qw(a o=s)]); \& } \& \& cmd(); # OK \& cmd(\-a => undef); # OK \-a accepts anything, even undef \& cmd(\-a => 1, \-o => ".."); # OK \& cmd(\-a => 1, \-o => undef); # WRONG: \-o does not accept undef \& cmd(\-x => 1); # WRONG: \-x is not a known argument name .Ve .PP Example 3 \*(-- Mixed optional / mandatory .PP .Vb 6 \& sub f { \& my ($x, $z) = xgetargs(@_, \& \-x => \*(Aqi\*(Aq, # \-x mandatory integer \& \-z => [\*(Aqn\*(Aq, \-20.4], # \-z optional, defaults to \-20.4 \& ); \& } \& \& f(x => 1, z => {}); # WRONG: z is not a numerical value \& f(z => 1, x => \-2); # OK \& f(\-z => 1); # WRONG: mandatory x is missing \& f(\-z => undef); # WRONG: z cannot be undef .Ve .PP Example 4 \*(-- Parsing options .PP .Vb 7 \& sub f { \& my ($x, $z) = xgetargs(@_, \& { \-strict => 0, \-ignorecase => 1 }, \& \-x => \*(Aqi\*(Aq, # \-x mandatory integer \& \-z => [\*(Aqn\*(Aq, \-20.4], # \-z optional, defaults to \-20.4 \& ); \& } \& \& f(x => 1, foo => {}); # OK, \-foo ignored since not strict \& f(\-X => 1); # OK, \-X actually specifies \-x with ignorecase .Ve .SH "INTERFACE" .IX Header "INTERFACE" All the routines take a mandatory first argument, called \fIarglist\fR, which is the array containing the named arguments for the routine (i.e. a succession of \fIname\fR => \fIvalue\fR tuples). This array is implicitely passed as reference, and will usually be given as \f(CW@_\fR. .PP All the routines take an optional \fIoptions\fR argument which comes in the second place. It is an hash reference containing named options that alter the behaviour of the routine. More details given in the Options section. .PP All the routines return a list of the arguments in the order they are specified, each \fIslot\fR in the list being either the argument value, if present, or \f(CW\*(C`undef\*(C'\fR if missing (and not mandatory). .SS "Simple Cases" .IX Subsection "Simple Cases" Simple cases are handled by \fBgetargs()\fR: named arguments should either be \&\fIall mandatory\fR or \fIall optional\fR, and there is no provision for specifying a default value for optional parameters. .PP The \fBgetargs()\fR routine and its cousin \fBcgetargs()\fR have two different interfaces, depending on whether the arguments are all mandatory or all optional. We'll only specify for \fBgetargs()\fR, but the signature of \fBcgetargs()\fR is identical. .IP "getargs \fIarglist\fR, \fIoptions\fR, \fIarg_spec1\fR, \fIarg_spec2\fR, ..." 4 .IX Item "getargs arglist, options, arg_spec1, arg_spec2, ..." We'll be ignoring the \fIoptions\fR argument from our discussion. See the Options section for details. .Sp All the routine formal arguments specified by \fIarg_spec1\fR, \fIarg_spec2\fR, etc... are mandatory. If \fIarg_spec1\fR is only a name, then it specifies a mandatory formal argument of that name, which can be of any type, even undef. If the name is followed by \f(CW\*(C`=type\*(C'\fR then \f(CW\*(C`type\*(C'\fR specifies the argument type: usually a reference type, unless 'i', 'n' or 's' is used for integer, natural and string scalars. .Sp Currently, types 'i', 'n' and 's' all mean the same thing: that the argument must be a scalar. A future implementation will probably ensure \&'i' and 'n' hold integers and natural numbers respectively, 's' being the placeholder for anything else that is defined. .Sp For instance: .Sp .Vb 5 \& foo expects mandatory "foo" of "\-foo" argument (undef ok) \& foo=s idem, and argument cannot be undef or reference \& foo=i value of argument \-foo must be an integer \& foo=My::Package foo is a blessed object, inheriting from My::Package \& foo=ARRAY foo is an ARRAY reference .Ve .Sp The rule for determing whether \f(CW\*(C`foo=X\*(C'\fR means \f(CW\*(C`foo\*(C'\fR is a reference \f(CW\*(C`X\*(C'\fR or \f(CW\*(C`foo\*(C'\fR is an object whose class is an heir of \f(CW\*(C`X\*(C'\fR depends on the argument value at runtime: if it is an unblessed ref, strict reference equality is expected. If it is a blessed ref, type conformance is based on inheritance, as you would expect. .Sp Example: .Sp .Vb 4 \& sub f { \& my ($port, $server) = getargs(@_, \& qw(port=i server=HTTP::Server)); \& } .Ve .Sp Some calls: .Sp .Vb 5 \& f(\-server => $server, port => 80); # or \-port, since \- is optional \& f(port => 80, server => $server); \& f(server => $server); # WRONG: missing mandatory \-port \& f(server => 80, port => 80); # WRONG: \-server not an HTTP::Server \& f(server => undef, port => 80); # WRONG: \-server cannot be undef .Ve .Sp By default, named argument processing is case-sensitive but there is an option to ignore case. .IP "getargs \fIarglist\fR, \fIoptions\fR, \fIarray_ref\fR" 4 .IX Item "getargs arglist, options, array_ref" This form specifies that all the formal arguments specified in the \&\fIarray_ref\fR are optional. Think of the '[' and ']' (which you'll probably use to supply the reference as a manifest constant) as syntactic markers for optional things. In the traditional Unix command line description, something like: .Sp .Vb 1 \& cmd [\-a] [\-o file] .Ve .Sp typically denotes that options \f(CW\*(C`\-a\*(C'\fR and \f(CW\*(C`\-o\*(C'\fR are optional, and that \f(CW\*(C`\-o\*(C'\fR takes one argument, a file name. To specify the same things for routine arguments using \fBgetargs()\fR: .Sp .Vb 3 \& sub cmd { \& my ($a, $o) = getargs(@_, [qw(a o=s)]); \& } .Ve .Sp Here however, the \f(CW\*(C`\-a\*(C'\fR argument can be anything: we're not specifying switches, we're specifying \fInamed\fR arguments. Big difference. .Sp Some calls: .Sp .Vb 5 \& cmd(); # OK \& cmd(\-a => undef); # OK \-a accepts anything, even undef \& cmd(\-a => 1, \-o => ".."); # OK \& cmd(\-a => 1, \-o => undef); # WRONG: \-o does not accept undef \& cmd(\-x => 1); # WRONG: \-x is not a known argument name .Ve .Sp It is important to note that there can only be tuples when using named arguments, which means that the routine is called with an \fIeven\fR number of arguments. If you forget a \f(CW\*(C`,\*(C'\fR separator between arguments, \fBgetargs()\fR will complain about an \fIodd\fR number of arguments (provided the resulting code still parses as valid Perl, naturally, or you'll never get a chance to reach the execution of \fBgetargs()\fR anyway). .IP "cgetargs \fIsame args as getargs\fR" 4 .IX Item "cgetargs same args as getargs" The \fBcgetargs()\fR routine behaves exactly as the \fBgetargs()\fR routine: it takes the same arguments, returns the same list. The only difference is that the first time it is called, it builds a routine to process the arguments, and then calls it. .Sp On subsequent calls to \fBcgetargs()\fR for the same routine, the cached argument parsing routine is re-used to analyze the arguments. For frequently called routines, this might be a win, even though Perl still needs to construct the argument list to \fBcgetargs()\fR and call it. .IP "cxgetargs \fIsame args as getargs\fR" 4 .IX Item "cxgetargs same args as getargs" Like \fBcgetargs()\fR, but with extended specifications allowing to specify defaults for non-mandatory arguments. Be careful: those defaults are deep-cloned and \&\*(L"frozen\*(R", so to speak. .SS "Complex Cases" .IX Subsection "Complex Cases" The \fBxgetargs()\fR routine and its cousin \fBcxgetargs()\fR (for the caching version) allow for a more verbose description of named parameters which allows specifying arguments that are mandatory or optional, and also give default values to optional arguments. .IP "xgetargs \fIarglist\fR, \fIoptions\fR, \fIname\fR => \fItype\fR, ..." 4 .IX Item "xgetargs arglist, options, name => type, ..." We'll be ignoring the \fIoptions\fR argument from our discussion. See Options for details. .Sp There can be as many \fIname\fR => \fItype\fR tuples as necessary to describe all the formal arguments of the routine. The \fIname\fR refers to the argument name, and \fItype\fR specifies both the mandatory nature and the expected type. You may use \fIname\fR or \fI\-name\fR to specify an argument called \fIname\fR, and the caller will also be able to spell it as he wishes. The \fItype\fR is encoded as follows: .Sp .Vb 7 \& "i" mandatory integer (scalar) \& "s" mandatory string (scalar) \& "TYPE" mandatory ref of type TYPE, or heir of type TYPE \& undef unspecified type, but mandatory argument \& ["i"] optional integer \& ["s"] optional string \& ["TYPE"] optional ref of type TYPE, or heir of type TYPE .Ve .Sp For optional parameter, an optional second value may be inserted in the list to specify a default value. For instance, the tupple: .Sp .Vb 1 \& \*(Aqy\*(Aq => [\*(AqHASH\*(Aq, { a => 1, b => 2 }] .Ve .Sp specifies an optional named argument \fIy\fR, which is expected to be a \s-1HASH\s0 reference, and whose default value is the hash given. .Sp You may specify an expression as default value instead of giving a manifest constant, but \fB\s-1BEWARE\s0\fR: the \fBcxgetargs()\fR routine will take a snapshot of your expression when building its analyzing routine. It's of no consequence when using a manifest constant, but when using an expression, it will be evaluated \fBonce\fR and the result of that evaluation will be taken as the manifest constant to use subsequently (and this does \fBnot\fR mean the \fBsame\fR reference will be returned, only the same topological structure as the one we evaluated during caching). .Sp Example: .Sp .Vb 6 \& sub f { \& my ($x, $z) = cxgetargs(@_, \& \-x => \*(Aqi\*(Aq, # \-x mandatory integer \& \-z => [\*(Aqn\*(Aq, \-20.4], # \-z optional, defaults to \-20.4 \& ); \& } \& \& f(x => 1, z => {}); # WRONG: z is not a numerical value \& f(z => 1, x => \-2); # OK \& f(\-z => 1); # WRONG: mandatory x is missing \& f(\-z => undef); # WRONG: z cannot be undef .Ve .Sp Remember that we are dealing with named parameters for a routine call, not with option parsing. Therefore, we are always expecting an \fIeven\fR number of arguments, and those arguments are tuples \fIname\fR => \fIvalue\fR. .SS "Options" .IX Subsection "Options" All the \fBgetargs()\fR and \fBxgetargs()\fR routines take an optional hash reference as second argument. Keys in this hash define options that apply locally to the call. In the case of caching routines, e.g. \fBcxgetargs()\fR, the options are only considered the first time, when the analyzing routine is built, and are ignored on subsequent calls. Therefore, it is wise to use manifest constants when specifying options, or use the non-caching function family instead if your options need to be dynamically computed (please, don't do that). .PP Options given there must be spelled out with the leading \f(CW\*(C`\-\*(C'\fR and are case sensitive. To enable an option, give a true value. For instance: .PP .Vb 7 \& sub f { \& my ($x, $z) = cxgetargs(@_, \& { \-strict => 0, \-ignorecase => 1 }, \& \-x => \*(Aqi\*(Aq, # \-x mandatory integer \& \-z => [\*(Aqn\*(Aq, \-20.4], # \-z optional, defaults to \-20.4 \& ); \& } .Ve .PP supplies two options, turning \f(CW\*(C`\-ignorecase\*(C'\fR on and \f(CW\*(C`\-strict\*(C'\fR off. .PP The available options are, in alphabetical order: .IP "\-extra" 4 .IX Item "-extra" Whether to report extra unknown arguments at the end of the argument list. Example: .Sp .Vb 2 \& my ($x, $y, @extra) = getargs(@_, \& { \-extra => 1, \-strict => 0 }, qw(x y)); .Ve .Sp Your setting is forced to false when \f(CW\*(C`\-strict\*(C'\fR is true. The default value is the negation of the boolean \f(CW\*(C`\-strict\*(C'\fR setting, which means the above can be rewritten as: .Sp .Vb 1 \& my ($x, $y, @extra) = getargs(@_, { \-strict => 0 }, qw(x y)); .Ve .Sp which will implicitely set \-extra to be true. This is usually what you want when not strict, i.e. get at the other parameters. Assuming we were writing the above for a function f(), calling: .Sp .Vb 1 \& f(\-x => 1, \-y => 2, \-other => 5); .Ve .Sp would set: .Sp .Vb 1 \& @extra = (\-other => 5); .Ve .Sp An alternative when you are not strict is to make use of the \f(CW\*(C`\-inplace\*(C'\fR option to edit \f(CW@_\fR inplace. .IP "\-ignorecase" 4 .IX Item "-ignorecase" Turn case-insensitive named parameters. False by default. Actually, if not explicitely specified, the default setting depends on the way \&\f(CW\*(C`Getargs::Long\*(C'\fR was imported within the package scope. If you said: .Sp .Vb 1 \& use Getargs::Long; .Ve .Sp then the default is indeed to be case-sensitive. However, if you said: .Sp .Vb 1 \& use Getargs::Long qw(ignorecase); .Ve .Sp then the default for the package scope is to be case-insensitive. You may still specify the \f(CW\*(C`\-ignorecase\*(C'\fR option to force case sensitivity on a per-routine basis, although I would never do such a thing and stick to a uniform case sensitivity on a package basis. .IP "\-inplace" 4 .IX Item "-inplace" Whether to edit the routine's argument list inplace, removing processed arguments as they are found and leaving unprocessed ones. False by default. .Sp Your setting is forced to false when \f(CW\*(C`\-strict\*(C'\fR is true, naturally, since an unknown argument is an error. .IP "\-strict" 4 .IX Item "-strict" Whether unknown named parameters are fatal. True by default. When \f(CW\*(C`\-strict\*(C'\fR is true, the \f(CW\*(C`\-inplace\*(C'\fR and \f(CW\*(C`\-extra\*(C'\fR options you may specify are ignored and forced to false. .SH "BUGS" .IX Header "BUGS" Currently, types 'i', 'n' and 's' all mean the same thing, but that will change. Don't take the current implementation's deficiency as an excuse for lamely specifying your scalar types. .PP You must be careful in this implementation to list options and variables in the very same order. Some day, I will probably add another routine to take arguments the way \f(CW\*(C`Getopt::Long\*(C'\fR does to cope with this ordering problem (but it forces to spell out variables twice \*(-- once for declaration, and once for specifying a pointer to it). .SH "RELATED MODULE" .IX Header "RELATED MODULE" See Params::Validate for another take at parameter validation. It is a completely independant module, developped by Dave Rolsky, which may also interest you. Its interface and purpose are different though. .SH "LICENSE" .IX Header "LICENSE" This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See the file \s-1LICENSE\s0 in the distribution for details. .SH "AUTHOR" .IX Header "AUTHOR" The original code (written before September 15, 2004) was written by Raphael Manfredi . .PP Maintenance of this module is now being done by David Coppit . .SH "SEE ALSO" .IX Header "SEE ALSO" Log::Agent, Params::Validate