Re: Comments: locking variables
From: Matthew Braid (mb_at_uq.net.au.invalid)
Date: 11/21/03
- Next message: Tyaan: "prevent \\\\ from getting substituted to \ in a system call."
- Previous message: John: "Re: trying to understand fork and wait"
- In reply to: Ala Qumsieh: "Re: Comments: locking variables"
- Next in thread: Ben Morrow: "Re: Comments: locking variables"
- Reply: Ben Morrow: "Re: Comments: locking variables"
- Reply: Eric J. Roode: "Re: Comments: locking variables"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 21 Nov 2003 10:13:45 +1000
Ala Qumsieh wrote:
> package lockKey;
>
> sub TIEHASH {
> my ($class, $key) = @_;
>
> my $obj = bless {
> LOCKED => $key,
> HASH => {},
> };
> return $obj;
> }
>
> sub FETCH { $_[0]{HASH}{$_[1]} }
>
> sub STORE {
> my ($this, $k, $v) = @_;
>
> if ($this->{LOCKED} eq $k) {
> # only store if it's not defined already.
> return if exists $this->{HASH}{$k};
> }
>
> $this->{HASH}{$k} = $v;
> }
>
> sub FIRSTKEY { # copied from perltie
> my $this = shift;
> my $a = keys %{$this->{HASH}}; # reset each() iterator
> each %{$this->{HASH}};
> }
>
> sub NEXTKEY { each %{$_[0]{HASH}} }
Hmmm. This still has a problem (that might actually exist in the
Tie::Watch form too):
# put the package lockKey stuff here....
package main;
my %hash;
tie %hash, 'lockKey', 'AltUID';
$hash{test} = 'a';
$hash{AltUID} = 'value1';
print "$_ = $hash{$_}.\n" for keys %hash;
# NEW STUFF HERE
package lockKey;
sub STORE {
my ($this, $k, $v) = @_;
$this->{HASH}{$k} = $v;
}
# END NEW STUFF, which could be inserted in a script easily
package main;
$hash{test} = 'b';
$hash{AltUID} = 'value2';
print "$_ = $hash{$_}.\n" for keys %hash;
__END__
Which results in:
AltUID = value1.
test = a.
AltUID = value2.
test = b.
I'm begining to wonder if there's a way of locking a value at all now....
1) The Hash::Util has the problem that anyone can unlock the value using
Hash::Util as well.
2) The direct tie (and possibly Tie::Watch) method suffers from a
package override.
I'm starting to think the way to do it may be:
package Whatever;
my $Protected = {};
...
sub _initialise {
my $self = shift;
...
$Protected->{$self} = {AltUID => -1};
...
}
sub AltUID {
my $self = shift;
return undef if not exists($Protected->{$self});
return $Protected->{$self}->{AltUID};
}
sub DELETE {
my $self = shift;
delete($Protected->{$self});
}
__END__
EOF
That way the AltUID value is stored in a my'd package variable that
can't be accessed directly from the outside (even by a package override)
and only has a readonly accessor. Now I just need to figure out how
child packages are going to work with this...
Blurgh. This is why I still think perl5's objects are very broken. The
paradigm that 'people messing with your internals should know what
they're doing anyway' is fatally flawed - they may know exactly what
they're doing - circumventing code you really don't want altered...
- Next message: Tyaan: "prevent \\\\ from getting substituted to \ in a system call."
- Previous message: John: "Re: trying to understand fork and wait"
- In reply to: Ala Qumsieh: "Re: Comments: locking variables"
- Next in thread: Ben Morrow: "Re: Comments: locking variables"
- Reply: Ben Morrow: "Re: Comments: locking variables"
- Reply: Eric J. Roode: "Re: Comments: locking variables"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|