Essentially, what appears within the tie function must have a sigil. That means that tie expects an actual hash, not a hashref. The {...} anonymous hash constructor returns a hashref, which doesn't work. For example:
tie( %hash, 'Class' );
...is valid, but ...
tie( { anon => 'hash' }, 'Class' );
...is not valid. However, you can tie the nested portions of nested datastructures like this:
my $href = {}; tie( %{$href}, 'Class' );
...or even...
my( %hash ) = ( 'this' => {}, 'that' => {} ); tie( %{$hash{'this'}}, 'Class' );
Also note, definately it is not doing what you expect if you do this:
my( %hash ) = tie( %otherhash, 'Class' );
...because tie doesn't return the tied hash, it returns the tied object's instance reference. Since you're assigning the object's reference to %hash, you'll stringify the object ref and turn it into a hash key in %hash with undef as the value. So don't do that. In other words, this does work:
my $obj = tie( %hash, 'Class' ); $obj->DESTROY;
Dave
In reply to Re: Tieing anonymous hashes
by davido
in thread Tieing anonymous hashes
by shemp
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |