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

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

Thread Previous | Thread Next
From:
Nick Ing-Simmons
Date:
November 1, 2001 09:38
Subject:
RE: [PATCH] core-only patch for clamp/readonly hashes
Message ID:
20011101173810.6376.10@bactrian.elixent.com
Brent Dax <brentdax@cpan.org> writes:
>Nick Ing-Simmons:
># >That's what NI-S was saying : "exists $hash{baz}" fires an error on a
># >non-existing key. But in this case an API is needed to know
># what's the set
># >of allowed keys,
>#
># Perhaps. But enumerated hashes are approx. equivalent to C structs
># (this is problem we are trying to solve). C does not have a way
># to determine at run time the members in a struct - rather the code
># should only use members that exists.
>
>C doesn't have any way to address the members of a struct at runtime,
>either.  What if I put this function into a module:
>
>sub load_hash_from_file(\%) {
>	my($hash)=@_;
>	while(<IN>) {
>		my($k, $v)=split(/:\s*/, $_);
>		$hash->{$k}=$v;
>	}
>}
>
>Now, I hear about these newfangled restricted-key hashes, and I want to
>support them.  With the current proposal, I do this:

>
>sub load_hash_from_file(\%) {
>	my($hash)=@_;
>	while(<IN>) {
>		my($k, $v)=split(/:\s*/, $_);
>		if(exists($hash->{$k})||!defined(exists $hash->{$k})){
>			$hash->{$k}=$v;
>		}
>		else {
>			carp "Wanted to load disallowed key '$k' a restricted hash" if $^W;
>		}
>	}
>}


The whole point (IMHO) is that "trying" is an error not a warning.

So the original code:

>sub load_hash_from_file(\%) {
>	my($hash)=@_;
>	while(<IN>) {
>		my($k, $v)=split(/:\s*/, $_);
>		$hash->{$k}=$v;
>	}
>}

Will "just" work if allowed, and fail automatically without being changed
if it isn't allowed.

>
>With the (perhaps wiser) proposal where undef is disallowed and 0 is
>allowed but deleted, it's:
>
>sub load_hash_from_file(\%) {
>	my($hash)=@_;
>	while(<IN>) {
>		my($k, $v)=split(/:\s*/, $_);
>		if(defined exists $hash->{$k}){
>			$hash->{$k}=$v;
>		}
>		else {
>			carp "Wanted to load disallowed key '$k' a restricted hash" if $^W;
>		}
>	}
>}
>
>With your proposal...hmm...the best thing I can think of is to use an
>eval BLOCK.  Ugh.

>
>sub load_hash_from_file(\%) {
>	my($hash)=@_;
>	while(<IN>) {
>		my($k, $v)=split(/:\s*/, $_);
>		if(eval {exists $hash->{$k}; 1}){
>			$hash->{$k}=$v;
>		}
>		else {
>			carp "Wanted to load disallowed key '$k' a restricted hash" if $^W;
>		}
>	}
>}
>
>How do you explain *that* construct to a newbie?  (And if you force me
>to use that construct, I'll put a comment to the effect of 'thank you
>SOOOOOO much, GBARR' to the right of it each time I use it. :^) )
>
># >or perhaps only to know whether some key is allowed. What
># >will this API do with regular hashes?
>#
># NI-S is also saying that keys %enumerated_hash should return
># the allowed keys.
>
>That's a Really Bad Idea, and I think everyone can agree on that.

You mean everyone else - I obviously don't agree ;-)

>Wasn't one of the big problems with pseudohashes in 5.005 that all the
>builtins had totally different behavior when using them?  What about the
>construct C<@hash{keys %otherhash}=values %otherhash>?

That is fine if it is written as

             %hash = %otherhash;

If you really want the long form then you need to make %values
provide an undef or other token for the allowed-but-deleted slots
(it can be 'the' special undef so that deleted-ness is preserved).

But actually my _favourite_ version of all this does not allow delete
so whole keys() problem goes away ;-)

>
>
>
>--Brent Dax
>brentdax@cpan.org
>Configure pumpking for Perl 6
>
>When I take action, I’m not going to fire a $2 million missile at a $10
>empty tent and hit a camel in the butt.
>    --Dubya
--
Nick Ing-Simmons
http://www.ni-s.u-net.com/



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