NAME Hash::Subset - Produce subset of a hash VERSION This document describes version 0.004 of Hash::Subset (from Perl distribution Hash-Subset), released on 2019-09-20. SYNOPSIS use Hash::Subset qw(hash_subset hashref_subset); # 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} 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); my $subset = hashref_subset(\%hash, \@keys); my %subset = hash_subset (\%hash, \%another_hash); my $subset = hashref_subset(\%hash, \%another_hash); # filter_sub is called with args ($key, $value) and return true when key should be included my %subset = hash_subset (\%hash, \&filter_sub); my $subset = hashref_subset(\%hash, \&filter_sub); 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. hashref_subset See "hash_subset". HOMEPAGE Please visit the project's homepage at . SOURCE Source repository is at . BUGS Please report any bugs or feature requests on the bugtracker website 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::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 COPYRIGHT AND LICENSE This software is copyright (c) 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.