JobQueue/Util.pm

filter: Statementstmt 97.7% 43 / 44 Branchbran 75.0% 12 / 16 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 82.1% 69 / 84 CC 12 SCAR 17.3
CC
12
Cov
85%
format_message
9.9
format_reference
4.2
BEGIN
1.0
CRAP 5.7
« JobQueue.pm ↑ Summary JobQueue/Job.pm »
1 package Redis::JobQueue::Util;
2  
3 =head1 NAME
4  
5 Redis::JobQueue::Util - String manipulation utilities.
6  
7 =head1 VERSION
8  
9 This documentation refers to C<Redis::JobQueue::Util> version 1.19
10  
11 =cut
12  
13 #-- Pragmas --------------------------------------------------------------------
14  
15 63 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 63 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 63 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # ENVIRONMENT ------------------------------------------------------------------
20  
21 63 our $VERSION = '1.19';
22  
23 63 ▶use Exporter qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
24 import
25 63 );
26  
27 63 our @EXPORT_OK = qw(
28 &format_message
29 &format_reference
30 );
31  
32 #-- load the modules -----------------------------------------------------------
33  
34 63 ▶use overload;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 63 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 63 ▶use Data::Dumper ();
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 #-- declarations ---------------------------------------------------------------
39  
40 =head1 SYNOPSIS
41  
42 use 5.010;
43 use strict;
44 use warnings;
45  
46 use Redis::CappedCollection::Util qw( format_message );
47 $string = format_message( 'Object %d loaded. Status: %s', $id, $message );
48  
49 =head1 DESCRIPTION
50  
51 String manipulation utilities.
52  
53 =cut
54  
55 #-- public functions -----------------------------------------------------------
56  
57 =head1 EXPORTED FUNCTIONS
58  
59 Use these functions by importing them into your package or by calling a fully-qualified method name.
60  
61 =cut
62  
63 =head2 format_reference
64  
65 say format_reference( $object );
66  
67 Dumps reference using preconfigured L<Data::Dumper>. Produces less verbose
68 output than default L<Data::Dumper> settings.
69  
70 =cut
71  
72 63 my $dumper;
73 63 my $empty_array = [];
74  
75 4 ▶sub format_reference {
Subroutine format_reference: called CC=4 SCAR=14.5
Pod documented
76 4 my ( $value ) = @_;
77  
78 4 ▶ unless( $dumper ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dumper) 1 3 4
79 1 $dumper = Data::Dumper->new( $empty_array )
80 ->Indent( 0 )
81 ->Terse( 1 )
82 ->Quotekeys( 0 )
83 ->Sortkeys( 1 )
84 ->Useperl( 1 ) # XS version seems to have a bug which sometimes results in modification of original object
85 # ->Sparseseen( 1 ) # speed up since we don't use "Seen" hash
86 ;
87 }
88  
89 4 my $r;
90 4 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (overload::Overloaded($value) and overload::Method($value, '""')) { } 0 4 4
Condition33%1/3
overload::Overloaded($value) and overload::Method($value, "\"\"")
!ll&&!rl&&r
400
MC/DC0%0/2
overload::Overloaded($value) and overload::Method($value, "\"\"")
overload::Overloaded($value) overload::Method($value, "\"\"")
Truth table33%1/3
A and B
A= overload::Overloaded($value)B= overload::Method($value, "\"\"")
ABresult
0X0
100
111
91 overload::Overloaded( $value ) &&
92 overload::Method( $value, '""' )
93 ) {
94 0 $r = "$value"; # force stringification
95 } else {
96 4 $r = $dumper->Values( [ $value ] )->Dump;
97 4 $dumper->Reset->Values( $empty_array );
98 }
99  
100 4 return $r;
101 }
102  
103 =head2 format_message
104  
105 $string = format_message( 'Object %d loaded. Status: %s', $id, $message );
106  
107 Returns string formatted using printf-style syntax.
108  
109 If there are more than one argument and the first argument contains C<%...>
110 conversions, arguments are converted to a string message using C<sprintf()>. In this case, undefined
111 values are printed as C<< <undef> >> and references are converted to strings using L</format_reference>.
112  
113 =cut
114 9 ▶sub format_message {
Subroutine format_message: called CC=9 SCAR=22.9
Pod documented
115 9 ▶ my $format = shift // return;
Condition50%1/2
shift() // (return)
l!l
90
MC/DC0%0/1
shift() // (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
116  
117 9 my $got = scalar @_;
118  
119 9 ▶ return $format unless $got && $format =~ /\%/;
Branch: 1 branch
BranchTrueFalseTotal
unless $got and $format =~ /\%/ 0 9 9
Condition33%1/3
$got and $format =~ /\%/
!ll&&!rl&&r
009
MC/DC0%0/2
$got and $format =~ /\%/
$got $format =~ /\%/
Truth table33%1/3
A and B
A= $gotB= $format =~ /\%/
ABresult
0X0
100
111
120  
121 9 my $expected = 0;
122 9 ▶ while ( $format =~ /(%%|%[^%])/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format =~ /(%%|%[^%])/g) 9 9 18
123 9 ▶ next if $1 eq '%%'; # don't count escape sequence
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq "%%" 0 9 9
124 9 ++$expected;
125 }
126  
127 9 ▶ Carp::cluck "Wrong number of arguments: $expected vs $got" unless $got == $expected;
Branch: 1 branch
BranchTrueFalseTotal
unless $got == $expected 0 9 9
128  
129 return sprintf $format, map {
130 9 ▶ !defined $_
Branch: 2 branches
BranchTrueFalseTotal
!defined($_) ? : 1 8 9
ref $_ ? : 4 4 8
131 ? '<undef>'
132 : ref $_
133 ? format_reference( $_ )
134 : $_
135 } @_;
136 }
137  
138 #-- private functions ----------------------------------------------------------
139  
140 63 1;
141  
142 __END__
« JobQueue.pm ↑ Summary JobQueue/Job.pm »