Skip to content

Instantly share code, notes, and snippets.

@moritz
Created March 21, 2012 18:00
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save moritz/2150358 to your computer and use it in GitHub Desktop.
Hashes that die when reading undefined values
1
5
10
No such hash key 'x'
in sub <anon> at safe-hash.pl:10
in any <anon> at src/gen/BOOTSTRAP.pm:306
in sub say at src/gen/CORE.setting:6226
in block <anon> at safe-hash.pl:24
role safety {
method at_key($key) is rw {
my $store = sub ($, $value) { self.STORE_AT_KEY($key, $value) };
self.exists($key)
?? Proxy.new(
FETCH => sub (|$) { self.EnumMap::at_key($key) },
STORE => $store
)
!! Proxy.new(
FETCH => sub (|$) { die "No such hash key '$key'" },
STORE => $store,
);
}
}
my %h;
%h does safety;
%h = a => 1;
say %h<a>;
%h<a> = 5;
say %h<a>;
%h<b> = 10;
say %h<b>;
say %h<x>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment