
Class::ParamParser - Provides complex parameter list parsing

5.004
I<none>
I<none>

use Class::ParamParser;
@ISA = qw( Class::ParamParser );
sub textfield {
my $self = shift( @_ );
my $rh_params = $self->params_to_hash( \@_, 0,
[ 'name', 'value', 'size', 'maxlength' ],
{ 'default' => 'value' } );
$rh_params->{'type'} = 'text';
return( $self->make_html_tag( 'input', $rh_params ) );
}
sub textarea {
my $self = shift( @_ );
my $rh_params = $self->params_to_hash( \@_, 0,
[ 'name', 'text', 'rows', 'cols' ], { 'default' => 'text',
'value' => 'text', 'columns' => 'cols' }, 'text', 1 );
my $ra_text = delete( $rh_params->{'text'} );
return( $self->make_html_tag( 'textarea', $rh_params, $ra_text ) );
}
sub AUTOLOAD {
my $self = shift( @_ );
my $rh_params = $self->params_to_hash( \@_, 0, 'text', {}, 'text' );
my $ra_text = delete( $rh_params->{'text'} );
$AUTOLOAD =~ m/([^:]*)$/;
my $tag_name = $1;
return( $self->make_html_tag( $tag_name, $rh_params, $ra_text ) );
}
sub property {
my $self = shift( @_ );
my ($key,$new_value) = $self->params_to_array(\@_,1,['key','value']);
if( defined( $new_value ) ) {
$self->{$key} = $new_value;
}
return( $self->{$key} );
}
sub make_html_tag {
my $self = shift( @_ );
my ($tag_name, $rh_params, $ra_text) =
$self->params_to_array( \@_, 1,
[ 'tag', 'params', 'text' ],
{ 'name' => 'tag', 'param' => 'params' } );
ref($rh_params) eq 'HASH' or $rh_params = {};
ref($ra_text) eq 'ARRAY' or $ra_text = [$ra_text];
return( join( '',
"<$tag_name",
(map { " $_=\"$rh_params->{$_}\"" } keys %{$rh_params}),
">",
@{$ra_text},
"</$tagname>",
) );
}

This Perl 5 object class implements two methods which inherited classes can use to tidy up parameter lists for their own methods and functions. The two methods differ in that one returns a HASH ref containing named parameters and the other returns an ARRAY ref containing positional parameters.
Both methods can process the same kind of input parameter formats:
Those examples included single or multiple positional parameters, single or multiple named parameters, and a HASH ref containing named parameters (with optional "remaining" values afterwards). That list of input variations is not exhaustive. Named parameters can either be prefixed with "-" or left natural.
We assume that the parameters are named when either they come as a HASH ref or the first parameter begins with a "-". We assume that they are positional if there is an odd number of them. Otherwise we are in doubt and rely on an optional argument to the tidying method that tells us which to guess by default.
We assume that any "value" may be an array ref (aka "multiple" values under the same name) and hence we don't do anything special with them, passing them as is. The only exception to this is with "remaining" values; if there is more than one of them and the first isn't an array ref, then they are all put in an array ref.
If the source and destination are both positional, then they are identical.

This class does not export any functions or methods, so you need to call them using object notation. This means using Class->function() for functions and $object->method() for methods. If you are inheriting this class for your own modules, then that often means something like $self->method(). Note that this class doesn't have any properties of its own.

See below for argument descriptions.
See below for argument descriptions.

The arguments for the above methods are the same, so they are discussed together here:

Copyright (c) 1999-2003, Darren R. Duncan. All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. However, I do request that this copyright information and credits remain attached to the file. If you modify this module and redistribute a changed version then please attach a note listing the modifications. This module is available "as-is" and the author can not be held accountable for any problems resulting from its use.
I am always interested in knowing how my work helps others, so if you put this module to use in any of your own products or services then I would appreciate (but not require) it if you send me the website url for said product or service, so I know who you are. Also, if you make non-proprietary changes to the module because it doesn't work the way you need, and you are willing to make these freely available, then please send me a copy so that I can roll desirable changes into the main release.
Address comments, suggestions, and bug reports to perl@DarrenDuncan.net.

Thanks to Laurie Shammel <lshammel@imt.net> for alerting me to some warnings that occur when converting a positional SOURCE to named where SOURCE has more array elements than NAMES. While the correct result was returned all along, warnings can be annoying in this context.

perl(1), HTML::FormTemplate, Class::ParmList, Class::NamedParms, Getargs::Long, Params::Validate, CGI.