in reply to references to elements of tied hashes

This is good. Thanks.

Maybe you can answer this one: The bug that started me down this path is something I mentioned having problems with before but in a different area. I can't tie elements of the tied hash. I think someone mentioned this as not possible. The reference issue came up in the course of tracking that down because when tying the reference, the tie works. I can only assume it's tying something I can no longer refer to. Test code using a hash-based scalar tie for the hash elements that upper cases them. Note that I also tried Tie::StdScalar with the same results (except that the tie did nothing).

package TieTest; sub TIESCALAR { return bless({}, __PACKAGE__) } sub STORE { @_[0]->{VALUE} = uc(@_[1]) } sub FETCH { @_[0]->{VALUE} } use strict; use Tie::IxHash; my $h = {}; my $a; tie %$h, "Tie::IxHash"; tie $h->{FOO}, "TieTest"; $h->{FOO} = 'foo'; tie $a, "TieTest"; $a = 'bar'; print "After scalar tie ...\n"; print "\$h->{FOO} = $h->{FOO}\n"; print "\$a = $a\n"; print "\$a tied? ", tied($a), "\n"; print "h->{FOO} tied? ", tied($h->{FOO}), "\n"; tie_ref(\$a); tie_ref(\$h->{FOO}); $a = 'bar2'; $h->{FOO} = 'foo2'; print "After reference tie ...\n"; print "\$h->{FOO} = $h->{FOO}\n"; print "\$a = $a\n"; print "\$a tied? ", tied($a), "\n"; print "h->{FOO} tied? ", tied($h->{FOO}), "\n"; sub tie_ref { my $ref = shift; my $tie; $tie = tie $$ref, "TieTest"; print "tie for reference $ref = $tie\n"; }
produces:
After scalar tie ... $h->{FOO} = foo $a = BAR $a tied? TieTest=HASH(0x13c8dc) h->{FOO} tied? tie for reference SCALAR(0x102efc) = TieTest=HASH(0x13c81c) tie for reference SCALAR(0x13c8d0) = TieTest=HASH(0x13c840) After reference tie ... $h->{FOO} = foo2 $a = BAR2 $a tied? TieTest=HASH(0x13c81c) h->{FOO} tied?

I love the abstraction ties offer but I find that all the nuances of using them are not well documented. Are there docs I'm missing that discuss ties in more detail?

The good news is that all this tracking helped me finally understand some inconsistent behavior. 8-)

Replies are listed 'Best First'.
Re^2: references to elements of tied hashes
by steves (Curate) on Sep 10, 2004 at 19:56 UTC

    So, it's a year and a half later and this gets me again in a way I can't solve: I have a self-referential hash. I use WeakRef to weaken the hash elements to avoid leaking/growing programs. But if I tie the hash I can't find any way to weaken the tied references. Any ideas besides shooting me in the head?

      Question related to this: Are stringified references used as hash keys unique? That is, if I only ever use them to look hashed values for given references up, do I need to use something like Tie::RefHash? I've gotten into the habit of using Tie::HashRef whenever I use references as hash keys. I realize now that in many cases I'm never trying to dereference the keys -- only take a reference and find its hashed value. So in the case above, I probably don't need a tie and I can therefore weaken the self referential reference I'm using as a key. The hash in question is always hashing references to bless'ed hashes of the same object (package) type.