Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Does one encapsulate a class from itself?

by toma (Vicar)
on Mar 07, 2008 at 08:14 UTC ( [id://672715] : note . print w/replies, xml ) Need Help??

in reply to Does one encapsulate a class from itself?

The important thing is to get used to building object-oriented modules, and make it part of your normal coding style. Build them in any way that seems easy and natural. If you need performance, optimize.

I tried several alternatives, and then settled on a blessed hash for most of my objects. I don't use the getter/setter functions internally, except when I think the object data access needs a more future-proof implementation.

For your matrix example, I would be tempted to access the data even more directly, bypassing the object reference altogether.

{ my $data= $self->{dataref}; foreach my $x (0..$max_x) { $data->{$x}{$y} = $new_value; } }
I use the smallest scope possible for the lexical variables that refer to chunks of my object. These lexical variables have descriptive names, and for me the code is easier to read. This approach reduces the length and complexity of my mathematical and string expressions, especially for setter functions.

I haven't seem much of a performance difference when I use the lexicals to refer to a sub-object, but I have sometimes had speed problems with getter and setter function calls.

There are good arguments for both ways, though. Since it depends on what you are doing, do whatever seems best, and it will be fine. If it isn't, change it!

It should work perfectly the first time! - toma