Subject: | delete on a tied hash returns an unusable value |
Try the following script:
#!/usr/bin/perl
use Tie::DxHash;
tie %h, 'Tie::DxHash', ( r => 'red', g => 'green', g => 'greenish',
b => 'blue' );
$deleted = delete $h{b};
use Data::Dumper;
print Dumper \%h, $deleted;
This produces a rather surpising output (at least, to me):
$VAR1 = {
'r' => 'red',
'g' => 'green',
'g' => 'greenish'
};
$VAR2 = bless( {
'occurrences' => {
'r' => 1,
'g' => 2
},
'ckey' => 3,
'data' => [
{
'value' => 'red',
'key' => 'r'
},
{
'value' => 'green',
'key' => 'g'
},
{
'value' => 'greenish',
'key' => 'g'
}
],
'iterators' => {
'r' => 1,
'g' => 1
}
}, 'Tie::DxHash' );
In short: delete returns the tied object, not the value that got deleted
from the hash, as any normal hash would do, and as is expected of a hash.
In other words, I had expected to see:
$VAR1 = {
'r' => 'red',
'g' => 'green',
'g' => 'greenish'
};
$VAR2 = 'blue';
Don't ask me what I would have expected if the tied hash contained more
than one value for this key. Maybe in list context, it could return a
list of all values that all got deleted? No idea what to expect in
scalar context, probably just one of the values.
Of course, none of this happens, instead, it produces a similar result
as the above test, i.e. delete returns the tied object.