line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test2::Util::ExternalMeta; |
2
|
246
|
|
|
246
|
|
1618
|
use strict; |
|
246
|
|
|
|
|
462
|
|
|
246
|
|
|
|
|
6961
|
|
3
|
246
|
|
|
246
|
|
1179
|
use warnings; |
|
246
|
|
|
|
|
420
|
|
|
246
|
|
|
|
|
10106
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '1.302181'; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
|
8
|
246
|
|
|
246
|
|
1442
|
use Carp qw/croak/; |
|
246
|
|
|
|
|
473
|
|
|
246
|
|
|
|
|
26161
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub META_KEY() { '_meta' } |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our @EXPORT = qw/meta set_meta get_meta delete_meta/; |
13
|
246
|
|
|
246
|
|
1848
|
BEGIN { require Exporter; our @ISA = qw(Exporter) } |
|
246
|
|
|
|
|
100234
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub set_meta { |
16
|
5
|
|
|
5
|
1
|
22
|
my $self = shift; |
17
|
5
|
|
|
|
|
16
|
my ($key, $value) = @_; |
18
|
|
|
|
|
|
|
|
19
|
5
|
|
|
|
|
18
|
validate_key($key); |
20
|
|
|
|
|
|
|
|
21
|
5
|
|
50
|
|
|
33
|
$self->{+META_KEY} ||= {}; |
22
|
5
|
|
|
|
|
19
|
$self->{+META_KEY}->{$key} = $value; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub get_meta { |
26
|
315
|
|
|
315
|
1
|
575
|
my $self = shift; |
27
|
315
|
|
|
|
|
557
|
my ($key) = @_; |
28
|
|
|
|
|
|
|
|
29
|
315
|
|
|
|
|
784
|
validate_key($key); |
30
|
|
|
|
|
|
|
|
31
|
315
|
100
|
|
|
|
734
|
my $meta = $self->{+META_KEY} or return undef; |
32
|
309
|
|
|
|
|
888
|
return $meta->{$key}; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub delete_meta { |
36
|
4
|
|
|
4
|
1
|
21
|
my $self = shift; |
37
|
4
|
|
|
|
|
21
|
my ($key) = @_; |
38
|
|
|
|
|
|
|
|
39
|
4
|
|
|
|
|
15
|
validate_key($key); |
40
|
|
|
|
|
|
|
|
41
|
2
|
50
|
|
|
|
11
|
my $meta = $self->{+META_KEY} or return undef; |
42
|
2
|
|
|
|
|
11
|
delete $meta->{$key}; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub meta { |
46
|
2242
|
|
|
2242
|
1
|
4364
|
my $self = shift; |
47
|
2242
|
|
|
|
|
4627
|
my ($key, $default) = @_; |
48
|
|
|
|
|
|
|
|
49
|
2242
|
|
|
|
|
6076
|
validate_key($key); |
50
|
|
|
|
|
|
|
|
51
|
2240
|
|
|
|
|
4477
|
my $meta = $self->{+META_KEY}; |
52
|
2240
|
100
|
100
|
|
|
6369
|
return undef unless $meta || defined($default); |
53
|
|
|
|
|
|
|
|
54
|
2237
|
100
|
|
|
|
4610
|
unless($meta) { |
55
|
371
|
|
|
|
|
777
|
$meta = {}; |
56
|
371
|
|
|
|
|
974
|
$self->{+META_KEY} = $meta; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
$meta->{$key} = $default |
60
|
2237
|
100
|
100
|
|
|
9136
|
if defined($default) && !defined($meta->{$key}); |
61
|
|
|
|
|
|
|
|
62
|
2237
|
|
|
|
|
6531
|
return $meta->{$key}; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub validate_key { |
66
|
2566
|
|
|
2566
|
0
|
3888
|
my $key = shift; |
67
|
|
|
|
|
|
|
|
68
|
2566
|
100
|
66
|
|
|
11892
|
return if $key && !ref($key); |
69
|
|
|
|
|
|
|
|
70
|
4
|
100
|
|
|
|
28
|
my $render_key = defined($key) ? "'$key'" : 'undef'; |
71
|
4
|
|
|
|
|
397
|
croak "Invalid META key: $render_key, keys must be true, and may not be references"; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
1; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
__END__ |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=pod |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=encoding UTF-8 |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head1 NAME |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
Test2::Util::ExternalMeta - Allow third party tools to safely attach meta-data |
85
|
|
|
|
|
|
|
to your instances. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 DESCRIPTION |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
This package lets you define a clear, and consistent way to allow third party |
90
|
|
|
|
|
|
|
tools to attach meta-data to your instances. If your object consumes this |
91
|
|
|
|
|
|
|
package, and imports its methods, then third party meta-data has a safe place |
92
|
|
|
|
|
|
|
to live. |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head1 SYNOPSIS |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
package My::Object; |
97
|
|
|
|
|
|
|
use strict; |
98
|
|
|
|
|
|
|
use warnings; |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
use Test2::Util::ExternalMeta qw/meta get_meta set_meta delete_meta/; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
... |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Now to use it: |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
my $inst = My::Object->new; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
$inst->set_meta(foo => 'bar'); |
109
|
|
|
|
|
|
|
my $val = $inst->get_meta('foo'); |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 WHERE IS THE DATA STORED? |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
This package assumes your instances are blessed hashrefs, it will not work if |
114
|
|
|
|
|
|
|
that is not true. It will store all meta-data in the C<_meta> key on your |
115
|
|
|
|
|
|
|
objects hash. If your object makes use of the C<_meta> key in its underlying |
116
|
|
|
|
|
|
|
hash, then there is a conflict and you cannot use this package. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head1 EXPORTS |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=over 4 |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=item $val = $obj->meta($key) |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=item $val = $obj->meta($key, $default) |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
This will get the value for a specified meta C<$key>. Normally this will return |
127
|
|
|
|
|
|
|
C<undef> when there is no value for the C<$key>, however you can specify a |
128
|
|
|
|
|
|
|
C<$default> value to set when no value is already set. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=item $val = $obj->get_meta($key) |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
This will get the value for a specified meta C<$key>. This does not have the |
133
|
|
|
|
|
|
|
C<$default> overhead that C<meta()> does. |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=item $val = $obj->delete_meta($key) |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
This will remove the value of a specified meta C<$key>. The old C<$val> will be |
138
|
|
|
|
|
|
|
returned. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=item $obj->set_meta($key, $val) |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
Set the value of a specified meta C<$key>. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=back |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 META-KEY RESTRICTIONS |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Meta keys must be defined, and must be true when used as a boolean. Keys may |
149
|
|
|
|
|
|
|
not be references. You are free to stringify a reference C<"$ref"> for use as a |
150
|
|
|
|
|
|
|
key, but this package will not stringify it for you. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head1 SOURCE |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
The source code repository for Test2 can be found at |
155
|
|
|
|
|
|
|
F<http://github.com/Test-More/test-more/>. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head1 MAINTAINERS |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=over 4 |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=item Chad Granum E<lt>exodist@cpan.orgE<gt> |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=back |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=head1 AUTHORS |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=over 4 |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=item Chad Granum E<lt>exodist@cpan.orgE<gt> |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=back |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=head1 COPYRIGHT |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
Copyright 2020 Chad Granum E<lt>exodist@cpan.orgE<gt>. |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or |
178
|
|
|
|
|
|
|
modify it under the same terms as Perl itself. |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
See F<http://dev.perl.org/licenses/> |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=cut |