Schuyler D. Erle on Thu, 2 Mar 2000 21:31:01 -0500 (EST)


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Pseudohashes.


Pseudohashes are funny little things, but I like them (q.v. the perlref manpage in 5.005 if this is all new to you). My issue is this: You can read from them like they were hash refs (i.e. $phash->{Foo}, @$phash{"Foo", "Bar"}, %$phash, exists, keys, values, each, etc.) but you can't _write_ to them the same way. Where ordinarily you'd do (with ordinary hash refs):

	%$foo = %$bar

You can't do the same with p-hash references. If they were p-hashes, %$bar would return a list of key/value pairs, which you'd expect from deferencing a hashref, but assignment to %$foo is really (it turns out) compiled as an assignment to @$foo. The magic doesn't work both ways -- in assigning a list to %$foo (i.e. @$foo) you obliterate the index hash and now $foo is no longer a pseudohash! Woe betide to the function that tries to use it as such subsequently! 

So you now have to do (with p-hashes):

	$foo->{$_} = $bar->{$_} for (keys %$foo) # or (keys %$bar)

Well, this only works if $foo and $bar are guaranteed to have the same index hash. Otherwise you get a runtime exception, so then you're looking at something like:

	for (keys %$bar) {
		$foo->{$_} = $bar->{$_} if (exists $foo->{$_})	
		# i.e. the intersection of the two indices
	}

Just to assign the contents of one p-hash to the other. This is madness! I've been developing an integrated set of OO libraries for webserver widgets, and I've been making considerable use of p-hashes (e.g. rows from a SQL query... they all have the same column ordering, so unshift an index onto each row and BOOM! each row is hashed by column name, but the index hash is only stored once in memory and each row is still just stored as an array). Is this a design mistake on my part, given that the manpage says "experimental -- subject to change"? What's the future of p-hashes in upcoming versions of Perl?

I think p-hashes are a Good Thing. If the inconsistency of assignment to a dereferenced p-hash can be cleared up, they'd be a real tool in code design, rather than the mixed blessing they are now (which is only due to the aforementioned hackery currently needed to make them work right). Perl's got enough semi-functional features (e.g. prototypes?) as it is. Anyone? Thoughts? Help?!

(Dominus -- is this good fodder for a lightning talk @ YAPC or what??) :)

SDE
**Majordomo list services provided by PANIX <URL:http://www.panix.com>**
**To Unsubscribe, send "unsubscribe phl" to majordomo@lists.pm.org**