in reply to Tieing anonymous hashes
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Tieing anonymous hashes
by diotalevi (Canon) on Jun 28, 2005 at 22:26 UTC | |
by davido (Cardinal) on Jun 28, 2005 at 22:39 UTC | |
by diotalevi (Canon) on Jun 28, 2005 at 22:52 UTC |