blib/lib/Data/GUID.pm

filter: Statementstmt 100% 111 / 111 Branchbran 100% 24 / 24 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 35 / 35 Podpod 100% 4 / 4 totaltotal 98.9% 180 / 182 CC 18 SCAR 7.7
CC
18
Cov
99%
__ANON__
6.0
__ANON__
3.0
from_data_uuid
3.0
CRAP 2.2
↑ Summary
1 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Data::GUID;
4 # ABSTRACT: globally unique identifiers
5 4 $Data::GUID::VERSION = '0.051';
6 4 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Data::UUID 1.148;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Sub::Install 0.03;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 #pod =head1 SYNOPSIS
11 #pod
12 #pod use Data::GUID;
13 #pod
14 #pod my $guid = Data::GUID->new;
15 #pod
16 #pod my $string = $guid->as_string; # or "$guid"
17 #pod
18 #pod my $other_guid = Data::GUID->from_string($string);
19 #pod
20 #pod if (($guid <=> $other_guid) == 0) {
21 #pod print "They're the same!\n";
22 #pod }
23 #pod
24 #pod =head1 DESCRIPTION
25 #pod
26 #pod Data::GUID provides a simple interface for generating and using globally unique
27 #pod identifiers.
28 #pod
29 #pod =head1 GETTING A NEW GUID
30 #pod
31 #pod =head2 new
32 #pod
33 #pod my $guid = Data::GUID->new;
34 #pod
35 #pod This method returns a new globally unique identifier.
36 #pod
37 #pod =cut
38  
39 4 my $_uuid_gen_obj;
40 my $_uuid_gen_pid;
41 79 ▶my $_uuid_gen = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
42 79 ▶ return $_uuid_gen_obj if $_uuid_gen_obj
Branch: 1 branch
BranchTrueFalseTotal
if $_uuid_gen_obj and $_uuid_gen_pid == $$ 77 2 79
Condition66%2/3
$_uuid_gen_obj and $_uuid_gen_pid == $$
!ll&&!rl&&r
2077
MC/DC50%1/2
$_uuid_gen_obj and $_uuid_gen_pid == $$
$_uuid_gen_obj $_uuid_gen_pid == $$
Truth table66%2/3
A and B
A= $_uuid_gen_objB= $_uuid_gen_pid == $$
ABresult
0X0
100
111
43 && $_uuid_gen_pid == $$;
44  
45 2 $_uuid_gen_pid = $$;
46 2 $_uuid_gen_obj = Data::UUID->new;
47 4 };
48  
49 18 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
50 18 my ($class) = @_;
51  
52 18 return $class->from_data_uuid($_uuid_gen->()->create);
53 }
54  
55 #pod =head1 GUIDS FROM EXISTING VALUES
56 #pod
57 #pod These method returns a new Data::GUID object for the given GUID value. In all
58 #pod cases, these methods throw an exception if given invalid input.
59 #pod
60 #pod =head2 from_string
61 #pod
62 #pod my $guid = Data::GUID->from_string("B0470602-A64B-11DA-8632-93EBF1C0E05A");
63 #pod
64 #pod =head2 from_hex
65 #pod
66 #pod # note that a hex guid is a guid string without hyphens and with a leading 0x
67 #pod my $guid = Data::GUID->from_hex("0xB0470602A64B11DA863293EBF1C0E05A");
68 #pod
69 #pod =head2 from_base64
70 #pod
71 #pod my $guid = Data::GUID->from_base64("sEcGAqZLEdqGMpPr8cDgWg==");
72 #pod
73 #pod =head2 from_data_uuid
74 #pod
75 #pod This method returns a new Data::GUID object if given a Data::UUID value.
76 #pod Because Data::UUID values are not blessed and because Data::UUID provides no
77 #pod validation method, this method will only throw an exception if the given data
78 #pod is of the wrong size.
79 #pod
80 #pod =cut
81  
82 41 ▶sub from_data_uuid {
Subroutine from_data_uuid: called CC=3 SCAR=11.0
Pod documented
83 41 my ($class, $value) = @_;
84  
85 4 ▶ my $length = do { use bytes; defined $value ? length $value : 0; };
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 39 2 41
86 41 ▶ Carp::croak "given value is not a valid Data::UUID value" if $length != 16;
Branch: 1 branch
BranchTrueFalseTotal
if $length != 16 6 35 41
87 35 bless \$value => $class;
88 }
89  
90 #pod =head1 IDENTIFYING GUIDS
91 #pod
92 #pod =head2 string_guid_regex
93 #pod
94 #pod =head2 hex_guid_regex
95 #pod
96 #pod =head2 base64_guid_regex
97 #pod
98 #pod These methods return regex objects that match regex strings of the appropriate
99 #pod type.
100 #pod
101 #pod =cut
102  
103 4 my ($hex, $base64, %type);
104  
105 4 ▶BEGIN { # because %type must be populated for method/exporter generation
Subroutine BEGIN: called CC=2 SCAR=6.9
106 4 $hex = qr/[0-9A-F]/i;
107 4 $base64 = qr{[A-Z0-9+/=]}i;
108  
109 4 %type = ( # uuid_method validation_regex
110 string => [ 'string', qr/\A$hex{8}-?(?:$hex{4}-?){3}$hex{12}\z/, ],
111 hex => [ 'hexstring', qr/\A0x$hex{32}\z/, ],
112 base64 => [ 'b64string', qr/\A$base64{24}\z/, ],
113 );
114  
115 4 for my $key (keys %type) {
116 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
117 12 my $subname = "$key\_guid_regex";
118 7 ▶ *$subname = sub { $type{ $key }[1] }
Subroutine __ANON__: called CC=1 SCAR=0.0
119 12 }
120 }
121  
122 # provided for test scripts
123 12 ▶sub __type_regex { shift; $type{$_[0]}[1] }
Subroutine __type_regex: called CC=1 SCAR=0.0
124  
125 12 ▶sub _install_from_method {
Subroutine _install_from_method: called CC=1 SCAR=0.0
126 12 my ($type, $alien_method, $regex) = @_;
127 12 my $alien_from_method = "from_$alien_method";
128  
129 73 ▶ my $our_from_code = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
130 73 my ($class, $string) = @_;
131 73 ▶ $string ||= q{}; # to avoid (undef =~) warning
Condition100%2/2
$string ||= ""
l!l
4330
MC/DC100%1/1
$string ||= ""
$string
Truth table100%2/2
A
A= $string
Aresult
11
00
132 73 ▶ Carp::croak qq{"$string" is not a valid $type GUID} if $string !~ $regex;
Branch: 1 branch
BranchTrueFalseTotal
unless $string =~ /$regex/ 57 16 73
133 16 $class->from_data_uuid( $_uuid_gen->()->$alien_from_method($string) );
134 12 };
135  
136 12 Sub::Install::install_sub({ code => $our_from_code, as => "from_$type" });
137 }
138  
139 12 ▶sub _install_as_method {
Subroutine _install_as_method: called CC=1 SCAR=0.0
140 12 my ($type, $alien_method) = @_;
141  
142 12 my $alien_to_method = "to_$alien_method";
143  
144 37 ▶ my $our_to_method = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
145 37 my ($self) = @_;
146 37 $_uuid_gen->()->$alien_to_method( $self->as_binary );
147 12 };
148  
149 12 Sub::Install::install_sub({ code => $our_to_method, as => "as_$type" });
150 }
151  
152 4 ▶BEGIN { # possibly unnecessary -- rjbs, 2006-03-11
Subroutine BEGIN: called CC=2 SCAR=6.9
153 4 do {
154 4 ▶ while (my ($type, $profile) = each %type) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %type) 12 4 16
155 12 _install_from_method($type, @$profile);
156 12 _install_as_method ($type, @$profile);
157 }
158 };
159 }
160  
161 8 ▶sub _from_multitype {
Subroutine _from_multitype: called CC=1 SCAR=0.0
162 8 my ($class, $what, $types) = @_;
163 30 ▶ sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
164 30 my ($class, $value) = @_;
165 30 ▶ return $value if eval { $value->isa('Data::GUID') };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $value->isa("Data::GUID") } } 2 28 30
166  
167 28 ▶ my $value_string = defined $value ? qq{"$value"} : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 24 4 28
168  
169 # The only good ref is a blessed ref, and only into our denomination!
170 28 ▶ if (my $ref = ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $value) 4 24 28
171 4 Carp::croak "a $ref reference is not a valid GUID $what"
172 }
173  
174 24 for my $type (@$types) {
175 63 my $from = "from_$type";
176 63 my $guid = eval { $class->$from($value); };
177 63 ▶ return $guid if $guid;
Branch: 1 branch
BranchTrueFalseTotal
if $guid 12 51 63
178 }
179  
180 12 Carp::croak "$value_string is not a valid GUID $what";
181 }
182 8 }
183  
184 #pod =head2 from_any_string
185 #pod
186 #pod my $string = get_string_from_ether;
187 #pod
188 #pod my $guid = Data::GUID->from_any_string($string);
189 #pod
190 #pod This method returns a Data::GUID object for the given string, trying all known
191 #pod string interpretations. An exception is thrown if the value is not a valid
192 #pod GUID string.
193 #pod
194 #pod =cut
195  
196 4 ▶BEGIN { # possibly unnecessary -- rjbs, 2006-03-11
Subroutine BEGIN: called CC=1 SCAR=0.0
197 4 Sub::Install::install_sub({
198 code => __PACKAGE__->_from_multitype('string', [ keys %type ]),
199 as => 'from_any_string',
200 });
201 }
202  
203 #pod =head2 best_guess
204 #pod
205 #pod my $value = get_value_from_ether;
206 #pod
207 #pod my $guid = Data::GUID->best_guess($value);
208 #pod
209 #pod This method returns a Data::GUID object for the given value, trying everything
210 #pod it can. It works like C<L</from_any_string>>, but will also accept Data::UUID
211 #pod values. (In effect, this means that any sixteen byte value is acceptable.)
212 #pod
213 #pod =cut
214  
215 4 ▶BEGIN { # possibly unnecessary -- rjbs, 2006-03-11
Subroutine BEGIN: called CC=1 SCAR=0.0
216 4 Sub::Install::install_sub({
217 code => __PACKAGE__->_from_multitype('value', [(keys %type), 'data_uuid']),
218 as => 'best_guess',
219 });
220 }
221  
222 #pod =head1 GUIDS INTO STRINGS
223 #pod
224 #pod These methods return various string representations of a GUID.
225 #pod
226 #pod =head2 as_string
227 #pod
228 #pod This method returns a "traditional" GUID/UUID string representation. This is
229 #pod five hexadecimal strings, delimited by hyphens. For example:
230 #pod
231 #pod B0470602-A64B-11DA-8632-93EBF1C0E05A
232 #pod
233 #pod This method is also used to stringify Data::GUID objects.
234 #pod
235 #pod =head2 as_hex
236 #pod
237 #pod This method returns a plain hexadecimal representation of the GUID, with a
238 #pod leading C<0x>. For example:
239 #pod
240 #pod 0xB0470602A64B11DA863293EBF1C0E05A
241 #pod
242 #pod =head2 as_base64
243 #pod
244 #pod This method returns a base-64 string representation of the GUID. For example:
245 #pod
246 #pod sEcGAqZLEdqGMpPr8cDgWg==
247 #pod
248 #pod =cut
249  
250 #pod =head1 OTHER METHODS
251 #pod
252 #pod =head2 compare_to_guid
253 #pod
254 #pod This method compares a GUID to another GUID and returns -1, 0, or 1, as do
255 #pod other comparison routines.
256 #pod
257 #pod =cut
258  
259 8 ▶sub compare_to_guid {
Subroutine compare_to_guid: called CC=2 SCAR=6.9
Pod documented
260 8 my ($self, $other) = @_;
261  
262 my $other_binary
263 8 ▶ = eval { $other->isa('Data::GUID') } ? $other->as_binary : $other;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $other->isa('Data::GUID') } } ? : 6 2 8
264  
265 8 $_uuid_gen->()->compare($self->as_binary, $other_binary);
266 }
267  
268 #pod =head2 as_binary
269 #pod
270 #pod This method returns the packed binary representation of the GUID. At present
271 #pod this method relies on Data::GUID's underlying use of Data::UUID. It is not
272 #pod guaranteed to continue to work the same way, or at all. I<Caveat invocator>.
273 #pod
274 #pod =cut
275  
276 51 ▶sub as_binary {
Subroutine as_binary: called CC=1 SCAR=0.0
Pod documented
277 51 my ($self) = @_;
278 51 $$self;
279 }
280  
281 use overload
282 4 ▶ q{""} => 'as_string',
Subroutine BEGIN: called CC=1 SCAR=0.0
283 6 ▶ '<=>' => sub { ($_[2] ? -1 : 1) * $_[0]->compare_to_guid($_[1]) },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 1 5 6
284 4 fallback => 1;
285  
286 #pod =head1 IMPORTING
287 #pod
288 #pod Data::GUID does not export any subroutines by default, but it provides a few
289 #pod routines which will be imported on request. These routines may be called as
290 #pod class methods, or may be imported to be called as subroutines. Calling them by
291 #pod fully qualified name is incorrect.
292 #pod
293 #pod use Data::GUID qw(guid);
294 #pod
295 #pod my $guid = guid; # OK
296 #pod my $guid = Data::GUID->guid; # OK
297 #pod my $guid = Data::GUID::guid; # NOT OK
298 #pod
299 #pod =cut
300  
301 #pod =head2 guid
302 #pod
303 #pod This routine returns a new Data::GUID object.
304 #pod
305 #pod =head2 guid_string
306 #pod
307 #pod This returns the string representation of a new GUID.
308 #pod
309 #pod =head2 guid_hex
310 #pod
311 #pod This returns the hex representation of a new GUID.
312 #pod
313 #pod =head2 guid_base64
314 #pod
315 #pod This returns the base64 representation of a new GUID.
316 #pod
317 #pod =head2 guid_from_anything
318 #pod
319 #pod This returns the result of calling the C<L</from_any_string>> method.
320 #pod
321 #pod =cut
322  
323 4 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
324 4 Sub::Install::install_sub({ code => 'new', as => 'guid' });
325  
326 4 for my $type (keys %type) {
327 12 my $method = "guid_$type";
328 12 my $as = "as_$type";
329  
330 Sub::Install::install_sub({
331 as => $method,
332 15 ▶ code => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
333 15 my ($class) = @_;
334 15 $class->new->$as;
335 },
336 12 });
337 }
338 }
339  
340 14 ▶sub _curry_class {
Subroutine _curry_class: called CC=2 SCAR=6.9
341 14 my ($class, $subname, $eval) = @_;
342 8 ▶ return $eval ? sub { eval { $class->$subname(@_) } }
Subroutine __ANON__: called CC=1 SCAR=0.0
343 14 ▶ : sub { $class->$subname(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$eval ? : 2 12 14
344 }
345  
346 4 my %exports;
347 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
348 %exports
349 16 ▶ = map { my $method = $_; $_ => sub { _curry_class($_[0], $method) } }
Subroutine __ANON__: called CC=1 SCAR=0.0
350 4 ((map { "guid_$_" } keys %type), 'guid');
351 }
352  
353 4 ▶use Sub::Exporter 0.90 -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.0
354 exports => {
355 %exports, # defined just above
356 2 ▶ guid_from_anything => sub { _curry_class($_[0], 'from_any_string', 1) },
Subroutine __ANON__: called CC=1 SCAR=0.0
357 }
358 4 };
359  
360 #pod =head1 TODO
361 #pod
362 #pod =for :list
363 #pod * add namespace support
364 #pod * remove dependency on wretched Data::UUID
365 #pod * make it work on 5.005
366 #pod
367 #pod =cut
368  
369 4 1;
370  
371 __END__
↑ Summary