I have a project I'm working on that has plug in style modules and I'm unhappy with the way I'm extending my base class. Was wondering if I could get an opinion of a more standard way to do this that doesn't feel like I'm doing it wrong. I threw in a couple of curve balls into the mix to show that I'm changing things on each package from the base class, and exporting methods also. Sorry for its length, but needed to make it long to get the full example together:
#!/usr/bin/perl package TOP::TEST; sub new { my $class = shift; my $self = {@_}; $self->{'thing'}->{'var_1'} = 'this changed'; $self->{'thing'}->{'var_3'} = 'other thing'; bless $self, $class; return $self; } package TOP::TEST::EXTENDONE; sub new { my $class = shift; my $self = {@_}; $self->{'thing'}->{'var_1'} = 'this changed'; $self->{'thing'}->{'var_3'} = 'other thing'; bless $self, $class; return $self; } sub print_one { my ($self) = @_; return 'FROM ONE: '.$self->{'thing'}->{'var_1'}; } package TOP::TEST::EXTENDTWO; sub new { my $class = shift; my $self = {@_}; $self->{'thing'}->{'var_3'} = 'other thing changed'; bless $self, $class; return $self; } package main; #### For this example added them to this file #use TOP::TEST; #use TOP::TEST::EXTENDONE; #use TOP::TEST::EXTENDTWO; # at any point there might be an EXTENDTHREE, or EXTENDFOUR when neede +d # # this how it works now. But feels wrong for some reason my $thing = new TOP::TEST( var_1 => 'this', var_2 => 'that'); my $one = new TOP::TEST::EXTENDONE( thing=> $thing,something=> 'extra' + ); my $two = new TOP::TEST::EXTENDTWO( thing=> $thing ); # this would be ideal with automatic inheritence in the packages... bu +t # can seem to get it done correctly. # Need some wisdom :( #my $thing = new TOP::TEST( var_1 => 'this', var_2 => 'that', somethin +g=>'extra'); print 'var_1 changed: '.$thing->{'var_1'}."\n"; print 'var_2: '.$thing->{'var_2'}."\n"; print 'var_3: '.$thing->{'var_3'}."\n"; print 'var_1 from $one: '.$one->print_one()."\n";
At any point I want to be able to write another EXTENDMETHOD and have it alter just "work". Because of my environment requirements I can't use MOOSE, but everything is OOP. Having everything just magically slide together would be ideal, am I missing a base class in between or something - or something else? The ideal scenerio being:
use TOP::TEST; use TOP::TEST::EXTENDONE; use TOP::TEST::EXTENDTWO; my $thing = new TOP::TEST( var_1 => 'this', var_2 => 'that', something +=>'extra'); # This is imported into the base class from EXTENDONE print $thing->print_one()."\n"; print $thing->{'var_3'}."\n";
Instead of daisy chaining them together passing the base class as a parameter of the new() for each extended class. Cheers to a wise helping hand!

In reply to Inheritance automation help by natelewis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.