W0lf's Guide to Perl OOP for C/C++ Programmers

Tuesday, October 18, 2005

 

Creating Objects

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;


Comments: Post a Comment



<< Home

Archives

October 2005  

This page is powered by Blogger. Isn't yours?