line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Remember::Class; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$Data::Remember::Class::VERSION = '0.140490'; |
4
|
|
|
|
|
|
|
} |
5
|
11
|
|
|
11
|
|
66197
|
use strict; |
|
11
|
|
|
|
|
22
|
|
|
11
|
|
|
|
|
427
|
|
6
|
11
|
|
|
11
|
|
58
|
use warnings; |
|
11
|
|
|
|
|
23
|
|
|
11
|
|
|
|
|
267
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
# ABSTRACT: remember complex information without giving yourself a headache, now with POOP! |
9
|
|
|
|
|
|
|
|
10
|
11
|
|
|
11
|
|
53
|
use Carp; |
|
11
|
|
|
|
|
26
|
|
|
11
|
|
|
|
|
726
|
|
11
|
11
|
|
|
11
|
|
62
|
use Scalar::Util qw( reftype ); |
|
11
|
|
|
|
|
21
|
|
|
11
|
|
|
|
|
715
|
|
12
|
11
|
|
|
11
|
|
1212
|
use Class::Load (); |
|
11
|
|
|
|
|
85390
|
|
|
11
|
|
|
|
|
429
|
|
13
|
|
|
|
|
|
|
use Data::Remember::Util |
14
|
11
|
|
|
|
|
157
|
process_que => { -as => '_process_que' }, |
15
|
11
|
|
|
11
|
|
7991
|
init_brain => { -as => '_init_brain' }; |
|
11
|
|
|
|
|
41
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub new { |
19
|
11
|
|
|
11
|
1
|
1316
|
my $class = shift; |
20
|
11
|
|
50
|
|
|
197
|
my $brain = shift || 'Memory'; |
21
|
|
|
|
|
|
|
|
22
|
11
|
|
|
|
|
22
|
my $caller = caller; |
23
|
|
|
|
|
|
|
|
24
|
11
|
|
|
|
|
56
|
my $gray_matter = _init_brain($brain, @_); |
25
|
|
|
|
|
|
|
|
26
|
11
|
|
|
|
|
108
|
return bless { brain => $gray_matter }, $class; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub remember { |
31
|
84
|
|
|
84
|
1
|
7120
|
my ($self, $que, $fact) = @_; |
32
|
|
|
|
|
|
|
|
33
|
84
|
|
|
|
|
420
|
my $clean_que = _process_que($que);; |
34
|
|
|
|
|
|
|
|
35
|
84
|
50
|
|
|
|
238
|
unless (defined $clean_que) { |
36
|
0
|
|
|
|
|
0
|
carp "Undefined que element found in call to remember()."; |
37
|
0
|
|
|
|
|
0
|
return; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
84
|
|
|
|
|
415
|
$self->{brain}->remember($clean_que, $fact); |
41
|
|
|
|
|
|
|
|
42
|
84
|
|
|
|
|
135905
|
return; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub remember_these { |
47
|
15
|
|
|
15
|
1
|
34
|
my ($self, $que, $fact) = @_; |
48
|
|
|
|
|
|
|
|
49
|
15
|
|
|
|
|
50
|
my $clean_que = _process_que($que);; |
50
|
|
|
|
|
|
|
|
51
|
15
|
50
|
|
|
|
49
|
unless (defined $clean_que) { |
52
|
0
|
|
|
|
|
0
|
carp "Undefined que element found in call to remember_these()."; |
53
|
0
|
|
|
|
|
0
|
return; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
15
|
|
|
|
|
137
|
my $brain = $self->{brain}; |
57
|
|
|
|
|
|
|
|
58
|
15
|
|
|
|
|
63
|
my $fact_list = $brain->recall($clean_que); |
59
|
|
|
|
|
|
|
|
60
|
15
|
100
|
66
|
|
|
107
|
if (defined reftype $fact_list and reftype $fact_list eq 'ARRAY') { |
61
|
10
|
|
|
|
|
27
|
push @$fact_list, $fact; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
else { |
65
|
5
|
|
|
|
|
25
|
$brain->remember($clean_que, [ $fact ]); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
15
|
|
|
|
|
19766
|
return; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub recall { |
73
|
218
|
|
|
218
|
1
|
1017
|
my ($self, $que) = @_; |
74
|
|
|
|
|
|
|
|
75
|
218
|
|
|
|
|
765
|
my $clean_que = _process_que($que); |
76
|
|
|
|
|
|
|
|
77
|
218
|
50
|
|
|
|
544
|
unless (defined $clean_que) { |
78
|
0
|
|
|
|
|
0
|
carp "Undefined que element used in call to recall()."; |
79
|
0
|
|
|
|
|
0
|
return; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
218
|
|
|
|
|
867
|
return scalar $self->{brain}->recall($clean_que); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub recall_each { |
87
|
10
|
|
|
10
|
1
|
26
|
my ($self, $que) = @_; |
88
|
10
|
|
|
|
|
20
|
my $brain = $self->{brain}; |
89
|
|
|
|
|
|
|
|
90
|
10
|
|
|
|
|
38
|
my $clean_que = _process_que($que); |
91
|
|
|
|
|
|
|
|
92
|
10
|
50
|
|
|
|
42
|
unless (defined $clean_que) { |
93
|
0
|
|
|
|
|
0
|
carp "Undefined que element used in call to recall_each()."; |
94
|
0
|
|
|
|
|
0
|
return; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
10
|
|
|
|
|
42
|
my $value = $brain->recall($clean_que); |
98
|
|
|
|
|
|
|
|
99
|
10
|
|
|
|
|
41
|
my $value_ref_type = reftype($value); |
100
|
10
|
50
|
|
|
|
36
|
$value_ref_type = '' unless defined $value_ref_type; |
101
|
|
|
|
|
|
|
|
102
|
10
|
50
|
|
|
|
33
|
if ($value_ref_type eq 'HASH') { |
|
|
0
|
|
|
|
|
|
103
|
10
|
|
|
|
|
45
|
my @keys = keys %$value; |
104
|
|
|
|
|
|
|
return sub { |
105
|
40
|
100
|
|
40
|
|
12629
|
return unless @keys; |
106
|
30
|
|
|
|
|
63
|
my $k = shift @keys; |
107
|
30
|
|
|
|
|
177
|
return ($k, $value->{$k}); |
108
|
10
|
|
|
|
|
6995
|
}; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
elsif ($value_ref_type eq 'ARRAY') { |
112
|
0
|
|
|
|
|
0
|
my @indexes = 0 .. $#$value; |
113
|
|
|
|
|
|
|
return sub { |
114
|
0
|
0
|
|
0
|
|
0
|
return unless @indexes; |
115
|
0
|
|
|
|
|
0
|
my $i = shift @indexes; |
116
|
0
|
|
|
|
|
0
|
return ($i, $value->[$i]); |
117
|
0
|
|
|
|
|
0
|
}; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
else { |
121
|
0
|
|
|
|
|
0
|
my @values = ($value); |
122
|
|
|
|
|
|
|
return sub { |
123
|
0
|
0
|
|
0
|
|
0
|
return unless @values; |
124
|
0
|
|
|
|
|
0
|
my $v = shift @values; |
125
|
0
|
|
|
|
|
0
|
return (undef, $v); |
126
|
0
|
|
|
|
|
0
|
}; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub recall_and_update { |
132
|
5
|
|
|
5
|
1
|
1022
|
my ($self, $code, $que) = @_; |
133
|
|
|
|
|
|
|
|
134
|
5
|
|
|
|
|
19
|
my $clean_que = _process_que($que); |
135
|
|
|
|
|
|
|
|
136
|
5
|
50
|
|
|
|
26
|
unless (defined $clean_que) { |
137
|
0
|
|
|
|
|
0
|
carp "Undefined que element used in call to recall_and_update()."; |
138
|
0
|
|
|
|
|
0
|
return; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
5
|
|
|
|
|
12
|
my $brain = $self->{brain}; |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
# Recall and modify $_ |
144
|
5
|
|
|
|
|
24
|
local $_ = $brain->recall($clean_que); |
145
|
5
|
|
|
|
|
18
|
my $result = $code->(); |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
# Store that value back |
148
|
5
|
|
|
|
|
50
|
$brain->remember($clean_que, $_); |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
# Return the result |
151
|
5
|
|
|
|
|
1530
|
return $result; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
sub forget { |
156
|
16
|
|
|
16
|
1
|
36
|
my ($self, $que) = @_; |
157
|
|
|
|
|
|
|
|
158
|
16
|
|
|
|
|
56
|
my $clean_que = _process_que($que); |
159
|
|
|
|
|
|
|
|
160
|
16
|
50
|
|
|
|
55
|
unless (defined $clean_que) { |
161
|
0
|
|
|
|
|
0
|
carp "Undefined que element used in call to forget()."; |
162
|
0
|
|
|
|
|
0
|
return; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
16
|
|
|
|
|
94
|
$self->{brain}->forget($clean_que); |
166
|
|
|
|
|
|
|
|
167
|
16
|
|
|
|
|
4153
|
return; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
sub forget_when { |
172
|
40
|
|
|
40
|
1
|
79
|
my ($self, $code, $que) = @_; |
173
|
|
|
|
|
|
|
|
174
|
40
|
|
|
|
|
135
|
my $clean_que = _process_que($que); |
175
|
|
|
|
|
|
|
|
176
|
40
|
50
|
|
|
|
373
|
unless (defined $clean_que) { |
177
|
0
|
|
|
|
|
0
|
carp "Undefined que element used in call to forget_when()."; |
178
|
0
|
|
|
|
|
0
|
return; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
40
|
|
|
|
|
80
|
my $brain = $self->{brain}; |
182
|
40
|
|
|
|
|
153
|
my $fact = $brain->recall($clean_que); |
183
|
|
|
|
|
|
|
|
184
|
40
|
100
|
100
|
|
|
340
|
if (ref $fact and reftype $fact eq 'HASH') { |
|
|
100
|
66
|
|
|
|
|
185
|
10
|
|
|
|
|
47
|
for my $key (keys %$fact) { |
186
|
30
|
|
|
|
|
8836
|
my $value = $fact->{ $key }; |
187
|
30
|
|
|
|
|
5654
|
local $_ = $value; |
188
|
30
|
100
|
|
|
|
76
|
delete $fact->{ $key } if $code->($key, $value); |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
elsif (ref $fact and reftype $fact eq 'ARRAY') { |
193
|
10
|
|
|
|
|
18
|
my $index = 0; |
194
|
|
|
|
|
|
|
my @new_fact |
195
|
10
|
|
|
|
|
33
|
= grep { my $value = $_; not $code->($index++, $value) } @$fact; |
|
30
|
|
|
|
|
9461
|
|
|
30
|
|
|
|
|
83
|
|
196
|
10
|
|
|
|
|
90
|
$brain->remember($clean_que, \@new_fact); |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
else { |
200
|
20
|
|
|
|
|
44
|
local $_ = $fact; |
201
|
20
|
100
|
|
|
|
67
|
$brain->forget($clean_que) if $code->(undef, $fact); |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
|
204
|
40
|
|
|
|
|
38567
|
return; |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
sub brain { |
209
|
12
|
|
|
12
|
1
|
19
|
my $self = shift; |
210
|
12
|
|
|
|
|
71
|
return $self->{brain}; |
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
1; |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
__END__ |