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

arh yes, but I can also do this
package main; my $id = DogTag->new(name=>"Smith",rank=>"private",serial=>'1234567'); print $id->name();
which somewhat defeats the purpose of Tie::SecureHash. What I really need is an autoloader that correctly enforces the same rules as Tie::SecureHash

Replies are listed 'Best First'.
Re: Re: Re: Tie::SecureHash and AUTOLOAD'ing
by TheDamian (Vicar) on Apr 11, 2003 at 22:52 UTC
    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}; }