Object-InsideOut - Readonly object from hash? Less verbose definition?

Posted on Mon Oct 9 14:57:04 2006 by nikc
Readonly object from hash? Less verbose definition?

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.

Direct Responses: 3235 | Write a response