Re: Comments: locking variables

From: Matthew Braid (mb_at_uq.net.au.invalid)
Date: 11/21/03


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...



Relevant Pages

  • SSIS - Adding Mappings Programatically
    ... I am stuck with Adding Mappings, ... '// Here is subset of my sample code ... Sub SSISPackageDemo() ... Savepackage(pkg, True) '//Save Package to SQL Server ...
    (microsoft.public.sqlserver.dts)
  • Re: best practice ... requires
    ... affect the way Perl parses your program, like strict and warnings). ... Do you know what calling a sub with & does? ... so you can see that my package is nested and I am now wondering how ...
    (comp.lang.perl.misc)
  • Re: perl menubased user interface
    ... actually use a perl package OR a hash for this, In a "perl package" ... sub map_method { ... # and they're all shell commands. ... Do whatever you want to wrap a shell command. ...
    (comp.lang.perl.misc)
  • Re: Using a DBI connection in many places (in the code)
    ... # non-exported package globals go here ... sub set_name { ... nothing to do with DBI. ...
    (comp.lang.perl.modules)
  • Re: multiple packages/classes in one file
    ... > so you can use globals under strict and not just lexicals. ... > this means global to the package that our is used in. ... > sub set_member ... > return $member; ...
    (comp.lang.perl.misc)