in reply to Object insanity and data hiding
This will dynamically create methods such as getname to access the underlying data, rather than letting someone poke at it willy-nilly. This keeps your access methods and your data in sync. It also allows for easier error checking, since all your methods are being dynamically defined in one place.package CD::Music; use strict; use vars '$AUTOLOAD'; # Keep 'use strict' happy { my %_attrs = ( _name => undef, _artist => undef, _publisher => undef, _ISBN => undef, _tracks => undef, _rating => undef, _room => undef, _shelf => undef, ); sub _accessible { exists $_attrs{$_[1]} } } sub AUTOLOAD { my ($self) = @_; $AUTOLOAD =~ /.*::get(_\w+)/ or croak "No such method: $AUTOLOAD"; $self->_accessible($1) or croak "No such attribute: $1"; $self->{_read_count}++; return $self->($1); }
-Blake
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Object insanity and data hiding
by thpfft (Chaplain) on Sep 09, 2001 at 14:36 UTC |