gargle has asked for the wisdom of the Perl Monks concerning the following question:
Fellow Monks
I currently use a constructor a la:
sub new { my $class = shift; my $self = { FLYBEHAVIOUR => undef, QUACKBEHAVIOUR => undef, }; my @protected = qw/FLYBEHAVIOUR QUACKBEHAVIOUR/; my @private= qw//; my $closure = sub { my $field = shift; grep( /$field$/, @private ) and caller(0) eq __PACKAGE__ || confess "$field is private"; grep( /$field/, @protected ) and caller(0)->isa(__PACKAGE__) || confess "$field is protected"; if (@_) { $self->{$field} = shift; } return $self->{$field}; }; bless( $closure, $class ); return $closure; }
to have private and protected fields in my classes.
I am wondering if there exists a faster way? What do you fellows think?
I particulary like this way. The $self explains ifselve and the two lists called @protected and @private make it clear that what's defined and mentioned in either list is either private or protected
But perhaps I need another OO model in Perl. If someone could point out what inside-out classes would provide I'd be grateful.
|
---|