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

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

Thread Previous | Thread Next
From:
Brent Dax
Date:
November 1, 2001 08:50
Subject:
RE: [PATCH] core-only patch for clamp/readonly hashes
Message ID:
FJELLKOPEAGHOOODKEDPGEANCJAA.brentdax@cpan.org
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;
		}
	}
}

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.
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>?



--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


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