in reply to oops concept in perl programming

Hi Tony.

Perl provides very minimal extra magic for object oriented programming. In fact, as I understand it, there's only 3 things Perl does for you to implement OOP:

  1. It provides the `->' syntax, so that when you use that syntax ($some_object->some_method;), perl makes sure to automatically pass the invocant to the method as the first argument.
  2. It provides inheritance via the @ISA package global. You put the names of parent classes into @ISA, and voila, you've just made your class a child of those listed in @ISA. When you call a method that's not in your current class, perl digs around in the ones listed in @ISA to find it.
  3. It provides the bless method to make the above two bits of magic work properly.

That's basically it. No automatic constructor calls, no automatic object allocation. Your ``object'' is actually (usually) just an anonymous hash which you create in (and is returned by) your `new' class method, and that gets passed around for you when calling methods. The ``instance attributes'' are just keys in that hash, and ``class attributes'' are just lexicals in the module where you write your methods.

When using objects, as long as you only ever access attributes via methods (and not directly -- which you, of course, can if you like to live dangerously), everything mostly just works like it ought to.

One last tip: The method that gets called is always looked up via the object reference (that blessed anonymous hash), so even if some base class method calls another base class method, if your own child class has a method of that name, the child class method is the one that'll get used.

Addendum: If a method is expecting certain instance attributes to exist, you'be better make sure they're there. Methods and instance attributes (the values you have in your anonymous hash) are two totally different things. Welcome to Perl OO: Watch your step. Here's your helmet. :)

Replies are listed 'Best First'.