in reply to Does one encapsulate a class from itself?
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.
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.{ my $data= $self->{dataref}; foreach my $x (0..$max_x) { $data->{$x}{$y} = $new_value; } }
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!
|
---|