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).
produces: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"; }
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-)
In reply to Re: references to elements of tied hashes
by steves
in thread references to elements of tied hashes
by steves
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |