DBI/Utils.pm

filter: Statementstmt 89.1% 132 / 148 Branchbran 71.7% 56 / 78 Conditioncond 59.2% 16 / 27 MC/DCmcdc 44.4% 8 / 18 Subroutinesub 100% 19 / 19 Podpod 100% 6 / 6 totaltotal 80.0% 237 / 296 CC 58 SCAR 26.8
CC
58
Cov
81%
_get_diff_string_bytes
31.1
_diff_structures
21.8
_stringify_data_structure
8.3
CRAP 14.5
« DBI.pm ↑ Summary DBI/Event.pm »
1 package Audit::DBI::Utils;
2  
3 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 ▶use Class::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 18 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 18 ▶use Data::Validate::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 =head1 NAME
13  
14 Audit::DBI::Utils - Utilities for the Audit::DBI distribution.
15  
16  
17 =head1 VERSION
18  
19 Version 1.9.0
20  
21 =cut
22  
23 18 our $VERSION = '1.9.0';
24  
25  
26 =head1 SYNOPSIS
27  
28 use Audit::DBI::Utils;
29  
30 my $ip_address = Audit::DBI::Utils::integer_to_ipv4( $integer );
31  
32 my $integer = Audit::DBI::Utils::ipv4_to_integer( $ip_address );
33  
34 my $differences = Audit::DBI::Utils::diff_structures(
35 $data_structure_1,
36 $data_structure_2,
37 comparison_function => sub { my ( $a, $b ) = @_; $a eq $b; }, #optional
38 );
39  
40 my $diff_string_bytes = Audit::DBI::Utils::get_diff_string_bytes(
41 $differences
42 );
43  
44  
45 =head1 FUNCTIONS
46  
47 =head2 stringify_data_structure()
48  
49 my $string = Audit::DBI::Utils::stringify_data_structure(
50 data_structure => $data_structure,
51 object_stringification_map =>
52 {
53 'Math::Currency' => 'as_float',
54 },
55 );
56  
57 =cut
58  
59 sub stringify_data_structure
60 3 ▶{
Subroutine stringify_data_structure: called CC=2 SCAR=7.0
Pod documented
61 3 my ( %args ) = @_;
62 3 my $data_structure = delete( $args{'data_structure'} );
63 3 my $object_stringification_map = delete( $args{'object_stringification_map'} );
64 3 ▶ croak 'The following arguments are not valid: ' . join( ', ', keys %args )
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %args != 0 0 3 3
65 if scalar( keys %args ) != 0;
66  
67 3 return _stringify_data_structure( $data_structure, $object_stringification_map );
68 }
69  
70 sub _stringify_data_structure
71 9 ▶{
Subroutine _stringify_data_structure: called CC=6 SCAR=21.2
72 9 my ( $data_structure, $object_stringification_map ) = @_;
73  
74 9 ▶ if ( Data::Validate::Type::is_arrayref( $data_structure ) )
Branch: 2 branches
BranchTrueFalseTotal
if (Data::Validate::Type::is_arrayref($data_structure)) { } 2 7 9
elsif (Data::Validate::Type::is_hashref($data_structure)) { } 1 6 7
75 {
76 # If we have an array, try to stringify each of the elements.
77 return
78 [
79 2 map { _stringify_data_structure( $_, $object_stringification_map ) } @$data_structure
80 ];
81 }
82 elsif ( Data::Validate::Type::is_hashref( $data_structure ) )
83 {
84 # First, we try to stringify this object.
85 1 foreach my $class ( keys %$object_stringification_map )
86 {
87 0 ▶ next if !Data::Validate::Type::is_instance( $data_structure, class => $class );
Branch: 1 branch
BranchTrueFalseTotal
unless Data::Validate::Type::is_instance($data_structure, "class", $class) 0 0 0
88 0 my $stringification_method = $object_stringification_map->{ $class };
89 0 ▶ next if !$data_structure->can( $stringification_method );
Branch: 1 branch
BranchTrueFalseTotal
unless $data_structure->can($stringification_method) 0 0 0
90 0 return $data_structure->$stringification_method();
91 }
92  
93 # If we haven't found it in our list of stringifiable objects,
94 # then we need to investigate the individual keys.
95 return
96 {
97 map
98 1 { $_ => _stringify_data_structure( $data_structure->{ $_ }, $object_stringification_map ) }
99 keys %$data_structure
100 };
101 }
102 else
103 {
104 6 return $data_structure;
105 }
106 }
107  
108  
109 =head2 integer_to_ipv4()
110  
111 Convert a 32-bits integer representing an IP address into its IPv4 form.
112  
113 my $ip_address = Audit::DBI::Utils::integer_to_ipv4( $integer );
114  
115 =cut
116  
117 sub integer_to_ipv4
118 5 ▶{
Subroutine integer_to_ipv4: called CC=3 SCAR=12.0
Pod documented
119 5 my ( $integer ) = @_;
120  
121 return undef
122 5 ▶ if !defined( $integer ) || $integer !~ m/^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $integer and $integer =~ /^\d+$/ 0 5 5
Condition33%1/3
defined $integer and $integer =~ /^\d+$/
!ll&&!rl&&r
005
MC/DC0%0/2
defined $integer and $integer =~ /^\d+$/
defined $integer $integer =~ /^\d+$/
Truth table33%1/3
A and B
A= defined $integerB= $integer =~ /^\d+$/
ABresult
0X0
100
111
123  
124 5 return join( '.', map { ( $integer >> 8 * ( 3 - $_ ) ) % 256 } 0..3 );
125 }
126  
127  
128 =head2 ipv4_to_integer()
129  
130 Convert an IPv4 address to a 32-bit integer.
131  
132 my $integer = Audit::DBI::Utils::ipv4_to_integer( $ip_address );
133  
134 =cut
135  
136 sub ipv4_to_integer
137 34 ▶{
Subroutine ipv4_to_integer: called CC=5 SCAR=16.1
Pod documented
138 34 my ( $ip_address ) = @_;
139  
140 return undef
141 34 ▶ if !defined( $ip_address );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip_address 6 28 34
142  
143 28 ▶ if ( my ( @bytes ) = $ip_address =~ m/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/x )
Branch: 1 branch
BranchTrueFalseTotal
if (my(@bytes) = $ip_address =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/x) 23 5 28
144 {
145 23 ▶ if ( ! grep { $_ > 255 } @bytes )
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ > 255;} @bytes) 23 0 23
146 {
147 23 @bytes = reverse( @bytes );
148 23 my $integer = 0;
149 23 foreach my $exponent ( 0..3 )
150 {
151 92 $integer += $bytes[ $exponent ] * 256**$exponent;
152 }
153 23 return $integer;
154 }
155 }
156  
157 # Invalid input.
158 5 return undef;
159 }
160  
161  
162 =head2 diff_structures()
163  
164 Return the differences between the two data structures passed as parameter.
165  
166 By default, if leaf nodes are compared with '==' if they are both numeric, and
167 with 'eq' otherwise.
168  
169 An optional I<comparison_function> parameter can be used to specify a different
170 comparison function.
171  
172 my $differences = Audit::DBI::Utils::diff_structures(
173 $data_structure_1,
174 $data_structure_2,
175 );
176  
177 # Alternative built-in comparison function.
178 # Leaf nodes are compared using 'eq'.
179 my $diff = Audit::DBI::Utils::diff_structures(
180 $data_structure_1,
181 $data_structure_2,
182 comparison_function => 'eq',
183 );
184  
185 # Alternative custom comparison function.
186 my $diff = Audit::DBI::Utils::diff_structures(
187 $data_structure_1,
188 $data_structure_2,
189 comparison_function => sub
190 {
191 my ( $variable_1, $variable2 ) = @_;
192 # [...]
193 return $is_equal;
194 }
195 );
196  
197 =cut
198  
199 sub diff_structures
200 28 ▶{
Subroutine diff_structures: called CC=1 SCAR=0.0
Pod documented
201 28 my ( @args ) = @_;
202 28 return _diff_structures(
203 {},
204 @args
205 );
206 }
207  
208 sub _diff_structures_comparison_eq
209 9 ▶{
Subroutine _diff_structures_comparison_eq: called CC=1 SCAR=0.0
210 9 my ( $variable_1, $variable_2 ) = @_;
211  
212 9 return $variable_1 eq $variable_2;
213 }
214  
215 sub _diff_structures_comparison_default
216 9 ▶{
Subroutine _diff_structures_comparison_default: called CC=3 SCAR=11.0
217 9 my ( $variable_1, $variable_2 ) = @_;
218  
219 # For numbers, return numerical comparison.
220 9 ▶ return $variable_1 == $variable_2
Branch: 1 branch
BranchTrueFalseTotal
if &Scalar::Util::looks_like_number($variable_1) and &Scalar::Util::looks_like_number($variable_2) 6 3 9
Condition66%2/3
&Scalar::Util::looks_like_number($variable_1) and &Scalar::Util::looks_like_number($variable_2)
!ll&&!rl&&r
306
MC/DC50%1/2
&Scalar::Util::looks_like_number($variable_1) and &Scalar::Util::looks_like_number($variable_2)
&Scalar::Util::looks_like_number($variable_1) &Scalar::Util::looks_like_number($variable_2)
Truth table66%2/3
A and B
A= &Scalar::Util::looks_like_number($variable_1)B= &Scalar::Util::looks_like_number($variable_2)
ABresult
0X0
100
111
221 if Scalar::Util::looks_like_number( $variable_1 ) && Scalar::Util::looks_like_number( $variable_2 );
222  
223 # Otherwise, use exact string match.
224 3 return $variable_1 eq $variable_2;
225 }
226  
227 sub _diff_structures
228 45 ▶{
Subroutine _diff_structures: called CC=21 SCAR=30.8
229 45 my ( $cache, $structure1, $structure2, %args ) = @_;
230 45 my $comparison_function = $args{'comparison_function'};
231  
232 # make sure the provided equality function is really a coderef
233 45 ▶ if ( !Data::Validate::Type::is_coderef( $comparison_function ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (Data::Validate::Type::is_coderef($comparison_function)) 28 17 45
234 {
235 28 ▶ if ( defined( $comparison_function ) && ( $comparison_function eq 'eq' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $comparison_function and $comparison_function eq 'eq') { } 14 14 28
Condition66%2/3
defined $comparison_function and $comparison_function eq "eq"
!ll&&!rl&&r
14014
MC/DC50%1/2
defined $comparison_function and $comparison_function eq "eq"
defined $comparison_function $comparison_function eq "eq"
Truth table66%2/3
A and B
A= defined $comparison_functionB= $comparison_function eq "eq"
ABresult
0X0
100
111
236 {
237 14 $comparison_function = \&_diff_structures_comparison_eq;
238 }
239 else
240 {
241 14 $comparison_function = \&_diff_structures_comparison_default;
242 }
243 }
244  
245 # If one of the structure is undef, return
246 45 ▶ if ( !defined( $structure1 ) || !defined( $structure2 ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $structure1 and defined $structure2) 9 36 45
Condition100%3/3
defined $structure1 and defined $structure2
!ll&&!rl&&r
6336
MC/DC100%2/2
defined $structure1 and defined $structure2
defined $structure1 defined $structure2
Truth table100%3/3
A and B
A= defined $structure1B= defined $structure2
ABresult
0X0
100
111
247 {
248 9 ▶ if ( !defined( $structure1 ) && !defined( $structure2 ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $structure1 || defined $structure2) { } 3 6 9
Condition100%3/3
defined $structure1 || defined $structure2
l!l&&r!l&&!r
333
MC/DC100%2/2
defined $structure1 || defined $structure2
defined $structure1 defined $structure2
Truth table100%3/3
A || B
A= defined $structure1B= defined $structure2
ABresult
1X1
011
000
249 {
250 3 return undef;
251 }
252 else
253 {
254 return
255 {
256 6 old => $structure1,
257 new => $structure2
258 };
259 }
260 }
261  
262 # Cache memory addresses to make sure we don't get into an infinite loop.
263 # The idea comes from Test::Deep's code.
264 return undef
265 36 ▶ if exists( $cache->{ "$structure1" }->{ "$structure2" } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $$cache{"$structure1"}{"$structure2"} 0 36 36
266 36 $cache->{ "$structure1" }->{ "$structure2" } = undef;
267  
268 # Hashes (including hashes blessed as objects)
269 36 ▶ if ( Data::Validate::Type::is_hashref( $structure1 ) && Data::Validate::Type::is_hashref( $structure2 ) )
Branch: 1 branch
BranchTrueFalseTotal
if (Data::Validate::Type::is_hashref($structure1) and Data::Validate::Type::is_hashref($structure2)) 3 33 36
Condition66%2/3
Data::Validate::Type::is_hashref($structure1) and Data::Validate::Type::is_hashref($structure2)
!ll&&!rl&&r
3303
MC/DC50%1/2
Data::Validate::Type::is_hashref($structure1) and Data::Validate::Type::is_hashref($structure2)
Data::Validate::Type::is_hashref($structure1) Data::Validate::Type::is_hashref($structure2)
Truth table66%2/3
A and B
A= Data::Validate::Type::is_hashref($structure1)B= Data::Validate::Type::is_hashref($structure2)
ABresult
0X0
100
111
270 {
271 3 my %union_keys = map { $_ => undef } ( keys %$structure1, keys %$structure2 );
272  
273 3 my %tmp = ();
274 3 foreach ( keys %union_keys )
275 {
276 my $diff = _diff_structures(
277 $cache,
278 $structure1->{$_},
279 6 $structure2->{$_},
280 %args,
281 );
282 6 ▶ $tmp{$_} = $diff
Branch: 1 branch
BranchTrueFalseTotal
if defined $diff 3 3 6
283 if defined( $diff );
284 }
285  
286 3 ▶ return ( scalar( keys %tmp ) != 0 ? \%tmp : undef );
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %tmp != 0 ? : 3 0 3
287 }
288  
289 # If the structures have different references, since we've ruled out blessed
290 # hashes (objects) above (that could have a different blessing with the same
291 # actual content), return the elements
292 33 ▶ if ( ref( $structure1 ) ne ref( $structure2 ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $structure1 ne ref $structure2) 0 33 33
293 {
294 return
295 {
296 0 old => $structure1,
297 new => $structure2
298 };
299 }
300  
301 # Simple scalars, compare and return
302 33 ▶ if ( ref( $structure1 ) eq '' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $structure1 eq "") 29 4 33
303 {
304 29 ▶ return $comparison_function->( $structure1, $structure2 )
Branch: 1 branch
BranchTrueFalseTotal
&$comparison_function($structure1, $structure2) ? : 15 14 29
305 ? undef
306 : {
307 old => $structure1,
308 new => $structure2
309 };
310 }
311  
312 # Arrays
313 4 ▶ if ( Data::Validate::Type::is_arrayref( $structure1 ) )
Branch: 1 branch
BranchTrueFalseTotal
if (Data::Validate::Type::is_arrayref($structure1)) 4 0 4
314 {
315 4 my @tmp = ();
316 4 my $max_length = ( sort { $a <=> $b } ( scalar( @$structure1 ), scalar( @$structure2 ) ) )[1];
317 4 for my $i ( 0..$max_length-1 )
318 {
319 11 my $diff = _diff_structures(
320 $cache,
321 $structure1->[$i],
322 $structure2->[$i],
323 %args,
324 );
325 11 ▶ next unless defined( $diff );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $diff 7 4 11
326  
327 4 $diff->{'index'} = $i;
328 4 push(
329 @tmp,
330 $diff
331 );
332 }
333  
334 4 ▶ return ( scalar( @tmp ) != 0 ? \@tmp : undef );
Branch: 1 branch
BranchTrueFalseTotal
scalar @tmp != 0 ? : 4 0 4
335 }
336  
337 # We don't track other types for audit purposes
338 0 return undef;
339 }
340  
341  
342 =head2 get_diff_string_bytes()
343  
344 Return the size in bytes of the string differences. The argument must be a diff
345 structure returned by C<Audit::DBI::Utils::diff_structures()>.
346  
347 This function has two modes:
348  
349 =over 4
350  
351 =item * Relative comparison (default):
352  
353 In this case, a string change from 'TestABC' to 'TestCDE' is a 0 bytes
354 change (since there is the same number of characters).
355  
356 my $diff_string_bytes = Audit::DBI::Utils::get_diff_string_bytes(
357 $diff_structure
358 );
359  
360 =item * Absolute comparison:
361  
362 In this case, a string change from 'TestABC' to 'TestCDE' is a 6 bytes
363 change (3 characters removed, and 3 added).
364  
365 my $diff_string_bytes = Audit::DBI::Utils::get_diff_string_bytes(
366 $diff_structure,
367 absolute => 1,
368 );
369  
370 Note that absolute comparison requires L<String::Diff> to be installed.
371  
372 =back
373  
374 =cut
375  
376 sub get_diff_string_bytes
377 6 ▶{
Subroutine get_diff_string_bytes: called CC=3 SCAR=12.5
Pod documented
378 6 my ( $diff_structure, %args ) = @_;
379  
380 croak 'Cannot perform string comparison without String::Diff installed, please install first and then retry'
381 6 ▶ if $args{'absolute'} && !Class::Load::try_load_class( 'String::Diff' );
Branch: 1 branch
BranchTrueFalseTotal
if $args{"absolute"} and not Class::Load::try_load_class("String::Diff") 0 6 6
Condition33%1/3
$args{"absolute"} and not Class::Load::try_load_class("String::Diff")
!ll&&!rl&&r
600
MC/DC0%0/2
$args{"absolute"} and not Class::Load::try_load_class("String::Diff")
$args{"absolute"} not Class::Load::try_load_class("String::Diff")
Truth table33%1/3
A and not(B)
A= $args{"absolute"}B= not Class::Load::try_load_class("String::Diff")
ABresult
0X0
100
111
382  
383 6 return _get_diff_string_bytes(
384 {},
385 $diff_structure,
386 %args,
387 );
388 }
389  
390 sub _get_diff_string_bytes
391 9 ▶{
Subroutine _get_diff_string_bytes: called CC=15 SCAR=34.4
392 9 my ( $cache, $diff_structure, %args ) = @_;
393  
394 9 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless defined $diff_structure 1 8 9
395 if !defined( $diff_structure );
396  
397 # Cache memory addresses to make sure we don't get into an infinite loop.
398 # The idea comes from Test::Deep's code.
399 return undef
400 8 ▶ if exists( $cache->{ "$diff_structure" } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $$cache{"$diff_structure"} 0 8 8
401 8 $cache->{ "$diff_structure" } = undef;
402  
403 # A hash can mean that a hash had different keys, or this is a leaf node
404 # indicating old/new data.
405 8 ▶ if ( Data::Validate::Type::is_hashref( $diff_structure ) )
Branch: 1 branch
BranchTrueFalseTotal
if (Data::Validate::Type::is_hashref($diff_structure)) 5 3 8
406 {
407 # If we have an 'old' and 'new' key, then it's a leaf node.
408 5 ▶ if ( exists( $diff_structure->{'new'} ) && exists( $diff_structure->{'old'} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $diff_structure->{'new'} and exists $diff_structure->{'old'}) { } 4 1 5
Condition66%2/3
exists $diff_structure->{"new"} and exists $diff_structure->{"old"}
!ll&&!rl&&r
104
MC/DC50%1/2
exists $diff_structure->{"new"} and exists $diff_structure->{"old"}
exists $diff_structure->{"new"} exists $diff_structure->{"old"}
Truth table66%2/3
A and B
A= exists $diff_structure->{"new"}B= exists $diff_structure->{"old"}
ABresult
0X0
100
111
409 {
410 # If we're performing an absolute comparison, we need to add the data removed
411 # to the data added.
412 4 ▶ if ( $args{'absolute'} )
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'absolute'}) { } 0 4 4
413 {
414 # If both structures are not strings, it means we can't inspect
415 # inside to do a finer grained comparison and we can only add their
416 # respective sizes.
417 return get_string_bytes( $diff_structure->{'new'} ) + get_string_bytes( $diff_structure->{'old'} )
418 if !Data::Validate::Type::is_string( $diff_structure->{'new'} )
419 0 ▶ || !Data::Validate::Type::is_string( $diff_structure->{'old'} );
Branch: 1 branch
BranchTrueFalseTotal
unless Data::Validate::Type::is_string($diff_structure->{"new"}) and Data::Validate::Type::is_string($diff_structure->{"old"}) 0 0 0
Condition0%0/3
Data::Validate::Type::is_string($diff_structure->{"new"}) and Data::Validate::Type::is_string($diff_structure->{"old"})
!ll&&!rl&&r
000
MC/DC0%0/2
Data::Validate::Type::is_string($diff_structure->{"new"}) and Data::Validate::Type::is_string($diff_structure->{"old"})
Data::Validate::Type::is_string($diff_structure->{"new"}) Data::Validate::Type::is_string($diff_structure->{"old"})
Truth table0%0/3
A and B
A= Data::Validate::Type::is_string($diff_structure->{"new"})B= Data::Validate::Type::is_string($diff_structure->{"old"})
ABresult
0X0
100
111
420  
421 # If both structures are strings however, then we can diff the
422 # strings to find out exactly how much has changed.
423 my $diff = String::Diff::diff_fully(
424 $diff_structure->{'old'},
425 0 $diff_structure->{'new'},
426 );
427  
428 0 my $diff_string_bytes = 0;
429 0 foreach my $line ( @{ $diff->[0] }, @{ $diff->[1] } )
430 {
431 0 ▶ if ( $line->[0] eq '+' )
Branch: 2 branches
BranchTrueFalseTotal
if ($line->[0] eq '+') { } 0 0 0
elsif ($line->[0] eq '-') { } 0 0 0
432 {
433 0 $diff_string_bytes += get_string_bytes( $line->[1] );
434 }
435 elsif ( $line->[0] eq '-' )
436 {
437 0 $diff_string_bytes += get_string_bytes( $line->[1] );
438 }
439 }
440 0 return $diff_string_bytes;
441 }
442 # If we're performing a relative comparison, we substract the data removed
443 # from the data added.
444 else
445 {
446 4 return get_string_bytes( $diff_structure->{'new'} ) - get_string_bytes( $diff_structure->{'old'} );
447 }
448 }
449 # Otherwise, we need to explore inside the values.
450 else
451 {
452 1 my $diff_string_bytes = 0;
453 1 foreach my $value ( values %$diff_structure )
454 {
455 1 $diff_string_bytes += _get_diff_string_bytes( $cache, $value, %args );
456 }
457 1 return $diff_string_bytes;
458 }
459 }
460  
461 # If we have an array, loop through it.
462 3 ▶ if ( Data::Validate::Type::is_arrayref( $diff_structure ) )
Branch: 1 branch
BranchTrueFalseTotal
if (Data::Validate::Type::is_arrayref($diff_structure)) 2 1 3
463 {
464 2 my $diff_string_bytes = 0;
465 2 foreach my $element ( @$diff_structure )
466 {
467 2 $diff_string_bytes += _get_diff_string_bytes( $cache, $element, %args );
468 }
469 2 return $diff_string_bytes;
470 }
471  
472 # The above parses entirely a diff structure, if anything didn't match
473 # then the diff structure is not valid.
474 1 local $Data::Dumper::Terse = 1;
475 1 croak 'Invalid diff structure: ' . Dumper( $diff_structure );
476 }
477  
478  
479 =head2 get_string_bytes()
480  
481 Return the size in bytes of all the strings contained in the data structure
482 passed as argument.
483  
484 my $string_bytes = Audit::DBI::Utils::get_string_bytes( 'Test' );
485  
486 my $string_bytes = Audit::DBI::Utils::get_string_bytes(
487 [ 'Test1', 'Test2' ]
488 );
489  
490 my $string_bytes = Audit::DBI::Utils::get_string_bytes(
491 { 'Test' => 1 }
492 );
493  
494 Note: this function is recursive, and will explore both arrayrefs and hashrefs.
495  
496 =cut
497  
498 sub get_string_bytes
499 15 ▶{
Subroutine get_string_bytes: called CC=1 SCAR=0.0
Pod documented
500 15 my ( $structure ) = @_;
501  
502 15 return _get_string_bytes(
503 {},
504 $structure,
505 );
506 }
507  
508  
509 sub _get_string_bytes
510 33 ▶{
Subroutine _get_string_bytes: called CC=8 SCAR=20.8
511 33 my ( $cache, $structure ) = @_;
512  
513 33 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless defined $structure 1 32 33
514 if !defined( $structure );
515  
516 # Use bytes pragma to calculate the byte size of the strings correctly.
517 18 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
518  
519 # Strings allow ending the recursion.
520 32 ▶ if ( Data::Validate::Type::is_string( $structure ) )
Branch: 1 branch
BranchTrueFalseTotal
if (Data::Validate::Type::is_string($structure)) 26 6 32
521 {
522 26 return bytes::length( $structure );
523 }
524  
525 # Cache memory addresses to make sure we don't get into an infinite loop.
526 # If a loop is detected in the structure, we've counted the size of one
527 # cycle at this point and we'll ignore the others.
528 6 ▶ return 0 if defined( $cache->{ "$structure" } );
Branch: 1 branch
BranchTrueFalseTotal
if defined $$cache{"$structure"} 0 6 6
529 6 $cache->{ "$structure" } = 1;
530  
531 # For hashrefs, we calculate the size of the keys and the values.
532 6 ▶ if ( Data::Validate::Type::is_hashref( $structure ) )
Branch: 1 branch
BranchTrueFalseTotal
if (Data::Validate::Type::is_hashref($structure)) 3 3 6
533 {
534 3 my $size = 0;
535 3 foreach my $data ( keys %$structure, values %$structure )
536 {
537 12 $size += _get_string_bytes( $cache, $data );
538 }
539 3 return $size;
540 }
541  
542 # For arrayrefs, we calculate the size of each element.
543 3 ▶ if ( Data::Validate::Type::is_arrayref( $structure ) )
Branch: 1 branch
BranchTrueFalseTotal
if (Data::Validate::Type::is_arrayref($structure)) 2 1 3
544 {
545 2 my $size = 0;
546 2 foreach my $data ( @$structure )
547 {
548 6 $size += _get_string_bytes( $cache, $data );
549 }
550 2 return $size;
551 }
552  
553 # If it's not a string, an array, or a hash, we can't retrieve strings
554 # from the data structure so we'll ignore it.
555 1 return 0;
556 }
557  
558  
559 =head1 BUGS
560  
561 Please report any bugs or feature requests through the web interface at
562 L<https://github.com/guillaumeaubert/Audit-DBI/issues/new>.
563 I will be notified, and then you'll automatically be notified of progress on
564 your bug as I make changes.
565  
566  
567 =head1 SUPPORT
568  
569 You can find documentation for this module with the perldoc command.
570  
571 perldoc Audit::DBI::Utils
572  
573  
574 You can also look for information at:
575  
576 =over 4
577  
578 =item * GitHub's request tracker
579  
580 L<https://github.com/guillaumeaubert/Audit-DBI/issues>
581  
582 =item * AnnoCPAN: Annotated CPAN documentation
583  
584 L<http://annocpan.org/dist/Audit-DBI>
585  
586 =item * CPAN Ratings
587  
588 L<http://cpanratings.perl.org/d/Audit-DBI>
589  
590 =item * MetaCPAN
591  
592 L<https://metacpan.org/release/Audit-DBI>
593  
594 =back
595  
596  
597 =head1 AUTHOR
598  
599 L<Guillaume Aubert|https://metacpan.org/author/AUBERTG>,
600 C<< <aubertg at cpan.org> >>.
601  
602  
603 =head1 COPYRIGHT & LICENSE
604  
605 Copyright 2010-2017 Guillaume Aubert.
606  
607 This code is free software; you can redistribute it and/or modify it under the
608 same terms as Perl 5 itself.
609  
610 This program is distributed in the hope that it will be useful, but WITHOUT ANY
611 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
612 PARTICULAR PURPOSE. See the LICENSE file for more details.
613  
614 =cut
615  
616 18 1;
« DBI.pm ↑ Summary DBI/Event.pm »