NAME

Hash::Subset - Produce subset of a hash

VERSION

This document describes version 0.006 of Hash::Subset (from Perl distribution Hash-Subset), released on 2020-06-11.

SYNOPSIS

use Hash::Subset qw(
    hash_subset
    hashref_subset
    hash_subset_without
    hashref_subset_without
);

# using keys specified in an array
my %subset = hash_subset   ({a=>1, b=>2, c=>3}, ['b','c','d']); # => (b=>2, c=>3)
my $subset = hashref_subset({a=>1, b=>2, c=>3}, ['b','c','d']); # => {b=>2, c=>3}

# using keys specified in another hash
my %subset = hash_subset   ({a=>1, b=>2, c=>3}, {b=>20, c=>30, d=>40}); # => (b=>2, c=>3)
my $subset = hashref_subset({a=>1, b=>2, c=>3}, {b=>20, c=>30, d=>40}); # => {b=>2, c=>3}

# filtering keys using a coderef
my %subset = hash_subset   ({a=>1, b=>2, c=>3}, sub {$_[0] =~ /[bc]/}); # => (b=>2, c=>3)
my $subset = hashref_subset({a=>1, b=>2, c=>3}, sub {$_[0] =~ /[bc]/}); # => {b=>2, c=>3}

# multiple filters: array, hash, coderef
my %subset = hash_subset   ({a=>1, b=>2, c=>3, d=>4}, {c=>1}, [qw/b/], sub {$_[0] =~ /[bcd]/}); # => (b=>2, c=>3, d=>4)
my $subset = hashref_subset({a=>1, b=>2, c=>3, d=>4}, {c=>1}, [qw/b/], sub {$_[0] =~ /[bcd]/}); # => {b=>2, c=>3, d=>4}

# excluding keys
my %subset = hash_subset_without   ({a=>1, b=>2, c=>3}, ['b','c','d']); # => (a=>1)
my $subset = hashref_subset_without({a=>1, b=>2, c=>3}, ['b','c','d']); # => {a=>1}

A use case is when you use hash arguments:

sub func1 {
    my %args = @_; # known arguments: foo, bar, baz
    ...
}

sub func2 {
    my %args = @_; # known arguments: all func1 arguments as well as qux, quux

    # call func1 with all arguments passed to us
    my $res = func1(hash_subset(\%args, [qw/foo bar baz/]));

    # postprocess result
    ...
}

If you use Rinci metadata in your code, this will come in handy, for example:

my %common_args = (
    foo => {...},
    bar => {...},
    baz => {...},
);

$SPEC{func1} = {
   v => 1.1,
   args => {
       %common_args,
   },
};
sub func1 {
    my %args = @_;
    ...
}

$SPEC{func2} = {
   v => 1.1,
   args => {
       %common_args,
       # func2 supports all func1 arguments plus a couple of others
       qux  => { ... },
       quux => { ... },
   },
};
sub func2 {
    my %args = @_;

    # call func1 with all arguments passed to us
    my $res = func1(hash_subset(\%args, $SPEC{func1}{args}));

    # postprocess result
    ...
}

DESCRIPTION

Keywords: hash arguments, hash picking, hash grep, hash filtering

FUNCTIONS

None exported by default.

hash_subset

Usage:

my %subset  = hash_subset   (\%hash, @keys_srcs);
my $subset  = hashref_subset(\%hash, @keys_srcs);

Where @keys_src elements can be arrayref, hashref, or coderef. Coderef will be called with args($key, $value) and return true when key should be included.

Produce subset of %hash, returning the subset hash (or hashref, in the case of hashref_subset function).

Perl lets you produce a hash subset using the hash slice notation:

my %subset = %hash{"b","c","d"};

The difference with hash_subset is: 1) hash slice is only available since perl 5.20 (in previous versions, only array slice is available); 2) when the key does not exist in the array, perl will create it for you with undef as the value:

my %hash   = (a=>1, b=>2, c=>3);
my %subset = %hash{"b","c","d"}; # => (b=>2, c=>3, d=>undef)

So basically hash_subset is equivalent to:

my %subset = %hash{grep {exists $hash{$_}} "b","c","d"}; # => (b=>2, c=>3)

and available for perl earlier than 5.20. In addition to that, hash_subset() accepts arrayref as well as hashref/coderef, and several of them.

hashref_subset

See "hash_subset".

hash_subset_without

Like "hash_subset", but reverses the logic: will create subset that only includes keys not in the specified arrays/hashes/coderefs.

hashref_subset_without

See "hash_subset_without".

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/Hash-Subset.

SOURCE

Source repository is at https://github.com/perlancar/perl-Hash-Subset.

BUGS

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Hash-Subset

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

SEE ALSO

Hash::MoreUtils provides various ways to create hash subset ("slice") through its slice_* functions. It does not provide way to specify subset keys via the keys of %another_hash, but that can be done trivially using keys %another_hash. Hash::Subset is currently more lightweight than Hash::MoreUtils.

Tie::Subset::Hash to create a tied version of a hash subset (a "view" of a subset of a hash).

Hash::Util::Pick also allows you to create a hash subset by specifying the wanted keys in a list or via filtering using a coderef. This XS module should perhaps be preferred over Hash::Subset for its performance, but there are some cases where you cannot use XS modules.

See some benchmarks in Bencher::Scenarios::HashPicking.

AUTHOR

perlancar <perlancar@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2020, 2019 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.