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

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

Thread Previous | Thread Next
From:
Jeffrey Friedl
Date:
July 19, 2001 11:43
Subject:
Re: [PATCH for discussion] new feature: clamp %hash
Message ID:
200107191843.LAA12424@ventrue.corp.yahoo.com

Graham Barr <gbarr@pobox.com> wrote:
|> I too have often wanted something like this. Currently I do it by a tied
|> hash, but due to performance only do so when I run my script in a debug
|> mode.

I've done this type of thing as well, but it seems that with complex
systems working on complex data, the debugging doesn't always uncover
everything.

|> However I would disagree that reading a non-existant entry should croak.

My thinking here is that when you create the hash (and I'm mostly thinking
in a using-a-hash-as-an-object mode), you would pre-create all fields
you'll ever need:

   sub Employee::New($$$)
   {
	my $class = shift;
	my $lastname = shift;
	my $firstname = shift;

	my $ref = bless {
	     Family  => $lastname,
	     Given   => $firstname,
	     DOB     => undef,
             EmpNum  => undef,
             Manager => undef,
	}, $class;
	clamp %$ref, 1;  ## only those keys listed above are ever used
	return $ref;
   }

This also helps toward documentation, since every time you want to add some
random key, you'll also have to go back and add it to the New() function.
Having them listed all there is the first step toward documenting them well.

If you really want to add another on the fly, you can easily do it with
	my $prev = clamp %$ref, 0;
	$ref->{NewKey} = 1;
	clamp %$ref, $prev;

(it's just flipping a bit, so is very low impact).

	Jeffrey

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