Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: Re: Re: Tie::SecureHash and AUTOLOAD'ing

by TheDamian (Priest)
on Apr 11, 2003 at 22:52 UTC ( #249994=note: print w/replies, xml ) Need Help??

in reply to Re: Re: Tie::SecureHash and AUTOLOAD'ing
in thread Tie::SecureHash and AUTOLOAD'ing

Okay, but first notice that the AUTOLOAD is enforcing encapsulation. You can call attribute accessors, but only ever to retrieve the current values, not to set new ones.

Remember: encapsulation is not about preventing access to attributes; it's about controlling access to attributes.

If you want the control that the AUTOLOAD enforces to be identical to the control that Tie::SecureHash enforces (which is a perfectly reasonable thing to want), just code AUTOLOAD accordingly:

sub AUTOLOAD { my ($self, $newval) = @_; $AUTOLOAD =~ s/.*:://; my $attr; if (exists $self->{$attr=$AUTOLOAD}) { # public attribute always accessible } elsif (exists $self->{$attr="_$AUTOLOAD"}) { croak "Cannot access protected attribute '$attr'" unless caller().isa(ref $self); } elsif (exists $self->{$attr="__$AUTOLOAD"}) { croak "Cannot access private attribute '$attr'" unless caller() eq ref $self; } else { croak "No such attribute ($AUTOLOAD) for object" } $self->{$attr} = $newval if @_ > 1; return $self->{$attr}; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://249994]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (13)
As of 2019-12-06 18:20 GMT
Find Nodes?
    Voting Booth?
    Strict and warnings: which comes first?

    Results (157 votes). Check out past polls.