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

Re: [PATCH] core-only patch for clamp/readonly hashes

Thread Previous | Thread Next
Nick Ing-Simmons
October 31, 2001 08:03
Re: [PATCH] core-only patch for clamp/readonly hashes
Message ID:
Jarkko Hietaniemi <> writes:
>> >+=item Can't access nonexistant key {%s} of readonly/clamped hash
>> That "clamped" word has IMHO got to go ...
>How about "restricted"?

Depends what it means - "restricted" is suitably vague ;-)

>If I got it right: at the moment of "restricting" the hash the keys
>that exist become the only allowed keys.  No new elements with new
>keys can be added.  Existing elements can be deleted, though (and
>there the "magic marker" is used), and the elements can be added back.
>This is a subset of READONLY, in that with READONLY *no* changes
>are allowed.
>                add     delete  add     delete  access
>                old     old     new     new
>READONLY        -       -       -       -       +
>restricted      +       +       -       -       +
>normal          +       +       +       +       +

Hmm, then I think what I have been calling READONLY is what you have
labeled restricted above.

%hash = (foo => 1, bar => 2);

$hash{foo} = 3;     # okay allowed key
$hash{foo}--;       # okay allowed key
$hash{baz} = 7;     # error - not an allowed key
if ($hash{baz}) ... # error - not an allowed key
exists $hash{baz};  # error - not an allowed key
delete $hash{baz};  # error not an allowed key
exists $hash{foo};  # true
delete $hash{bar};  # okay
exists $hash{bar};  # now false
if ($hash{bar}) ... # okay - allowed just not there

That is READONLY fixes the set of allowed slots (keys for a hash,
upper bound for an array) but not the values.

One can of course fix the values (without anything extra) thus:

%hash = (foo => 1, bar => 2);
Access::Readonly($_) foreach values %hash;

$hash{foo} = 3;     # error - modify readonly value
$hash{baz} = 7;     # error - not an allowed key etc. etc.
delete $hash{bar};  # ????

We could define semantics that a READONLY value in a READONLY hash
was not delete-able. Snag is that this may occur "by accident".

I assume this extra delete protection is the root of the extra flag bit.
This is an extra feature which is orthogonal to the key names.

So if we do add the feature I would rather it was a property of the value
and not the hash. That way one could mark some values as "stuck" while
allowing others to come and go/change.
For example:

%nick = (Date_of_birth => ...,   # fixed and non-deleteable
         Location      => ...,   # must have one, but can vary
         Mobile_number => ...,   # optional

(Aside - is Storable going to save/restore RO-ness ???)

Nick Ing-Simmons

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About