I don't feel at all comfortable with Perl OO, to be honest: it feels... weird to me for some reason.
I think a lot of people give "OO" a lot more fear and awe than it deserves. I think partly this is because of all the multisyllable terms that the jargon freaks just love to insist is the cool parts of OO. (Polymorphism, Inheritance, Method Dispatch, Overloading, etc). Perl's OO is really close to the raw idea of OO: it tightly associates data with the methods/subs that operate on that data. (The fact that Perl also does pretty much the full repretoire of OO, with the exception of Data Hiding, but thats not an essential part of OO in my mind.)
So lets consider this subroutine structure...
sub make_complex_array {
my @args=@_;
# ...
return $complex_array_ref
}
sub do_funky_stuff_with_complex_array {
my $array=shift;
# ....
return $value;
}
To convert this to a class/object all we have to do is add one line and alter two! (Well as a sop to convention and aesthetics we'll rename the subs)
package Complex::Array;
sub new {
my ($class,@args)=@_;
# ...
return bless $complex_array_ref,$class;
}
sub do_funky_stuff {
my $array=shift;
# ....
return $value;
}
And whats so hard about this? Now the $array knows that it isnt just an array with indexes, it knows its a complex data strcture with a set of extended behaviour. If we need to cache things about the array we have a nice convient handle that we do so with.
The point of this all is that you want to maintain state through a set of package level variables. This isnt a good idea a general rule (singletons aside). If you need to maintain state across multiple subroutine invocations you should use OO. Especially as in Perl its so damn easy. :-)
my objects really have no attached methods and very little need for privacy.
There is no privacy in Perl so thats no excuse. As for the methods from what youve described all of your subroutines are method candidates to me. If you like post a more beefy code example and ill refactor it into OO for you.
HTH
---
demerphq
<Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
|