shoez has asked for the wisdom of the Perl Monks concerning the following question:

Hello all, I've got a bit of a strange error that keeps popping up, involving the subclassing of various packages. I was reading this response by the venerable Ovid pretty recently, in which he explains the an OO concept called a factory. Anyway, great idea :) So I went off to CPAN to see if there was already a module built, and low and behold Class::Factory pops up. I've been struggling to get it working however. I'm wondering if anyone could spare a few minutes to point out my problem.
In one package, the central one, I have this code..

package pkg1; use strict; use factory; etc, etc...

In the package called factory, I have the following.

package factory; use strict; use base qw(Class::Factory); __PACKAGE__->add_factory_type(hello=>'speak::hello'); 1;

Now that all seems fine to me. The package speak::hello should be preloaded using require, by Class::Factory, and I should be able to call it at a later time. Herein lies my issue. I hacked together a little test script, which contained just these lines.

#!d:/perl use lib 'c:/perlcode'; use pkg1;

Sadly it didn't work, as Perl seemingly wanted to give me the error you see below. I must be doing something hideously wrong, but I can't seem to work out what/why. Please help, I'm tearing my hair out :) Oh, and I don't know if it's worth mentioning, but I'm using the latest ActiveState Perl distribution (5.8).

Can't locate object method "add_factory_type" via package "factory" at d:/site/ line 4.

with respect -shoez

2003-04-20 edit ybiC: retitle from "strange error when subclassing packages"

Replies are listed 'Best First'.
Re: strange error when subclassing packages
by adrianh (Chancellor) on Apr 20, 2003 at 21:56 UTC

    Just checking the most obvious cause - are you sure that you have the Class::Factory module installed? Since base doesn't die if the module cannot be located, does changing

    use base qw(Class::Factory);


    use Class::Factory; our @ISA = qw(Class::Factory);

    give you an error?

      Oh my golly! I think you've just saved my screen having a keyboard embedded in it. That worked just fine! Why would there be a difference ? Someone told me "use base" was more correct... oh well, adrianh, and merlyn - thank you ever so much for your time!


        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.

        Both should work or neither should - if one works and one fails something odd is happening.

•Re: strange error when subclassing packages
by merlyn (Sage) on Apr 20, 2003 at 20:32 UTC
    I don't see any problems with your code immediately, other than that you are violating the convention that lowercase package names are reserved for pragmata, not user objects. Try renaming your packages to an initial uppercase character.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Hi there merlyn! Thanks for the pointer, I shall try to remember that pearl of wisdom. Anyhow, I did as you suggest, and it didn't change anything. The strange thing is, I can execute and it returns no errors. I am completely stumped :(


Re: strange error when subclassing packages
by shoez (Sexton) on Apr 20, 2003 at 20:26 UTC
    opps, slight correction. I moved all my code from d:/site to c:/perlcode, so it's not that...{chuckle}