I'm writing the specification for some objects that will be initialised by a hash, and thereafter treated as read-only. The definition for this is proving to be quite verbose. For example, consider:
my $obj = My::Obj->new({ foo => 1, bar => 2 });
print $obj->get_foo();
To support that I have to write:
my @foo :Field
:Arg(Mandatory => 1, Name => 'Foo')
:Get(get_foo);
my @bar :Field
:Arg(Mandatory => 1, Name => 'Bar')
:Get(get_bar);
That's OK for two fields, but gets tedious for 15 (I'm turning received XML in to a hash (via SOAP) and thence to an object).
I can almost do this by programmatically building up %InitArgs
foreach my $field (qw(foo bar)) {
$init_args{$field} = {
Mandatory => 1,
Param => ucfirst($field),
}
}
But that doesn't handle accessor generation.
|