line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HashDataRole::Source::Hash; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY |
4
|
|
|
|
|
|
|
our $DATE = '2021-05-21'; # DATE |
5
|
|
|
|
|
|
|
our $DIST = 'HashDataRoles-Standard'; # DIST |
6
|
|
|
|
|
|
|
our $VERSION = '0.001'; # VERSION |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
873
|
use 5.010001; |
|
1
|
|
|
|
|
20
|
|
9
|
1
|
|
|
1
|
|
6
|
use Role::Tiny; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
10
|
1
|
|
|
1
|
|
218
|
use Role::Tiny::With; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
697
|
|
11
|
|
|
|
|
|
|
with 'HashDataRole::Spec::Basic'; |
12
|
|
|
|
|
|
|
with 'Role::TinyCommons::Collection::GetItemByPos'; # bonus |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new { |
15
|
1
|
|
|
1
|
0
|
113
|
my ($class, %args) = @_; |
16
|
|
|
|
|
|
|
|
17
|
1
|
50
|
|
|
|
6
|
my $hash = delete $args{hash} or die "Please specify 'hash' argument"; |
18
|
|
|
|
|
|
|
|
19
|
1
|
50
|
|
|
|
6
|
die "Unknown argument(s): ". join(", ", sort keys %args) |
20
|
|
|
|
|
|
|
if keys %args; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# cache keys for iteration |
23
|
1
|
|
|
|
|
3
|
my $keys = []; |
24
|
1
|
|
|
|
|
9
|
for my $key (sort keys %$hash) { |
25
|
3
|
|
|
|
|
8
|
push @$keys, $key; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
bless { |
29
|
1
|
|
|
|
|
9
|
hash => $hash, |
30
|
|
|
|
|
|
|
_keys => $keys, |
31
|
|
|
|
|
|
|
pos => 0, |
32
|
|
|
|
|
|
|
}, $class; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub get_next_item { |
36
|
5
|
|
|
5
|
0
|
57
|
my $self = shift; |
37
|
5
|
100
|
|
|
|
9
|
die "StopIteration" unless $self->{pos} < @{ $self->{_keys} }; |
|
5
|
|
|
|
|
65
|
|
38
|
4
|
|
|
|
|
18
|
my $key = $self->{_keys}->[ $self->{pos}++ ]; |
39
|
4
|
|
|
|
|
29
|
[$key, $self->{hash}{$key}]; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub has_next_item { |
43
|
2
|
|
|
2
|
0
|
9
|
my $self = shift; |
44
|
2
|
|
|
|
|
4
|
$self->{pos} < @{ $self->{_keys} }; |
|
2
|
|
|
|
|
15
|
|
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub reset_iterator { |
48
|
2
|
|
|
2
|
0
|
11
|
my $self = shift; |
49
|
2
|
|
|
|
|
9
|
$self->{pos} = 0; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub get_iterator_pos { |
53
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
54
|
0
|
|
|
|
|
0
|
$self->{pos}; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub get_item_count { |
58
|
1
|
|
|
1
|
0
|
4
|
my $self = shift; |
59
|
1
|
|
|
|
|
2
|
scalar @{ $self->{_keys} }; |
|
1
|
|
|
|
|
7
|
|
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub get_item_at_pos { |
63
|
2
|
|
|
2
|
0
|
36
|
my ($self, $pos) = @_; |
64
|
2
|
50
|
|
|
|
8
|
if ($pos < 0) { |
65
|
0
|
0
|
|
|
|
0
|
die "Out of range" unless -$pos <= @{ $self->{_keys} }; |
|
0
|
|
|
|
|
0
|
|
66
|
|
|
|
|
|
|
} else { |
67
|
2
|
100
|
|
|
|
3
|
die "Out of range" unless $pos < @{ $self->{_keys} }; |
|
2
|
|
|
|
|
15
|
|
68
|
|
|
|
|
|
|
} |
69
|
1
|
|
|
|
|
3
|
my $key = $self->{_keys}->[$pos]; |
70
|
1
|
|
|
|
|
7
|
[$key, $self->{hash}{$key}]; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub has_item_at_pos { |
74
|
2
|
|
|
2
|
0
|
6
|
my ($self, $pos) = @_; |
75
|
2
|
50
|
|
|
|
9
|
if ($pos < 0) { |
76
|
0
|
0
|
|
|
|
0
|
return -$pos <= @{ $self->{_keys} } ? 1:0; |
|
0
|
|
|
|
|
0
|
|
77
|
|
|
|
|
|
|
} else { |
78
|
2
|
100
|
|
|
|
4
|
return $pos < @{ $self->{_keys} } ? 1:0; |
|
2
|
|
|
|
|
13
|
|
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub get_item_at_key { |
83
|
2
|
|
|
2
|
0
|
51
|
my ($self, $key) = @_; |
84
|
2
|
100
|
|
|
|
20
|
die "No such key '$key'" unless exists $self->{hash}{$key}; |
85
|
1
|
|
|
|
|
7
|
$self->{hash}{$key}; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub has_item_at_key { |
89
|
2
|
|
|
2
|
0
|
5
|
my ($self, $key) = @_; |
90
|
2
|
|
|
|
|
12
|
exists $self->{hash}{$key}; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub get_all_keys { |
94
|
0
|
|
|
0
|
0
|
|
my ($self, $key) = @_; |
95
|
0
|
|
|
|
|
|
@{$self->{_keys}}; |
|
0
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
1; |
99
|
|
|
|
|
|
|
# ABSTRACT: Get hash data from a Perl hash |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
__END__ |