package Password; use Carp; use strict; my $security = 'reallysecure'; sub TIESCALAR { my $class = shift; my $arg; return bless \$arg, $class; } sub FETCH { my $self = shift; confess "wrong type" unless ref $self; croak "usage error" if @_; return substr($$self ^ $security, 0, length($$self)); } sub STORE { my $self = shift; confess "wrong type" unless ref $self; my $newval = shift; croak "usage error" if @_; if (length($newval) < 5) { carp "Not long enough" } elsif (length($newval) > 12) { carp "Too long" } else { $$self = substr($newval ^ $security, 0, length($newval)); # print "Stored ", join('.', unpack('H2'x length($$self), $$self)), "\n"; # print "Plain is ", join('.', unpack('H2'x length($$self), $newval)), "\n"; } } sub encoded_form { my $self = shift; $$self; } package Main; my $foo; # This would be my object my $pwref = tie $foo->{'bar'}, 'Password'; $foo->{'bar'} = 'squamous'; print $foo->{'bar'}, "\n"; $foo->{'bar'} =~ s/am/a1m/; print $foo->{'bar'}, "\n"; print "Encoded form is ", $pwref->encoded_form, "\n";