the usual
inconsistent at best
Perl Hash Reference Partial Copy

Given a multi-level deep hash reference:

my $conf = {
    bucket => {
        list => {
            h    => 'help me',
            _sub => sub { say "something" }
        }
    },
    file => {
        upload => {
            h    => 'help me too',
            _sub => sub { say "else" }
        }
    }
};

We want to remove all of the _sub keys and put them in a separate hash reference with the same structure. This does that:

my $sub = {
    map {
        my $k = $_;
        $k => {
            map { $_ => delete $conf->{$k}->{$_}->{_sub} }
            keys %{ $conf->{$k} }
        }
    } keys %$conf
};

Last modified on 2015-03-28