develooper Front page | perl.perl6.internals | Postings from October 2002

Two problems groping around in PerlHashes

Thread Next
Clinton A. Pierce
October 19, 2002 20:55
Two problems groping around in PerlHashes
Message ID:
While working on ...something... I found the need to be able to tell if a 
key exists in a PerlHash.  Here's the kicker, I don't know what kind of 
data's gonna be there: int, float, PMC, or string.

After hunting around in t/perlhash.t I found a few examples of checking for 
keys that don't exist.  They look like:

	new P1, .PerlHash
	set P0, P1["not_there"]
	typeof S1, P0
	eq S1, "PerlUndef", NOTTHEREBRANCH

Okay this is cool, except it doesn't work if the hash has mixed types in it 
and the key you're looking for *actually exists*.  For this code:

	new P1, .PerlHash
	set P1["mykey"], I0   # So it will exist
	set P0, P1["mykey"]

Parrot correctly throws an error:

	Cannot fetch PMC out of non-PMC key!

Correct or not, this is a pain in the butt.  There's no way to grope around 
in a PerlHash unless you already know what keys are there and what type 
they are.

What I think we need is an exists opcode.  And looking at 
classes/perlhash.c it looks like there's a exists function already 
there.  I just can't figure out how to use it in PASM.  There appears to be 
no opcode for it.

Is this something of the PMC API we can expose to PASM programmers?

Can someone gimme a hint of how to make this a real live opcode?  Or, if 
someone can easily turn this on somehow, I think the best API for something 
like exists would be:

	exists Px[key], branch

So once I can tell the key is there, that brings up the second problem: 
what exactly is behind that key?  A PMC?  A native type?  The only thing I 
can think of that would suffice would be to tinker with typeof to make it 
able to recognize the PASM native types (N, I, S) and then to make it a 
keyed op.

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