in reply to Making a failed hash lookup return something other than undef
Anyway, this isn't too hard to implement with tie:
Be careful with this though. You have to make sure you initialize hash values that you are going to alter. A lot of magic/useful things depend on the default value being undef:package Tie::Hash::Default; require Tie::Hash; our @ISA = qw/Tie::ExtraHash/; sub TIEHASH { my ($class, $default) = @_; bless [ {}, $default ] => $class; } sub FETCH { my ($self, $key) = @_; exists $self->[0]->{$key} ? $self->[0]->{$key} : $self->[1]; } ############# package main; ## -1 is the default value for nonexistant keys in %h tie my %h, 'Tie::Hash::Default', -1; $h{foo} = $h{bar} = 1; print "$_ => $h{$_}\n" for qw/foo bar baz/;
... although using exists on the tied hash will still work as expected.$h{asdf}++; ## $h{asdf} == 0 $h{jkl} .= "jlk"; ## $h{jkl} eq "-1jkl" use strict; $h{xyz}{blah} = 1; ## Can't use string ("-1") as a HASH ref ..
blokhead
|
|---|