Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Creating dynamic parent/child relationships

by Corion (Patriarch)
on Sep 03, 2019 at 09:17 UTC ( [id://11105507]=note: print w/replies, xml ) Need Help??


in reply to Creating dynamic parent/child relationships

Don't use inheritance for this.

Consider having an array of "modificators" or "plugins" (for lack of detail) that the main class calls:

my $foo = One->new_from_config('myconfig.yml'); print Dumper $foo->frobnicate($bar); sub One::frobnicate( $self, $item ) { for my $step (@{ $self->steps }) { $item = $step->frobnicate( $item ); }; return $item }

The One::frobnicate method dispatches the call to all the listed ->frobnicate methods in ->steps. The ->steps array is filled with classes (or instances) from the configuration.

See also Module::Pluggable, which can load plugins.

Replies are listed 'Best First'.
Re^2: Creating dynamic parent/child relationships
by nysus (Parson) on Sep 03, 2019 at 17:14 UTC

    Ok, I'm having trouble wrapping my head around your example. And I'm not sure if things are complicated by my particular situation because I rely on SUPER calls. Here's a simplified representation of the classes that have the relationships hard coded:

    package Base ; sub new { my $class = shift; bless {}, $class; } sub do { my $s = shift; print "hi Base here\n"; } package One ; use parent 'Base'; sub do { my $s = shift; $s->SUPER::do; print "hi from pkg one\n"; } package Two ; use parent 'One'; sub do { my $s = shift; $s->SUPER::do; print "hi from pkg two\n"; } package Three ; use parent 'Two'; sub do { my $s = shift; $s->SUPER::do; print "hi from pkg Three\n"; }

    And then in a script:

    my $three = Three->new; $three->do;
    This, of course, outputs:
    hi Base here hi from pkg one hi from pkg two hi from pkg Three

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

Re^2: Creating dynamic parent/child relationships
by nysus (Parson) on Sep 03, 2019 at 17:34 UTC

    OK, I think I see what you are saying. So essentially, my Base class does the following when the "do" method is called on it:

    package Base; use One; use Two; use Three; sub do { print "hi from Base\n"; Three::do; Two::do; One::do; }

    And my config file would determine which order to run the subroutines from each of the packages. Is that right?

    UPDATE: Here is a version of the code above that is a bit more dynamic:

    package Base ; use One; use Two; use Three; sub new { my $class = shift; bless { steps => [ qw(Three Two One) ] }, $class; } sub do { my $s = shift; print "hi from Base\n"; for my $step ( @{ $s->{steps} } ) { $step->do; } }

    So I think I got it from here. I just have to dynamically import the correct modules and populate the steps attribute with the config file during construction. Nice trick. Thanks!

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

      Just a quick follow up to show a crude way of getting this done by passing the order of the classes into the base class:

      package Base ; sub new { my $class = shift; for my $mod (@_) { eval "require $mod"; } bless { steps => \@_ }, $class; } sub do { my $s = shift; print "hi from Base\n"; for my $step ( @{ $s->{steps} } ) { $step->do; } }

      Then call construct new from a script:

      my $base = Base->new( qw ( One Three Two ) ); $base->do;

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
      $nysus = $PM . ' ' . $MCF;
      Click here if you love Perl Monks

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11105507]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (1)
As of 2024-04-24 13:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found