Identifier/Wellknown.pm

filter: Statementstmt 92.8% 142 / 153 Branchbran 83.3% 55 / 66 Conditioncond 58.3% 28 / 48 MC/DCmcdc 27.5% 8 / 29 Subroutinesub 86.6% 13 / 15 Podpod 100% 2 / 2 totaltotal 79.2% 248 / 313 CC 59 SCAR 36.9
CC
59
Cov
84%
import
55.7
_known_provider
9.8
classes_of
6.0
CRAP 40.2
« Identifier/Util.pm ↑ Summary Identifier/Interface/Userdata.pm »
1 # Copyright (c) 2023-2026 Philipp Schafft
2  
3 # licensed under Artistic License 2.0 (see LICENSE file)
4  
5 # ABSTRACT: format independent identifier object
6  
7  
8 package Data::Identifier::Wellknown;
9  
10 2 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Fcntl qw(SEEK_SET);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶use Data::Identifier;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Data::Identifier::Generate;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 ▶use parent 'Data::Identifier::Interface::Known';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 our $VERSION = v0.38;
24  
25 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 WK_UUID => '8be115d2-dc2f-4a98-91e1-a6e3075cbc31', # uuid
27 WK_SID => 'f87a38cb-fd13-4e15-866c-e49901adbec5', # small-identifier
28 WK_SNI => '039e0bb7-5dd3-40ee-a98c-596ff6cce405', # sirtx-numerical-identifier
29 WK_CHAT0W => '2c7e15ed-aa2f-4e2f-9a1d-64df0c85875a', # chat-0-word-identifier
30 2 };
31  
32 2 my %imported;
33 2 my %loaded = (wellknown => undef, registered => undef); # consider two loaded to begin with.
34 2 my $start_of_data = DATA->tell;
35  
36 2 foreach my $class (keys %loaded) {
37 4 foreach my $id (Data::Identifier->known($class)) {
38 248 _add_classes($id, $class);
39 }
40 }
41  
42 1 ▶sub _known_provider {
Subroutine _known_provider: called CC=6 SCAR=22.9
43 1 my ($pkg, $class, %opts) = @_;
44 1 ▶ croak 'Unsupported options passed' if scalar(keys %opts);
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %opts 0 1 1
45  
46 1 ▶ unless (exists $loaded{$class}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $loaded{$class}) 0 1 1
47 0 __PACKAGE__->import($class);
48 }
49  
50 1 ▶ if ($class eq 'wellknown' || $class eq 'registered' || $class eq ':all') {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq 'wellknown' or $class eq 'registered' or $class eq ':all') { } 1 0 1
Condition33%2/6
$class eq "wellknown" or $class eq "registered" or $class eq ":all"
l!l&&r!l&&!r
010
$class eq "wellknown" or $class eq "registered"
l!l&&r!l&&!r
001
MC/DC0%0/3
$class eq "wellknown" or $class eq "registered" or $class eq ":all"
$class eq "wellknown" $class eq "registered" $class eq ":all"
Truth table25%1/4
A or B or C
A= $class eq "wellknown"B= $class eq "registered"C= $class eq ":all"
ABCresult
1XX1
01X1
0011
0000
51 1 goto &Data::Identifier::_known_provider;
52 } else {
53 0 return ($imported{$class}, rawtype => 'Data::Identifier');
54 }
55 }
56  
57 3 ▶sub import {
Subroutine import: called CC=51 SCAR=40.2
58 3 my ($pkg, @args) = @_;
59 3 my $id_type = Data::Identifier->new(wellknown => 'uuid');
60 3 my $default_class;
61 my %generator;
62 3 my $namespace;
63 3 my %found;
64 3 my @extra_classes;
65 3 my $displayname_is_tagname;
66  
67 3 ▶ return if exists $loaded{':all'}; # if we done :all we ... did all!
Branch: 1 branch
BranchTrueFalseTotal
if exists $loaded{":all"} 0 3 3
68  
69 3 @args = grep {!exists($loaded{$_})} @args;
70  
71 3 ▶ return unless scalar @args;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @args 2 1 3
72  
73 1 DATA->seek($start_of_data, SEEK_SET);
74  
75 1 ▶ while (my $line = <DATA>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline DATA)) 1254 0 1254
76 1254 my ($classes, $id, $displayname, $special);
77 1254 my @classes;
78 1254 my $found;
79  
80 1254 $line =~ s/\s{2,}%.*$//;
81 1254 $line =~ s/^\s*%.*$//;
82 1254 $line =~ s/\s+$//;
83 1254 $line =~ s/^\s+//;
84 1254 $line =~ s/\r?\n$//;
85  
86 1254 ▶ next if $line eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "" 195 1059 1254
87  
88 1059 ▶ if ($line =~ /^\$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\$/u) 134 925 1059
89 134 my ($command, $arg) = split(/\s+/, $line, 2);
90
91 134 ▶ if ($command eq '$type') {
Branch: 7 branches
BranchTrueFalseTotal
if ($command eq '$type') { } 28 106 134
elsif ($command eq '$class') { } 33 73 106
elsif ($command eq '$extra_classes') { } 26 47 73
elsif ($command eq '$displayname_is_tagname') { } 34 13 47
elsif ($command eq '$generator') { } 6 7 13
elsif ($command eq '$namespace') { } 6 1 7
elsif ($command eq '$end') { } 1 0 1
92 28 ▶ if ($arg =~ /^(.+)=(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /^(.+)=(.+)$/u) { } 3 25 28
93 3 $id_type = Data::Identifier->new($1, $2);
94 } else {
95 25 $id_type = Data::Identifier->new(wellknown => $arg);
96 }
97 28 $namespace = undef;
98 } elsif ($command eq '$class') {
99 33 $default_class = $arg;
100 33 @extra_classes = ();
101 33 $displayname_is_tagname = undef;
102 } elsif ($command eq '$extra_classes') {
103 26 @extra_classes = split(/,/, $arg);
104 } elsif ($command eq '$displayname_is_tagname') {
105 34 $displayname_is_tagname = $arg eq 'true';
106 } elsif ($command eq '$generator') {
107 6 %generator = split(/[,=]/, $arg);
108 6 $id_type = undef;
109 6 $namespace = undef;
110 6 foreach my $key (qw(generator namespace)) {
111 12 ▶ next unless defined $generator{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $generator{$key} 0 12 12
112 12 $generator{$key} = Data::Identifier->new(from => $generator{$key})->register;
113 }
114 } elsif ($command eq '$namespace') {
115 6 $namespace = $arg;
116 } elsif ($command eq '$end') {
117 1 last;
118 } else {
119 0 croak 'BUG';
120 }
121 133 next;
122 }
123  
124 925 ($classes, $id, $displayname, $special) = split(/\s{2,}/, $line, 4);
125 925 ▶ if ($classes eq '.') {
Branch: 1 branch
BranchTrueFalseTotal
if ($classes eq '.') { } 925 0 925
126 925 @classes = ($default_class);
127 } else {
128 0 @classes = split(',', $classes);
129 }
130 925 ▶ if (defined($displayname)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $displayname) 856 69 925
131 856 ▶ $displayname = undef if $displayname eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $displayname eq "." 16 840 856
132 }
133  
134 925 push(@classes, @extra_classes);
135  
136 925 foreach my $class_a (@classes) {
137 1364 foreach my $class_b (@args) {
138 1364 ▶ if ($class_a eq $class_b || $class_b eq ':all') {
Branch: 1 branch
BranchTrueFalseTotal
if ($class_a eq $class_b or $class_b eq ":all") 1364 0 1364
Condition33%1/3
$class_a eq $class_b or $class_b eq ":all"
l!l&&r!l&&!r
013640
MC/DC0%0/2
$class_a eq $class_b or $class_b eq ":all"
$class_a eq $class_b $class_b eq ":all"
Truth table33%1/3
A or B
A= $class_a eq $class_bB= $class_b eq ":all"
ABresult
1X1
011
000
139 1364 $found = 1;
140 1364 last;
141 }
142 }
143 }
144 925 ▶ next unless $found;
Branch: 1 branch
BranchTrueFalseTotal
unless $found 0 925 925
145  
146 {
147 925 my $identifier;
148  
149 925 ▶ if (defined $id_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id_type) { } 828 97 925
150 828 ▶ $identifier = Data::Identifier->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $displayname ? : 814 14 828
151 $id_type => $id,
152 defined($displayname) ? (displayname => $displayname) : (),
153 );
154 } else {
155 97 ▶ $identifier = Data::Identifier::Generate->generic(
Branch: 1 branch
BranchTrueFalseTotal
defined $displayname ? : 26 71 97
156 %generator,
157 request => $id,
158 defined($displayname) ? (displayname => $displayname) : (),
159 );
160 }
161  
162 925 ▶ if (defined $namespace) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $namespace) 292 633 925
163 292 my $uuid;
164  
165 292 ▶ if ($namespace =~ /^(.+),lc$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($namespace =~ /^(.+),lc$/u) { } 12 280 292
166 12 $uuid = Data::Identifier::Generate->_uuid_v5($1, lc($id));
167 } else {
168 280 $uuid = Data::Identifier::Generate->_uuid_v5($namespace, $id);
169 }
170 292 ▶ $identifier->{id_cache} //= {};
Condition50%1/2
$identifier->{"id_cache"} //= {}
l!l
0292
MC/DC0%0/1
$identifier->{"id_cache"} //= {}
$identifier->{"id_cache"}
Truth table50%1/2
A
A= $identifier->{"id_cache"}
Aresult
11
00
171 292 ▶ $identifier->{id_cache}->{WK_UUID()} //= $uuid;
Condition33%1/3
$identifier->{"id_cache"}{"8be115d2-dc2f-4a98-91e1-a6e3075cbc31"} //= $uuid
l!l&&r!l&&!r
02920
MC/DC0%0/2
$identifier->{"id_cache"}{"8be115d2-dc2f-4a98-91e1-a6e3075cbc31"} //= $uuid
$identifier->{"id_cache"}{"8be115d2-dc2f-4a98-91e1-a6e3075cbc31"} $uuid
Truth table33%1/3
A //= B
A= $identifier->{"id_cache"}{"8be115d2-dc2f-4a98-91e1-a6e3075cbc31"}B= $uuid
ABresult
1X1
011
000
172 }
173  
174 925 ▶ if (defined($special) && length($special)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $special and length $special) 202 723 925
Condition66%2/3
defined $special and length $special
!ll&&!rl&&r
7230202
MC/DC50%1/2
defined $special and length $special
defined $special length $special
Truth table66%2/3
A and B
A= defined $specialB= length $special
ABresult
0X0
100
111
175 202 my %special = split(/[,=]/, $special);
176  
177 202 ▶ if (defined $special{sid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $special{"sid"}) 185 17 202
178 185 ▶ $identifier->{id_cache} //= {};
Condition100%2/2
$identifier->{"id_cache"} //= {}
l!l
76109
MC/DC100%1/1
$identifier->{"id_cache"} //= {}
$identifier->{"id_cache"}
Truth table100%2/2
A
A= $identifier->{"id_cache"}
Aresult
11
00
179 185 ▶ $identifier->{id_cache}->{WK_SID()} //= $special{sid};
Condition66%2/3
$identifier->{"id_cache"}{"f87a38cb-fd13-4e15-866c-e49901adbec5"} //= $special{"sid"}
l!l&&r!l&&!r
361490
MC/DC0%0/2
$identifier->{"id_cache"}{"f87a38cb-fd13-4e15-866c-e49901adbec5"} //= $special{"sid"}
$identifier->{"id_cache"}{"f87a38cb-fd13-4e15-866c-e49901adbec5"} $special{"sid"}
Truth table66%2/3
A //= B
A= $identifier->{"id_cache"}{"f87a38cb-fd13-4e15-866c-e49901adbec5"}B= $special{"sid"}
ABresult
1X1
011
000
180 }
181  
182 202 ▶ if (defined $special{sni}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $special{"sni"}) 21 181 202
183 21 ▶ $identifier->{id_cache} //= {};
Condition100%2/2
$identifier->{"id_cache"} //= {}
l!l
138
MC/DC100%1/1
$identifier->{"id_cache"} //= {}
$identifier->{"id_cache"}
Truth table100%2/2
A
A= $identifier->{"id_cache"}
Aresult
11
00
184 21 ▶ $identifier->{id_cache}->{WK_SNI()} //= $special{sni};
Condition66%2/3
$identifier->{"id_cache"}{"039e0bb7-5dd3-40ee-a98c-596ff6cce405"} //= $special{"sni"}
l!l&&r!l&&!r
8130
MC/DC0%0/2
$identifier->{"id_cache"}{"039e0bb7-5dd3-40ee-a98c-596ff6cce405"} //= $special{"sni"}
$identifier->{"id_cache"}{"039e0bb7-5dd3-40ee-a98c-596ff6cce405"} $special{"sni"}
Truth table66%2/3
A //= B
A= $identifier->{"id_cache"}{"039e0bb7-5dd3-40ee-a98c-596ff6cce405"}B= $special{"sni"}
ABresult
1X1
011
000
185 }
186  
187 202 ▶ if (defined $special{chat0w}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $special{"chat0w"}) 24 178 202
188 24 ▶ $identifier->{id_cache} //= {};
Condition100%2/2
$identifier->{"id_cache"} //= {}
l!l
177
MC/DC100%1/1
$identifier->{"id_cache"} //= {}
$identifier->{"id_cache"}
Truth table100%2/2
A
A= $identifier->{"id_cache"}
Aresult
11
00
189 24 ▶ $identifier->{id_cache}->{WK_CHAT0W()} //= $special{chat0w};
Condition33%1/3
$identifier->{"id_cache"}{"2c7e15ed-aa2f-4e2f-9a1d-64df0c85875a"} //= $special{"chat0w"}
l!l&&r!l&&!r
0240
MC/DC0%0/2
$identifier->{"id_cache"}{"2c7e15ed-aa2f-4e2f-9a1d-64df0c85875a"} //= $special{"chat0w"}
$identifier->{"id_cache"}{"2c7e15ed-aa2f-4e2f-9a1d-64df0c85875a"} $special{"chat0w"}
Truth table33%1/3
A //= B
A= $identifier->{"id_cache"}{"2c7e15ed-aa2f-4e2f-9a1d-64df0c85875a"}B= $special{"chat0w"}
ABresult
1X1
011
000
190 }
191 }
192  
193 925 ▶ if ($displayname_is_tagname && defined($displayname)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($displayname_is_tagname and defined $displayname) { } 443 482 925
Condition100%3/3
$displayname_is_tagname and defined $displayname
!ll&&!rl&&r
46913443
MC/DC100%2/2
$displayname_is_tagname and defined $displayname
$displayname_is_tagname defined $displayname
Truth table100%3/3
A and B
A= $displayname_is_tagnameB= defined $displayname
ABresult
0X0
100
111
194 443 $identifier->_add_tagnames($displayname);
195 } else {
196 482 ▶ $identifier->{displayname} //= $displayname; # force-update, using internal API. DO NOT TRY AT HOME!
Condition66%2/3
$identifier->{"displayname"} //= $displayname
l!l&&r!l&&!r
455027
MC/DC50%1/2
$identifier->{"displayname"} //= $displayname
$identifier->{"displayname"} $displayname
Truth table66%2/3
A //= B
A= $identifier->{"displayname"}B= $displayname
ABresult
1X1
011
000
197 }
198 925 $identifier->register;
199  
200 925 foreach my $class (@classes) {
201 1364 $found{$class} = undef;
202 1364 ▶ $imported{$class} //= [];
Condition100%2/2
$imported{$class} //= []
l!l
132044
MC/DC100%1/1
$imported{$class} //= []
$imported{$class}
Truth table100%2/2
A
A= $imported{$class}
Aresult
11
00
203 1364 push(@{$imported{$class}}, $identifier);
204 }
205  
206 925 _add_classes($identifier, @classes);
207 }
208 }
209  
210 # deduplicate:
211 1 foreach my $class (keys %found) {
212 44 my %tmp = map {$_ => $_} @{$imported{$class}};
213 44 @{$imported{$class}} = values %tmp;
214 }
215  
216 # Mark classes loaded that we found data for.
217 1 foreach my $class (@args) {
218 1 ▶ if (exists($found{$class}) || $class eq ':all') {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $found{$class} or $class eq ':all') { } 1 0 1
Condition33%1/3
exists $found{$class} or $class eq ":all"
l!l&&r!l&&!r
010
MC/DC0%0/2
exists $found{$class} or $class eq ":all"
exists $found{$class} $class eq ":all"
Truth table33%1/3
A or B
A= exists $found{$class}B= $class eq ":all"
ABresult
1X1
011
000
219 1 $loaded{$class} = undef;
220 } else {
221 0 croak 'Unsupported class: '.$class;
222 }
223 }
224 }
225  
226  
227 0 ▶sub classes_of {
Subroutine classes_of: not called CC=2 SCAR=17.9
Pod documented
228 0 my ($pkg, $identifier) = @_;
229 0 $identifier = Data::Identifier->new(from => $identifier);
230  
231 0 ▶ return keys %{$identifier->userdata(__PACKAGE__, 'classes') // {}};
Condition0%0/2
$identifier->userdata(__PACKAGE__, "classes") // {}
l!l
00
MC/DC0%0/1
$identifier->userdata(__PACKAGE__, "classes") // {}
$identifier->userdata(__PACKAGE__, "classes")
Truth table0%0/2
A
A= $identifier->userdata(__PACKAGE__, "classes")
Aresult
11
00
232 }
233  
234  
235 0 ▶sub announce {
Subroutine announce: not called CC=1 SCAR=6.9
Pod documented
236 0 require Data::Identifier::Util;
237 0 goto &Data::Identifier::Util::announce;
238 }
239  
240 # ---- Private helpers ----
241  
242 1173 ▶sub _add_classes {
Subroutine _add_classes: called CC=3 SCAR=11.1
243 1173 my ($identifier, @classes) = @_;
244 1173 ▶ my $set = $identifier->userdata(__PACKAGE__, 'classes') // $identifier->userdata(__PACKAGE__, 'classes' => {});
Condition66%2/3
$identifier->userdata(__PACKAGE__, "classes") // $identifier->userdata(__PACKAGE__, "classes", {})
l!l&&r!l&&!r
1779960
MC/DC0%0/2
$identifier->userdata(__PACKAGE__, "classes") // $identifier->userdata(__PACKAGE__, "classes", {})
$identifier->userdata(__PACKAGE__, "classes") $identifier->userdata(__PACKAGE__, "classes", {})
Truth table66%2/3
A // B
A= $identifier->userdata(__PACKAGE__, "classes")B= $identifier->userdata(__PACKAGE__, "classes", {})
ABresult
1X1
011
000
245 1173 $set->{$_} = undef foreach @classes, ':all', 'registered';
246 }
247  
248 2 1;
249  
250 =pod
251  
252 =encoding UTF-8
253  
254 =head1 NAME
255  
256 Data::Identifier::Wellknown - format independent identifier object
257  
258 =head1 VERSION
259  
260 version v0.38
261  
262 =head1 SYNOPSIS
263  
264 use Data::Identifier::Wellknown qw(classes...);
265 e.g.:
266 use Data::Identifier::Wellknown ':all';
267  
268 (experimental since v0.07)
269  
270 This package provides a simple list of well known identifiers.
271 Classes are loaded on demand. However for speedy lookup classes can
272 be imported (given via C<use> arguments).
273  
274 If a class is already loaded, it is not reloaded.
275 If a program knows the classes it will use early it makes sense to
276 include this module in the main program (or root module) before other modules that make
277 use of this module are used with all the used classes listed.
278 This improves speed as it will reduce the read of the full list to a single pass.
279 In contrast if every use will only list a single class that is not yet loaded loading will be most in-efficient.
280  
281 B<Note:>
282 This is an B<experimental> package.
283 It's methods and classes are not stable.
284 But the package itself and the class C<:all> is.
285  
286 This package implements L<Data::Identifier::Interface::Known>.
287  
288 =head1 METHODS
289  
290 =head2 classes_of
291  
292 my @classes = Data::Identifier::Wellknown->classes_of($identifier);
293  
294 (experimental since v0.07)
295  
296 Returns the classes the identifier is known for.
297 C<$identifier> is parsed as per C<from> of L<Data::Identifier/new>.
298  
299 B<Note:>
300 This module does not guarantee any specific order of the returned list.
301  
302 B<Note:>
303 Classes may not be included in returned list unless they (or C<:all>) have been
304 imported before.
305  
306 B<Note:>
307 This is an B<experimental> method. It may be changed, renamed, or removed without notice.
308  
309 =head2 announce
310  
311 Data::Identifier::Wellknown->announce($type => $data);
312  
313 (experimental since v0.26, moved in v0.37 to L<Data::Identifier::Util>, will be removed in v0.40)
314  
315 This method was moved to L<Data::Identifier::Util/announce> in v0.37.
316 The new method has the same interface as this method had.
317  
318 This module has a proxy method to aid migration.
319 The proxy method will be removed in v0.40.
320  
321 =head1 AUTHOR
322  
323 Philipp Schafft <lion@cpan.org>
324  
325 =head1 COPYRIGHT AND LICENSE
326  
327 This software is Copyright (c) 2023-2026 by Philipp Schafft <lion@cpan.org>.
328  
329 This is free software, licensed under:
330  
331 The Artistic License 2.0 (GPL Compatible)
332  
333 =cut
334  
335 __DATA__
« Identifier/Util.pm ↑ Summary Identifier/Interface/Userdata.pm »