in reply to Re: Inheritance automation help
in thread Inheritance automation help

You have just enlightened me! I spend a few hours chewing through your example, and researching the why and how in the context I'm using it. I have my code switched over to an iteration of this, and I'm just trying to work out one last kink of it. What would I need to do to use the above examples packages with a main that looks like this?
package main; my $test = Top->new(passed1=>'this is shared'); my $one = Top::Extend1->new(); my $two = Top::Extend2->new(); $test->doPrint(); $one->doPrint(); $two->doPrint(); print "Extra test: $test->{passed1}\n"; print "Extra one: $one->{passed1}\n"; print "Extra two: $two->{passed1}\n";
And have the result say "this is shared" for each Extra Test. I think with that nugget of knowledge I should be able to tie the rest of my questions up on my own. Thanks again for the great response!

Replies are listed 'Best First'.
Re^3: Inheritance automation help
by GrandFather (Saint) on Apr 05, 2012 at 04:59 UTC

    You can't. At least, what you are asking for is not what class inheritance does. It is some sort of run time information sharing and there are lots of ways of doing that depending on what you actually want to achieve. It maybe that a clonish constructor does what you want (but probably not, and there are nasty gotchas):

    #!/usr/bin/perl use strict; use warnings; package Top; sub new { my ($class, %params) = @_; my $self = bless \%params, $class; $self->{var1} = 'Top set var1'; $self->{var3} = 'Top set var3'; return $self; } sub clonish { my ($class, $other, %params) = @_; return bless {%$other, %params}, $class; } sub doPrint { my ($self) = @_; my $type = ref $self; print "Test ($type): $_ => $self->{$_}\n" for keys %$self; } package Top::Extend1; push @Top::Extend1::ISA, 'Top'; sub doPrint { my ($self) = @_; my $type = ref $self; print "Override ($type): $_ => $self->{$_}\n" for keys %$self; } package Top::Extend2; push @Top::Extend2::ISA, 'Top'; sub new { my ($class, %params) = @_; my $self = $class->SUPER::new(%params); $self->{var1} = 'Top::Extend2 set var1'; return $self; } package main; my $test = Top->new(passed1 => 'this is copied'); my $one = Top::Extend1->clonish($test); my $two = Top::Extend2->clonish($test, something => 'extra'); $test->doPrint(); $one->doPrint(); $two->doPrint();

    Prints:

    Test (Top): var3 => Top set var3 Test (Top): var1 => Top set var1 Test (Top): passed1 => this is copied Override (Top::Extend1): var3 => Top set var3 Override (Top::Extend1): var1 => Top set var1 Override (Top::Extend1): passed1 => this is copied Test (Top::Extend2): var3 => Top set var3 Test (Top::Extend2): var1 => Top set var1 Test (Top::Extend2): passed1 => this is copied Test (Top::Extend2): something => extra

    If you tell us more about the problem domain we may be able to offer a better solution. Quite likely you are trying to use the wrong tool for the problem at hand and the monk's wider experience may be able to give a better approach.

    True laziness is hard work
Re^3: Inheritance automation help
by tangent (Parson) on Apr 05, 2012 at 09:10 UTC
    have the result say "this is shared" for each Extra Test
    I'm not quite sure what you require but, adding to Grandfather's first code example, this may help:
    package Top; # ... sub shared { my ($self) = @_; my $type = ref $self; print "Test ($type): This is shared\n\n"; } # ... package main; # ... print "Extra test:\n"; $test->shared(); print "Extra one:\n"; $one->shared(); print "Extra two:\n"; $two->shared();
    This will print:
    Extra test: Test (Top): This is shared Extra one: Test (Top::Extend1): This is shared Extra two: Test (Top::Extend2): This is shared