Odd inheritance behaviour

TheNomad on 2006-06-29T13:56:38

I've got an application where I have an abstract class A, B inherits from A, and C inherits from B. Class P has-a C.

Class A uses 'base' Class::Accessor::Fast to create accessors. All classes A, B and C do __PACKAGE__->mk_accessors, to make their accessors.

The test suite ran fine over classes A, B and C. However, when I came to run the tests for P, perl would complain 'can't find package method mk_accessors in class C'.

If I added 'use base 'Class::Accessor' to C, the tests would run fine. But this violated the principle that C should only inherit from B.

I guessed that somehow at compile-time perl wasn't running all the way up the class hierarchy when importing package C. I figured that delaying the import until run-time would fix this problem.

I changed 'use C' to 'require C' and the problem was solved, with the tests for class P running fine.

But the question remains, why is perl (5.8.8) not running all the way up the class hierarchy when it imports classes? Or is it something to do with the implementation of Class::Accessor::Fast?


Hard to say...

phaylon on 2006-06-29T14:12:06

...without seeing what you've actually done. Can you reproduce it with a small set of code and show that?

Re:Hard to say...

TheNomad on 2006-06-30T07:12:51

Sure...Each of these are in a seperate file:

package A;
use base 'Class::Accessor::Fast';
__PACKAGE__->use_best_practice;

package B;
use base 'A';
__PACKAGE__->mk_accessors(qw/foo/);

package C;
use base 'B';
__PACKAGE__->mk_accessors(qw/bar/);

package P;

use C;

In the test suite:

use_ok('C') .... ok

In a seperate test file:

use_ok('P');

Fails 'Can't find package method mk_accessors via package 'C'.

On the other hand if I change P from 'use P' to 'require P' it works.