Volks,

am I just slow or what?

I liked david's illustration yesterday, and dashed off
this morning to code up the illustration about overridden
method - on the specific order of Connie's original concern,
so I had three packages A, B1, C - { base is to the left }.

So B1's overriding of A's return_hash() method did the
and C's overriding of B1 worked smothely with a variation
on the basic:

  sub return_hash {
        
        my ($self) = shift;
        
        my $hash = $self->SUPER::return_hash();
        
        $hash->{B} = 'B2';

        return($hash);
  }

but when I went to make the method in B1 that C would
inherit to display the inheritence chain, I had thought
I could likewise get away with the 'SUPER' trick  a la

  sub parent_chain {
        my ($me) = @_;

     my @generations = ref($me);

     for my $parent ( @{$me->child_of()} ) {
                #
                # check that my 'SUPER' can parent_chain()
                #
        if ( my $sub_ref = $me->SUPER::can('parent_chain')) {
                        # this is line #83
                my $grand_parents = $me->SUPER::parent_chain();
                push(@generations, $_) for (@$grand_parents);
        } else {
                push(@generations, $parent);
        }
     }

     return(\@generations);
  }

only to get the error message on the order of

        File "B1.pm"; Line 84:
                Can't locate object method "parent_chain" via package "B1"

and then it HIT ME!

Smack! - and this is where I need some help making sure
that my 'explanaition' matches the 'real answer'...

package C inherited that method from B1, B1 did not over-ride that
method - so

        a) the fact that the $me there is a blessed reference
                to the Class C

        b) there exists no over-ridden method in the super-class
                of B1 that can be called - and that $sub_ref - which
                is the address of this method ... while 'true' in the
                sense that it is NOT an undefined value - did not majikally
                "jump" where I had mistakenly thought it should...

So i re-wrote that as:

  sub parent_chain {
        my ($me) = @_;
        
     my @generations = ref($me);

     for my $parent ( @{$me->child_of()} ) {
        
        if  ( UNIVERSAL::can($parent, 'parent_chain' )) {
                push (@generations, $_ )
                        for @{$parent->new->parent_chain()} ;
        } else {
                        push(@generations, $parent);
                }
     }

     return(\@generations);

  } # end of parent_chain

Which expressly asks IF my Parent Class can do that method,
then we do the usual autonomous object method...

so that the simple demo code

        #!/usr/bin/perl -w
        use strict;
        use C ; # we find it because @INC includes "."
        
        my $c = new C;
        my $family_tree = $c->parent_chain();

        print "Iam - ", shift @$family_tree ," :\n  ";
        while ( my $Iam = shift @$family_tree) {
                print "Which is a Child of - $Iam :\n  ";
        }

happily generates:

        Iam - C :
                Which is a Child of - B1 :
                Which is a Child of - A :


Hence one should only use SUPER in a method if

        a) the method itself is overriding an inherited method
        b) you have a clue that in multiple class inheritences
                you will find the one you really want?



ciao
drieux

---


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to