我正在尝试定制哈希。以下是尝试为配置类哈希实现更简单的查找:
use v6;
class X::Config::KeyNotFound is Exception {
method message() {
"Key not found!";
}
}
# A hash that allows for nested lookup using a '.' to separate keys.
# (This means that keys themselves cannot contain a dot)
# For example:
#
# %h = Config.new(%(a => %(b => 1)));
# my $foo = %h<a.b>; # <-- $foo = 1
#
class Config does Associative[Cool,Str] {
has %.hash;
multi method AT-KEY ( ::?CLASS:D: $key) {
my …Run Code Online (Sandbox Code Playgroud)