line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Hash::Subset; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY |
4
|
|
|
|
|
|
|
our $DATE = '2020-06-11'; # DATE |
5
|
|
|
|
|
|
|
our $DIST = 'Hash-Subset'; # DIST |
6
|
|
|
|
|
|
|
our $VERSION = '0.006'; # VERSION |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
56112
|
use strict; |
|
1
|
|
|
|
|
10
|
|
|
1
|
|
|
|
|
25
|
|
9
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
23
|
|
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
4
|
use Exporter qw(import); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
388
|
|
12
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
13
|
|
|
|
|
|
|
hash_subset |
14
|
|
|
|
|
|
|
hashref_subset |
15
|
|
|
|
|
|
|
hash_subset_without |
16
|
|
|
|
|
|
|
hashref_subset_without |
17
|
|
|
|
|
|
|
); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub _routine { |
20
|
14
|
|
|
14
|
|
29
|
my ($which, $hash, @keys_srcs) = @_; |
21
|
|
|
|
|
|
|
|
22
|
14
|
|
|
|
|
43
|
my $reverse = $which =~ /_without\z/; |
23
|
14
|
|
|
|
|
25
|
my $return_ref = $which =~ /\Ahashref_/; |
24
|
|
|
|
|
|
|
|
25
|
14
|
|
|
|
|
18
|
my %subset; |
26
|
14
|
100
|
|
|
|
41
|
%subset = %$hash if $reverse; |
27
|
|
|
|
|
|
|
|
28
|
14
|
|
|
|
|
22
|
for my $keys_src (@keys_srcs) { |
29
|
18
|
|
|
|
|
27
|
my $ref = ref $keys_src; |
30
|
18
|
100
|
|
|
|
40
|
if ($ref eq 'ARRAY') { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
31
|
6
|
100
|
|
|
|
7
|
if ($reverse) { |
32
|
3
|
|
|
|
|
5
|
for (@$keys_src) { |
33
|
5
|
|
|
|
|
8
|
delete $subset{$_}; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
} else { |
36
|
3
|
|
|
|
|
5
|
for (@$keys_src) { |
37
|
5
|
50
|
|
|
|
15
|
$subset{$_} = $hash->{$_} if exists $hash->{$_}; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
} elsif ($ref eq 'HASH') { |
41
|
6
|
100
|
|
|
|
10
|
if ($reverse) { |
42
|
3
|
|
|
|
|
6
|
for (keys %$keys_src) { |
43
|
7
|
|
|
|
|
10
|
delete $subset{$_}; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
} else { |
46
|
3
|
|
|
|
|
9
|
for (keys %$keys_src) { |
47
|
7
|
100
|
|
|
|
15
|
$subset{$_} = $hash->{$_} if exists $hash->{$_}; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
} elsif ($ref eq 'CODE') { |
51
|
6
|
100
|
|
|
|
8
|
if ($reverse) { |
52
|
3
|
|
|
|
|
6
|
for (keys %$hash) { |
53
|
10
|
100
|
|
|
|
37
|
delete $subset{$_} if $keys_src->($_, $hash->{$_}); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
} else { |
56
|
3
|
|
|
|
|
8
|
for (keys %$hash) { |
57
|
10
|
100
|
|
|
|
39
|
$subset{$_} = $hash->{$_} if $keys_src->($_, $hash->{$_}); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
} else { |
61
|
0
|
|
|
|
|
0
|
die "Key source ($keys_src) must be a hashref/arrayref/coderef"; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} # for $keys_src |
64
|
|
|
|
|
|
|
|
65
|
14
|
100
|
|
|
|
39
|
if ($return_ref) { |
66
|
6
|
|
|
|
|
28
|
return \%subset; |
67
|
|
|
|
|
|
|
} else { |
68
|
8
|
|
|
|
|
49
|
return %subset; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
4
|
|
|
4
|
1
|
76
|
sub hash_subset { _routine('hash_subset' , @_) } |
73
|
3
|
|
|
3
|
1
|
7
|
sub hashref_subset { _routine('hashref_subset', @_) } |
74
|
4
|
|
|
4
|
1
|
9
|
sub hash_subset_without { _routine('hash_subset_without' , @_) } |
75
|
3
|
|
|
3
|
1
|
7
|
sub hashref_subset_without { _routine('hashref_subset_without', @_) } |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
1; |
78
|
|
|
|
|
|
|
# ABSTRACT: Produce subset of a hash |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
__END__ |