OOP
Saturday, December 5th, 2009Object orientated Perl! Boy, if there is one thing that I want to fully grasp, this would be it. So I spent time (yet again) reading perldoc perlboot. This would be the second time I’ve been through the beginner’s guide to OO Perl. Here’s are the key points (from memory which may be inaccurate):
- Packages are used to create “Classes”
- In each class, you have methods which operate on an object
- Methods are created by using Perl’s subroutines
- Methods from can be “inherited” from another class using the @ISA array
- The @ISA variable needs to be explicitly (if that’s the right word) declared, either using the full namespace or defined globally using “our”
- A constructor is a method used to create a specific instance
- An instance is a blessed Perl reference (be it a scalar, hash or array)
- The arrow invocation arrow “->” is used to specify a method for an object (i.e. $object->$method)
- When using “->” with an instance, the first argument passed to the method is the object
- When using “->” in the manner of Class->method, the first argument passed will be the class
- Interestingly (to me), when you use “->” with an instance, the method can still pick up the class from the instance
So by using these ideas, you can create classes with packages; within these classes you can create methods, i.e. the new method as the constructor which creates an instance of the object.
I think the best way to really learn OO Perl, is to start writing some. I will post my OO Perl exploits as they come along.