Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: strange error when subclassing packages

by adrianh (Chancellor)
on Apr 20, 2003 at 21:56 UTC ( [id://251887]=note: print w/replies, xml ) Need Help??


in reply to problem using Class::Factory

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);

to

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

give you an error?

Replies are listed 'Best First'.
Re: Re: strange error when subclassing packages
by shoez (Sexton) on Apr 20, 2003 at 22:25 UTC
    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!

    shoez

      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
        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 :-)

        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.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://251887]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-24 18:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found