If you don't want other code to alter attributes, then perhaps using a hashref isn't the right way to implement an object.
Inside out objects are designed to prevent this. And with 5.10.x, implementing inside out objects is much easier:
use strict;
use warnings;
use Hash::Util::FieldHash;
fieldhash my %attr1; # You need one for each attribute.
sub new {bless \do {my $var}, shift}
sub set_attr1 {
my $self = shift;
$attr1{$self} = shift;
}
sub get_attr1 {
my $self = shift;
$attr1{$self}
}
etc.
Since the attribute is tucked inside a lexical variable, it's impossible for code defined outside of the scope to touch the attribute.
There's also a couple of modules out there to help you implement inside out objects, but I don't think they make use of fieldhashes (meaning they do extra work at DESTROY and CLONE time). Of course, if you're stuck with an old perl, those modules are the way to go.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.