gwg has asked for the wisdom of the Perl Monks concerning the following question:
Greeting monks,
I'm using Moose for the first time, and would appreciate some expert advice on some issues I'm running into.
I have a set of attributes with custom accessors, methods, etc.. I would like to group them in a single hash structure so that I can set/get/clear them globally (and keep my object nice and neat). I think I could do this by creating a new class with these various attributes in it, but I don't want to do that for a number of reasons. I'm stuck on how to move these attributes into a single place.
To illustrate, the object code currently looks like this:
package My::New::Object; has max_chunk => (is=>'rw', isa=>'Int', init_arg => 'size', clearer=>' +clear_max_chunk'); has liberal_mode => (is=>'rw', isa=>'Bool', default=>sub{1}); has fh => (is=>'rw', isa=>'My::New::Object::ProtoFileHandle', clearer= +>'clear_fh', predicate=>'has_fh', writer=>'_set_fh', coerce => 1, ini +t_arg => 'file', trigger => \&reset_temporary_variables ); [... more attributes, code, etc. ]
And dumping it gives this:
object: $VAR1 = bless( { 'liberal_mode' => 1, 'fh' => bless( \*Symbol::GEN0, 'FileHandle' ), [... other data ...] }, 'My::New::Object' );
I would like it to look like this:
object: $VAR1 = bless( { 'options' => { 'liberal_mode' => 1, 'fh' => bless( \*Symbol::GEN0, 'FileHandle' ), }, [... other data ...] }, 'My::New::Object' );
so that I could have methods like 'set_all_options' and 'clear_options' to deal with everything in the options hash, as well as preserving the various methods I already have associated with the existing attributes.
Any wisdom would be greatly appreciated! Thanks.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Moose question
by stvn (Monsignor) on Aug 28, 2009 at 20:11 UTC | |
by gwg (Beadle) on Aug 28, 2009 at 22:52 UTC | |
|
Re: Moose question
by james2vegas (Chaplain) on Aug 29, 2009 at 10:31 UTC |