I am currently writing a large-ish set of classes, with a decent amount of inherritence and other shennanigans going on. For the first time, I am going to enforce strict OO standards by hiding my class data and using closures for accessors, as suggested on
perltooc. So I have something like this set up:
package My::Class;
use strict;
use warnings;
{
# hide from the rest of the class
my %CLASSDATA = ( foo => 1, bar => 2, baz => 3 );
# install generic accessor/mutator methods
no strict 'refs';
for my $key(keys %CLASSDATA) {
*$key = sub { shift;
$CLASSDATA{$key} = shift if @_;
return $CLASSDATA{$key};
};
}
use strict 'refs';
}
This works great, and allows me to inherrit and/or override the class data in subclasses.
But now, I want to do the same thing for instance data. (I don't need to inherrit instance data, I just want to protect it.) Of course, I can't simply install more closure subs in the constructor, because then each instance would re-define the subs from the previous ones. I don't want to store the instance data in the blessed reference, because that would allow direct access to it.
I thought about doing something like AUTOLOAD with closures, but that doesn't seem like a great solution. Any ideas would be appreciated! Thanks,
-Mike
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.