On Thu, 23 Feb 2012, Bill Moseley wrote:

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?

I've thought about this a little. Building something like Class::Container (https://metacpan.org/release/Class-Container) would be way, way easier with Moose.

You could do it as a parameterized role, maybe:

  with 'Role::Container' =>
      { contains => { bar => 'Foo::Bar', baz => 'Baz::Object' } );

This would introspect the relevant classes and either add attributes or some sort of BUILD or BUILDARGS wrapper to record the relevant parameters and save them for later.


-dave

/*============================================================
http://VegGuide.org               http://blog.urth.org
Your guide to all that's veg      House Absolute(ly Pointless)
============================================================*/

Reply via email to