http://www.perlmonks.org?node_id=684065


in reply to Default Hash Key

Is there a way to define a default value for a hash?
I do not know. But, you could create a function to see if a key exists:
use warnings; use strict; my %description = ( a => 'a vowel', b => 'a consonant' ); print 'a is ', check(\%description, 'a'), "\n"; print 'b is ', check(\%description, 'b'), "\n"; print '~ is ', check(\%description, '~'), "\n"; sub check { my ($href, $key) = @_; return (exists $href->{$key}) ? $href->{$key} : 'not in the alphab +et'; }

prints:

a is a vowel b is a consonant ~ is not in the alphabet