in reply to Possibly Stupid OOP question

If you have classes in the same source in which they are used, you need to make sure the inheritance tree is set up before it is used. Often, this means putting the @ISA = into a BEGIN block.

Your imports appear to me to be useless. re your new and import calls, please try to avoid indirect object syntax ("method $object args", or "method Class args"). Use $object->method(args) or Class->method(args) instead. Indirect object syntax has some gotchas.

Replies are listed 'Best First'.
Re^2: Possibly Stupid OOP question (order)
by tye (Sage) on Feb 27, 2008 at 03:24 UTC

    It also helps to put the classes higher in the source file than the uses of them (which matches how classes get used when in separate files). The good practices that you described are also ways to overcome not using this "natural order" but it can still be a good idea to stick to the usual order.

    I'll also note that the original code smells like a typical design made after reading a typical introduction to OO programming. Jumping to using inheritance is probably the most common mistake made by OO programmers who haven't yet become old and tired. Old, tired OO programmers have learned that an "is a" relationship is very tight and quite inflexible and so should be reserved for rather rare cases and only used for a very fundamental connection (and that fundamental connections can still usually be better implemented without inheritance).

    - tye        

      It is funny that most OO books bang on about inheritance so much when the guy who invented the term OO thinks that OO is:

      "OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things." -- Alan Kay

      ___________________
      Jeremy
      Bots of the disorder.

        I think this is such a common pitfall, at least in part, because a person writing a tutorial about OO needs to mention and explain inheritance and so also feels compelled to explain the "power" and "benefits" of inheritance. The pitfall is going on and on about the benefits, rarely even mentioning the pitfalls, and using really sucky examples to explain all of the things that can be done with inheritance.

        Though I'm often quite disappointed that tutorials on OO nearly fail to mention data organization, data hiding, and associating the code with the data it works on. Those are the big wins of OO for me.

        - tye