in reply to __PACKAGE__ in subclass

I think you've got something wrong about that subclassing. The superclass doesn't know its subclasses, only the subclass knows its superclasses as they are in @ISA. The base pragma only puts a class into @ISA.

Now, whenever a method in the subclass is called and not found there, it is searched for in all classes that stand in @ISA of that package:

package Superclass; sub foo { print "Superclass::foo( @_ )\n" } sub bar { print "Superclass::bar( @_ )\n" } package Subclass; use base 'Superclass'; sub bar { print "Subclass::bar( @_ )\n" } package main; Subclass->foo; #Superclass::foo( Subclass ) Subclass->bar; #Subclass::bar( Subclass )

If you make Class::DBI create a 'sessions()' method for "Super::Person" and "Sub::Person" has "Super::Person" in its @ISA, you can be sure that perl will look for 'sessions()' in "Super::Person" if it doesn't find it in "Sub::Person", but the first parameter passed to 'sessions' will be "Sub::Person". Just get rid of that __PACKAGE__, it only makes you unflexible.

--
http://fruiture.de

Replies are listed 'Best First'.
Re: Re: __PACKAGE__ in subclass
by thpfft (Chaplain) on Oct 20, 2002 at 19:58 UTC

    Reading your reply, I realise that I missed out a key point. Very sorry. I was writing to the poop group too and assumed too much immersion in Class::DBI.

    __PACKAGE__->has_many( 'sessions', 'Super::Session', 'person' );

    The has_many() class method here creates two object methods: a sessions() method in this class, which returns a list of session objects, and a person() method in Super::Session, which returns a single person object. It's just a way of representing a one to many link.

    As you say, everything is fine in the class and subclass presented here: you get what you ask for according to where you ask for it.

    Where it breaks down is in the method created in the foreign class. It thinks it should return an object of Super::Person, since that was the package from which it was created, but in fact I want an object of Sub::Person, since that was the subclass which caused - but did not make - the call to has_many. grrr.

    I'm beginning to think that Class::DBI's inheritance is just broken here. Or, as you say, my thinking is broken here. Guess I need to find another way.

      I'm unsure as to what you're trying to do, but maybe the caller function would help you?

      Makeshifts last the longest.