http://qs321.pair.com?node_id=251903


in reply to Re: Re: strange error when subclassing packages
in thread problem using Class::Factory

use base qw(xxx); is equivalent to require xxx; our @ISA=qw(xxx);. Since base requires the module and doesn't use it, the base module's import method is never called. Therefore, no symbols are imported from the base class to the deriving class's namespace.

If I recall correctly, perl only follows derivation chains to locate a sub to satisfy object methods. If you were using __PACKAGE__->whatever(), perl would not look for whatever() in base classes since that syntax specifies a class method, not an object method. Perl would only look for it in the current package. Since the base module was required and not used, no import was performed and the symbol doesn't exist in the current module.

When you remove the use base and replace it with use followed by setting @ISA yourself, the fact that you've used the module gets the routine into your packages namespace and everything works.

90% of every Perl application is already written.
dragonchild

Replies are listed 'Best First'.
Re^4: strange error when subclassing packages
by adrianh (Chancellor) on Apr 21, 2003 at 09:33 UTC
    use base qw(xxx); is equivalent to require xxx; our @ISA=qw(xxx);.

    Although you would be somewhat challanged to discover this from the documentation this isn't entirely true. It isn't exactly equivalent since base explicitly ignores errors from require that indicate the module cannot be loaded. This is handy since it allows you to write things like this:

    { package Foo; # ... some methods ... } { package Bar; use base qw(Foo); };

    where no modules are involved at all. Take a look at the source for details.

    As Aristotle points out the lack of an import isn't the cause of the problem. Class::Factory works perfectly well with use base (at least on my box :-)

Re^4: strange error when subclassing packages (not use vs require)
by Aristotle (Chancellor) on Apr 21, 2003 at 02:50 UTC

    You are wrong. Perl will search up the inheritance tree for class methods. The only time it will only look in a single package is using the syntax $foo->Bar::baz() or Foo->Bar::baz(), which will pass a $self of $foo or "Foo", respectively, but invoke the method in the Bar package.

    You are also wrong about import(); it is the entire point of OO not to have to sully someone else's namespace with your own symbols.

    You would also have easily found out that your assumption is wrong in this case if you had cared to look at the Class::Factory source - no import() function is defined nor is any exporter used.

    Makeshifts last the longest.

      The only time it will only look in a single package is using the syntax $foo->Bar::baz() or Foo->Bar::baz(), which will pass a $self of $foo or "Foo", respectively, but invoke the method in the Bar package.

      Ok, I see where I had it wrong. Derived::foo() would not find Base::foo() but Derived->foo() would.

      You are also wrong about import(); it is the entire point of OO not to have to sully someone else's namespace with your own symbols.

      I understand that completely. I was just pointing out that the manner in which the routine was being invoked might not work without the import. Unfortunately, I was wrong. Sorry to muddy the waters.

      90% of every Perl application is already written.
      dragonchild