sub set_config_value { my($config, $name, $value) = @_; my @nesting = split /-/, $name; while (@nesting > 1) { my $field = shift @nesting; # make sure the next level is initialized to a hashref $config->{$field} //= {}; # now point to it $config = $config->{$field}; } # store at the bottom level $config->{$nesting[0]} = $value; } # use like this: set_config_value(\%config, "address-virtual-email", "rpaskudniak");