Subject: | Documentation / Feature Issue in Class::MethodMaker |
Hello!
I like your module. I found out about it in the Damian Conway OO Perl book. I'd like to suggest something, though, about the way it operates. There's no way to default a list of values in easily; at least I couldn't figure it out. The documentation doesn't include an example of how to do this. I'm wondering if there actually is an easy way to do this.
I want to be able to specify the default values for the class in the class itself, and override those values in with the call to new with a hash. Pretty straightforward, methinks.
Below is code that I found works to do this task. Perhaps you could include this code in your readme file under 'EXAMPLES'. Or, you could figure out how to make this easier for us all and add that as a feature. If the feature to do this easily already exists, please document exactly how to do it???? Please???
Thanks,
Cordially yours,
-- Kevin Rice
CPAN ID: KEVINRICE
kevin@justanyone.com
Code Follows:
package Simple;
use Class::MethodMaker
get_set => [qw(a b)],
new_with_init => 'new';
sub init {
my $self = shift;
my %in_args = @_;
my %default_args = ( b => "default");
my %final_args = (%default_args, %in_args);
foreach my $thiskey (keys(%final_args)) {
$self->{$thiskey} = $final_args{$thiskey};
}
return;
}
-------------- in use_it.pl ---------
use Simple;
my $test = new Simple (a=>1);
# now a==1, b==default.
my $test = new Simple (a=>1, b=>2);
# now a==1, b==2.