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