in reply to Re^3: Method of child module called by parent
in thread Method of child module called by parent
Not the OP, but, Why is this backwards? This is actually a design pattern that I use somewhat regularly with Moose. Or the Correlary, Moose makes this design pattern extremely easy to implement with Delegation (and it's more advanced cousin Currying)
An example is my Build::VM library I'm working on: https://github.com/three18ti/Build-VM/blob/master/lib/Build/VM.pm
In my main class Build::VM, I have (stripped down):
package Build::VM; use 5.010; use Moose; use strict; use warnings; use Build::VM::Guest; has guest => ( isa => 'Build::VM::Guest', lazy => 1, default => sub { Build::VM::Guest->new( name => $_[0]->guest_name, memory => $_[0]->to_kib($_[0]->guest_memory), disk_list => $_[0]->disk_list, cdrom_list => $_[0]->cdrom_list || [[]], ); }, handles => { get_memory => 'memory', }, );
Then in my driver script that calls the module, I do:
#!/usr/bin/perl use 5.010; use strict; use warnings; use Build::VM; # Get the parameters from the CLI or config file my $bvm = Build::VM->new( name => $name, memory => $memory, disk_list => $disk_list, cdrom_list => $cdrom_list ); say $bvm->get_memory();
(ok, bad example because cdrom_list is the only one that is using delegation, technically ->memory is a method in the Build::VM::Guest object, it's just not a user defined function so it's a little more ambiguous).
Unless I'm misunderstanding what OP wants to do, why is this backwards or inside out? And if it's so backwards, why does Moose make it so easy? If you don't mind, what would be the appropriate organization for my various modules?
Also, when talking about Perl and inheritance, (which is what I think of when I hear "child class") I'm usually pointed to Moose Roles because they make multiple inheritance trivial (because it's not really inheritance, it's more like Ruby's mixins).
Thanks, I always like posts like this, even though it's not something I thought I was doing wrong, I like to learn when I am :)
-three18ti
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Method of child module called by parent
by Athanasius (Archbishop) on Jan 01, 2015 at 04:06 UTC | |
by three18ti (Monk) on Jan 02, 2015 at 16:49 UTC |