in reply to Re^3: initialize all values in a hash
in thread initialize all values in a hash
Oh, that's annoying. In fairness, a documented feature of mine is that it explicitly avoids autovivifying keys. So with Tie::Hash::Vivify, this is true:
tie my %hash, 'Tie::Hash::Vivify', sub { 10 }; say $hash{foo}; # says 10 my $is_this_true = exists $hash{foo};
Whereas this is false:
tie my %hash, 'Hash::DefaultValue', sub { 10 }; say $hash{foo}; my $is_this_true = exists $hash{foo};
Another feature of Hash::DefaultValue is that the coderef gets passed a copy of the key, so can use that information when generating the default value.
tie my %hash, 'Hash::DefaultValue', sub { uc }; say $hash{foo}; # says "FOO"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: initialize all values in a hash
by DrHyde (Prior) on May 18, 2012 at 15:35 UTC | |
by tobyink (Canon) on May 18, 2012 at 16:49 UTC |