in reply to a simple matter of elegance
Unless you absolutely positively want exactly that internal data structure, you can solve the general problem with speed and elegance by using one of the Class::Accessor modules.
That gives you a mature, extensible and (in my opinion) well-designed solution to the problem and others like it in your codebase.
Of course, it's possible that this isn't dynamic enough for you, and that you already know Class::Accessor like the back of your hand and the CTO insists that the API is frozen and so on. But if not, I'd really recommend you use Class::Accessor (or Class::Accessor::Fast or Class::Accessor::Faster). I bet you could make a factory on top of that if you really need one.
Anyhow, here's a simple example of that module in action:
package Foo; use strict; use warnings; use base qw(Class::Accessor); __PACKAGE__->mk_accessors(qw(spiro london)); 1;
Meanwhile, in a nearby t/*.t:
my $foo = Foo->new( { spiro => 'Agnew', london => 'England' } ); isa_ok( $foo, 'Foo' ); is( $foo->spiro, 'Agnew', "spiro" ); is( $foo->london, 'England', "london" ); ok( $foo->spiro('Not really Agnew'), "set spiro" ); ok( $foo->london('Ontario'), "set london" ); is( $foo->spiro, 'Not really Agnew', "spiro" ); is( $foo->london, 'Ontario', "london" );
|
|---|