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

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

Thread Previous | Thread Next
Michael G Schwern
July 20, 2001 12:10
Re: [PATCH for discussion] new feature: clamp %hash
Message ID:
On Fri, Jul 20, 2001 at 03:16:35AM -0700, Jeffrey Friedl wrote:
> What I've done so far is certainly not complete, but it shows the idea:
>    * When an element is deleted from a clamped hash, rather than simply
>      remove the whole key/value pair, I keep the key in there with a
>      special placeholder value. Internally, this is distinct from undef or
>      any possible scalar. (I created "PL_sv_placehold", which is similar to
>      "PL_sv_undef").
>    * exists(), keys(), values(), hash access, etc., all know to ignore this
>      placeholder value, so for their purposes it's as if the key doesn't
>      exist.

Okay, I had a worry that this would slow down exists() in general
(since it would be necessary to look at the value before determining
existence), but you could do that extra logic only if its clamped.

But if you do that... what happens when a hash becomes unclamped?  You
can't delete these PL_sv_placehold values because unclamping might be
followed by a clamp (ie. you unclamped for a moment to add a new key).


Then again, since exists() has to go through the trouble of looking up
the key *anyway* it shouldn't be much extra work for it to look at the
value and see if its PL_sv_placehold.  The difference, hopefully,
should not be noticable.  Benchmarking is in order.

Anyhow, yes, this is good. :)

> So, with just this added functionality, it's ugly, but you could:
>      1) create your hash with all approved keys.
>      2) clamp it
>      3) delete keys you didn't want at the moment. This leaves them
>         pre-populated in memory, but !defined() and !exists() and
>         not showing up with keys() or values() or each().
> Of course, it would be nice to go further and provide ways to:
>      * set/add/remove approved keys [can do now, but ugly]
>      * query if a key is approved [can do now, but ugly]

I agree with Arthur, we can make this all pretty with a simple module.

>      * query the list of all approved keys [can't do now]

Some XS should be able to do that.  "Solved problem"


Sounds acceptable.


Michael G. Schwern   <>
Perl6 Quality Assurance     <>	     Kwalitee Is Job One
Maybe they hooked you up with one of those ass-making magazines.
	-- brian d. foy as misheard by Michael G Schwern

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