develooper Front page | perl.perl5.porters | Postings from July 2001

Re: [PATCH for discussion] new feature: clamp %hash

Thread Previous | Thread Next
From:
Michael G Schwern
Date:
July 29, 2001 01:32
Subject:
Re: [PATCH for discussion] new feature: clamp %hash
Message ID:
20010728180517.D2983@blackrider
On Tue, Jul 24, 2001 at 09:32:54AM +0100, Nick Ing-Simmons wrote:
> my %hash = (a => 1, b => 2);
> my $ref  = \$hash;
> readonly \%hash;     # clamped

C<readonly %hash> should be achievable by simply using the \% prototype.


> $hash{'a'} = 10;    # okay

So we can change the values...


> $hash{'c'} = 30;    # dies "Attempt to add key to readonly hash"
> delete $hash{'b'};  # dies "Attempt to delete key from readonly hash"

...but not delete the key.  That seems a little odd, since you're not
protecting much and it makes the normal delete/exists combination not
work.  Pseudo-hashes had the same problem pre-5.6 and it was very
annoying.

Just want to reiterate the point that if we can change the value of a
key, we should be able to delete the key *and* reinstate it.

    my %hash = (a => 1, b => 2);
    readonly %hash;
    $hash{a} = "foo";
    delete $hash{a};
    $hash{a} = "bar";

All good.  Jeff came up with a workable internal solution by delete()
not actually deleting the key internally, but simply putting a magic
scalar flag on the value being deleted and having exists() look for
that on readonly hashes.



> readonly(\$_) foreach values %hash;  # readonly - fix the values
> $hash{'a'} = 15;    # dies "Attempt to modify readonly value" 

Here, C<delete $hash{a}> would be an error.


-- 

Michael G. Schwern   <schwern@pobox.com>    http://www.pobox.com/~schwern/
Perl6 Quality Assurance     <perl-qa@perl.org>	     Kwalitee Is Job One
grep { ref and ref !~ /^[A-Z]+$/ } kill 9, @ARGV;

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About