I often have classes that abstract out the work of fetching data from
multiple sources -- so I have attributes that hold instances of the objects
that do the actual work.   Each of these objects need their own config.

I've used three approaches to pass config to the contained objects, but
each seems a bit cumbersome.  Just wondering if there's other ideas.


One is to have a HashRef attribute in the "parent" that just gets passed to
the "child" object's constructor.   This doesn't work so good if the class
is turned into a command line tool.

I've also use a configuration class and pass an instance of that from the
parent to the child.   Essentially the same as the hashref approach.

And I have used a configuration role consumed by both parent and child
objects and just manually copy the attributes from the parent to the child
constructor.   This is handy for classes that get turned into command line
tools because the configuration of the sub/child objects is flattened.

fetch_widget --widget_server=widgets.example.com --price_server=
prices.example.com --id 1234

Another common class I seem to have is one that maintains a list of "child"
objects.  For example, I might pass in a path to a directory and the class
creates one object for each file in the directory.

package Directory:
use Moose;

has directory => ( ... );
has debug => ( isa => 'Bool', ... );
has file_list => ( traits => ['Array'], lazy_build => 1, ... );

sub _build_file_list {
     my $self = shift;
     return [ map { $self->file_class->new( { debug => $self->debug, path
=> $_ } ) } $self->files_in_dir ];
}

Not problem copying a few attributes like that, but some of my real child
objects have much more config to pass around.

Other more clever ideas?


-- 
Bill Moseley
mose...@hank.org

Reply via email to