in reply to Referencing an object's value
What you're looking for is a way to make perl more like C++ or Java, enforcing some sort of privilege (public, private, protected, package(Java)). Perl doesn't do that. Perl takes a completely different tract.
There is a simple convention in perl: any function with a leading underscore (which would be against The Rules(TM) in C - that is reserved for the compiler vendor) is considered "private." Other packages can call it, but it's not recommended - do so at your own risk.
That said, there are ways to enforce things like this. For example, you could create "sessions", and store the real object data in a private hash, e.g.:
my %data; # private hash sub new { my $data = rand(); my $self = bless \$data; $data{$$self}{BKG_IMAGE} = 'image'; } my $bkg_img = # private anon-sub sub { my $self = shift; if (@_) { $data{$$self}{BKG_IMAGE} = shift; } $data{$$self}{BKG_IMAGE}; } # called as: $self->$bkg_img('new_image') # or $image = $self->$bkg_img();
Not recommended. Just document that _bkg_img is a private internal method. You'd be surprised how often a method you think should be private really shouldn't be ;-)
|
|---|