File Coverage

blib/lib/DBR/Common.pm
Criterion Covered Total %
statement 54 86 62.7
branch 18 38 47.3
condition 2 8 25.0
subroutine 12 17 70.5
pod 0 1 0.0
total 86 150 57.3


line stmt bran cond sub pod time code
1             package DBR::Common;
2              
3 19     19   1601 use strict;
  19         38  
  19         821  
4 19     19   27053 use Time::HiRes;
  19         60614  
  19         145  
5 19     19   3302 use Carp;
  19         42  
  19         29526  
6              
7             my %TIMERS;
8              
9             sub _uniq{
10 60     60   2308 my $self = shift;
11 60         721 my $has_undef;
12             my %uniq;
13 60 100       177 return grep{ defined($_)?( !$uniq{$_}++ ):( !$has_undef++ ) } @_;
  196         1185  
14              
15             }
16              
17             sub _split{
18 1012     1012   2802 my $self = shift;
19 1012         2264 my $value = shift;
20              
21 1012         1971 my $out;
22 1012 100       4130 if(ref($value)){
23 1         4 $out = $value;
24             }else{
25 1011         17911 $value =~ s/^\s*|\s*$//g;
26 1011         4854 $out = [ split(/\s+/,$value) ];
27             }
28              
29 1012 100       6389 return wantarray? (@$out): $out;
30             }
31              
32             sub _arrayify{
33 2273     2273   4405 my $self = shift;
34 2273 100       4880 my @out = map { ref($_) eq 'ARRAY' ? (@$_) : ($_) } @_;
  2273         14048  
35 2273 50       12837 return wantarray? (@out) : \@out;
36             }
37              
38             sub _hashify{
39 0     0   0 my $self = shift;
40 0         0 my %out;
41 0         0 while(@_){
42 0         0 my $k = shift;
43 0 0       0 if(ref($k) eq 'HASH'){
44 0         0 %out = (%out,%$k);
45 0         0 next;
46             }
47 0         0 my $v = shift;
48 0         0 $out{ $k } = $v;
49             }
50 0 0       0 return wantarray? (%out) : \%out;
51             }
52              
53             # returns true if all elements of Arrayref A (or single value) are present in arrayref B
54             sub _b_in{
55 16     16   35 my $self = shift;
56 16         40 my $value1 = shift;
57 16         36 my $value2 = shift;
58 16 50       104 $value1 = [$value1] unless ref($value1);
59 16 50       72 $value2 = [$value2] unless ref($value2);
60 16 50 33     154 return undef unless (ref($value1) eq 'ARRAY' && ref($value2) eq 'ARRAY');
61 16         46 my %valsA = map {$_ => 1} @{$value2};
  4         25  
  16         55  
62 16         38 my $results;
63 16         35 foreach my $val (@{$value1}) {
  16         53  
64 16 50       73 unless ($valsA{$val}) {
65 16         102 return 0;
66             }
67             }
68 0         0 return 1;
69             }
70              
71             sub _stopwatch{
72 0     0   0 my $self = shift;
73 0         0 my $label = shift;
74              
75 0         0 my ( $package, $filename, $line, $method ) = caller( 1 ); # First caller up
76 0   0     0 $method ||= '';
77 0         0 my ($m) = $method =~ /([^\:]*)$/;
78              
79 0 0       0 if($label){
80 0         0 my $elapsed = Time::HiRes::time() - $TIMERS{$method};
81 0         0 my $seconds = sprintf('%.8f',$elapsed);
82 0         0 $self->_logDebug2( "$m ($label) took $seconds seconds");
83             }
84              
85 0         0 $TIMERS{ $method } = Time::HiRes::time(); # Logger could be slow
86              
87 0         0 return 1;
88             }
89              
90             sub _log {
91 0 0   0   0 my $s = shift->_session or return 1;
92 0         0 $s->_log( shift, 'INFO' );
93 0         0 return 1
94             }
95             sub _logDebug {
96 34 50   34   266 my $s = shift->_session or return 1;
97 34         161 $s->_log( shift, 'DEBUG' );
98 34         92 return 1
99             }
100             sub _logDebug2 {
101 1292 50   1292   9558 my $s = shift->_session or return 1;
102 1292         24662 $s->_log( shift, 'DEBUG2' );
103 1292         3843 return 1
104             }
105             sub _logDebug3 {
106 236 50   236   1149 my $s = shift->_session or return 1;
107 236         1010 $s->_log( shift, 'DEBUG3' );
108 236         537 return 1
109             }
110              
111             sub _warn {
112 0 0   0   0 my $s = shift->_session or return 1;
113 0         0 $s->_log( shift, 'WARN' );
114 0         0 return 1
115             }
116              
117             sub _error {
118 7     7   30 my $s = shift->_session;
119              
120 7 50 33     65 if(!$s || $s->use_exceptions){
121 0         0 local $Carp::CarpLevel = 1;
122 0         0 croak shift;
123             }
124              
125              
126 7 50       19 if($s){
127 7         30 $s->_log( shift, 'ERROR' )
128             }else{
129 0         0 print STDERR "DBR ERROR: " . shift() . "\n";
130             }
131 7         60 return undef;
132             }
133              
134 412     412   2124 sub _session { $_[0]->{session} }
135 0     0 0   sub is_debug { $_[0]->{debug} }
136              
137             package DBR::Common::DummySession;
138              
139              
140             # sub _error {
141             # my $self = shift;
142             # my $message = shift;
143              
144             # my ( $package, $filename, $line, $method) = caller(1);
145             # if ($self->session){
146             # $self->session->logErr($message,$method);
147             # }else{
148             # print STDERR "DBR ERROR: $message ($method, line $line)\n";
149             # }
150             # return undef;
151             # }
152              
153             # sub _logDebug{
154             # my $self = shift;
155             # my $message = shift;
156             # my ( $package, $filename, $line, $method) = caller(1);
157             # if ($self->session){
158             # $self->session->logDebug($message,$method);
159             # }elsif($self->is_debug){
160             # print STDERR "DBR DEBUG: $message\n";
161             # }
162             # }
163             # sub _logDebug2{
164             # my $self = shift;
165             # my $message = shift;
166             # my ( $package, $filename, $line, $method) = caller(1);
167             # if ($self->session){
168             # $self->session->logDebug2($message,$method);
169             # }elsif($self->is_debug){
170             # print STDERR "DBR DEBUG2: $message\n";
171             # }
172             # }
173             # sub _logDebug3{
174             # my $self = shift;
175             # my $message = shift;
176             # my ( $package, $filename, $line, $method) = caller(1);
177             # if ($self->session){
178             # $self->session->logDebug3($message,$method);
179             # }elsif($self->is_debug){
180             # print STDERR "DBR DEBUG3: $message\n";
181             # }
182              
183             # }
184              
185             # #HERE HERE HERE - do some fancy stuff with dummy subroutines in the symbol table if nobody is in debug mode
186              
187             # sub _log{
188             # my $self = shift;
189             # my $message = shift;
190             # my ( $package, $filename, $line, $method) = caller(1);
191             # if ($self->session){
192             # $self->session->log($message,$method,'INFO');
193             # }else{
194             # print STDERR "DBR: $message\n";
195             # }
196             # return 1;
197             # }
198              
199             1;