So how do you create objects? Again, before we delve into the details: a word of warning. What you will see will only be 'morphologically' similar to C++. i.e. it will look like C++ code, but the underlying mechanisms shall be very different, and to someone unfamiliar with Perl, just completely cryptic.
So unless you have a good handle on Perl's native data types, nested data structures and references, don't even bother with OOP programming in Perl.
To create an object:
# Here's my class again.
package CMyClass
sub method1 {
print "method1\n";
}
sub method2 {
print "method2\n";
}
sub new {
my $class = shift;
my $self = $class;
bless($self, $class);
}
package main;
my $object1 = new CMyClass;
my $object2 = CMyClass->new;
In C++, a class is merely an abstraction that serves as a specification, if you will, from which 'objects' get created at run-time. An object is an instance of a class -- It is the living organism that is made up from the DNA of its class.
So, for instance, in C++, if you had a class:
class CMyClass {
public:
int method1();
int method2();
};
declared in the file CMyClass.h, with the method implementations in file CMyClass.cpp, the Perl equivalent would be a file CMyClass.pm, containing:
#!/usr/bin/perl
package CMyClass;
sub method1 {
# Method Implementation
}
sub method2 {
# Method Implementation
}
Et Voila! You have a class.
Now I mentioned the two addressing operators "::", "->", and also the relationship between an object and its class.
We are all familiar with how objects come into being -- lets just recap:
- By 'static' allocation in the BSS as global variables or on the program stack as automatic variables.
- By dynamic memory allocation on the heap using new or 'placement' new.
Note that in C++, the actual allocation of the object is handled by the C++ run-time libraries. Of course, you can define your own allocation mechanism by overloading the 'new' and 'delete' operators. The 'constructor' for the object is then called after memory for the object has been allocated. In that sense, a C++ constructor is really only an 'initializer'.
Perl, however, does things a little differently.
In fact, Perl requires you to handle the allocation for the object yourself. This is usually done in the constructor method. So you could argue that Perl object constructors are constructors in the truest sense of the word.
Perl classes are perl packages. Say that twice, and two times more.
So if you want to create a class, just create a package. The class name should be the package name.
Now, all methods and variables you create belong to the namespace of the package you just created.
Ergo, you have a class.
Now, as C++ programmers, we're used to using two kinds of operators for traversing class/object spaces:
The scope-resolution operator: "::".
The class member operator : "."
and its sibling, the class pointer member access operator : "->".
Perl provides both these types of operators i.e. an operator to traverse the class namespace, and an operator to access object menbers.
The actual semantics of these operators are different, but hey, it sure looks like C++ to me.
I am learning to use Perl's Object Oriented Programming facilities. Its a bit different from what I was used to in C/C++.
For one thing, the entire OOP framework in Perl is a chimera, built out of namespace hacks in the interpreter.
Once you get your head around that, there's no stopping you.
I'm still struggling with some advanced concepts, so won't talk about them now, but let's start with the basics.