Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Re: strange error when subclassing packages

by shoez (Sexton)
on Apr 20, 2003 at 22:25 UTC ( [id://251893]=note: print w/replies, xml ) Need Help??


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

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

  • Comment on Re: Re: strange error when subclassing packages

Replies are listed 'Best First'.
Re: Re: Re: strange error when subclassing packages
by pfaut (Priest) on Apr 21, 2003 at 00:40 UTC

    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.

        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
Re^3: strange error when subclassing packages
by adrianh (Chancellor) on Apr 21, 2003 at 00:14 UTC

    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://251893]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-04-19 11:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found