CLI.pm

filter: Statementstmt 27.9% 43 / 154 Branchbran 14.8% 11 / 74 Conditioncond 8.7% 2 / 23 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 32.0% 8 / 25 Podpod 100% 17 / 17 totaltotal 26.3% 81 / 308 CC 48 SCAR 29.8
CC
48
Cov
22%
read
42.0
new
38.3
is_open
20.0
CRAP 19.7
↑ Summary CLI/POP3.pm »
1 package IO::Socket::CLI;
2 2 $IO::Socket::CLI::VERSION = '0.041';
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use IO::Socket::SSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use IO::Socket::INET6;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # defaults
11 2 my $DEBUG = 0; # boolean?
12 2 my $DELAY = 10; # number of milliseconds between each attempt at reading the response from the server.
13 2 my $TIMEOUT = 5; # number of seconds to wait for a response from server before returning an empty list.
14 2 my $PRINT_RESPONSE = 1; # boolean
15 2 my $PREPEND = 1; # boolean
16 2 our $SSL = 0; # boolean
17 2 my $HOST = '127.0.0.1'; # IP or domain
18 2 our $PORT = '143'; # port
19 2 our $BYE = qr'^\* BYE( |\r?$)'; # string server sends when it hangs up.
20  
21 1 ▶sub new {
Subroutine new: called CC=15 SCAR=36.4
Pod documented
22 1 my $this = shift;
23 1 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
24 1 my $self = {};
25 1 ▶ my $args = (ref($_[0]) eq 'HASH') ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 1 1
26  
27 1 ▶ $self->{_HOST} = ($args->{HOST}) ? $args->{HOST} : $HOST;
Branch: 1 branch
BranchTrueFalseTotal
$args->{'HOST'} ? : 1 0 1
28 1 ▶ $self->{_PORT} = ($args->{PORT}) ? $args->{PORT} : $PORT;
Branch: 1 branch
BranchTrueFalseTotal
$args->{'PORT'} ? : 1 0 1
29 1 ▶ $self->{_BYE} = ($args->{BYE}) ? $args->{BYE} : $BYE;
Branch: 1 branch
BranchTrueFalseTotal
$args->{'BYE'} ? : 0 1 1
30 1 ▶ $self->{_DELAY} = ($args->{DELAY}) ? $args->{DELAY} : $DELAY;
Branch: 1 branch
BranchTrueFalseTotal
$args->{'DELAY'} ? : 0 1 1
31 1 ▶ $self->{_TIMEOUT} = ($args->{TIMEOUT}) ? $args->{TIMEOUT} : $TIMEOUT;
Branch: 1 branch
BranchTrueFalseTotal
$args->{'TIMEOUT'} ? : 0 1 1
32 1 ▶ $self->{_PRINT_RESPONSE} = (defined $args->{PRINT_RESPONSE}) ? $args->{PRINT_RESPONSE} : $PRINT_RESPONSE;
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'PRINT_RESPONSE'} ? : 0 1 1
33 1 ▶ $self->{_PREPEND} = (defined $args->{PREPEND}) ? $args->{PREPEND} : $PREPEND;
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'PREPEND'} ? : 0 1 1
34 1 ▶ $self->{_DEBUG} = (defined $args->{DEBUG}) ? $args->{DEBUG} : $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'DEBUG'} ? : 0 1 1
35 1 ▶ $self->{_SSL} = (defined $args->{SSL}) ? $args->{SSL} : $SSL;
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'SSL'} ? : 0 1 1
36 $self->{_SOCKET} = IO::Socket::INET6->new(PeerAddr => $self->{_HOST},
37 PeerPort => $self->{_PORT},
38 1 ▶ Blocking => 0) ||
Condition50%1/2
'IO::Socket::INET6'->new('PeerAddr', $self->{'_HOST'}, 'PeerPort', $self->{'_PORT'}, 'Blocking', 0) || die("Can't bind : $@\n")
l!l
01
MC/DC0%0/1
'IO::Socket::INET6'->new('PeerAddr', $self->{'_HOST'}, 'PeerPort', $self->{'_PORT'}, 'Blocking', 0) || die("Can't bind : $@\n")
'IO::Socket::INET6'->new('PeerAddr', $self->{'_HOST'}, 'PeerPort', $self->{'_PORT'}, 'Blocking', 0)
Truth table50%1/2
A
A= 'IO::Socket::INET6'->new('PeerAddr', $self->{'_HOST'}, 'PeerPort', $self->{'_PORT'}, 'Blocking', 0)
Aresult
11
00
39 die "Can't bind : $@\n";
40  
41 0 ▶ ($self->{_SSL}) and IO::Socket::SSL->start_SSL($self->{_SOCKET});
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_SSL"} and "IO::Socket::SSL"->start_SSL($self->{"_SOCKET"}) 0 0 0
42 0 ▶ $self->{_OPEN} = ($self->{_SOCKET}->connected()) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_SOCKET'}->connected ? : 0 0 0
43 0 $self->{_COMMAND} = '';
44 0 $self->{_SERVER_RESPONSE} = [];
45  
46 0 bless ($self, $class);
47 0 return $self;
48 }
49  
50 0 ▶sub read {
Subroutine read: not called CC=6 SCAR=37.4
Pod documented
51 0 my $self = shift;
52 0 my $i = 0;
53 0 my $max_i = $self->{_TIMEOUT} / ($self->{_DELAY} / 1000);
54  
55 do {
56 0 select(undef, undef, undef, $self->{_DELAY} / 1000);
57 0 @{$self->{_SERVER_RESPONSE}} = $self->{_SOCKET}->getlines;
58 0 $i++;
59 0 ▶ } while (!@{$self->{_SERVER_RESPONSE}} && $i < $max_i);
Branch: 1 branch
BranchTrueFalseTotal
not @{$self->{"_SERVER_RESPONSE"};} and $i < $max_i and do { do { select undef, undef, undef, $self->{"_DELAY"} / 1000; @{$$self{"_SERVER_RESPONSE"};} = $self->{"_SOCKET"}->getlines; ++$i } } 0 0 0
Condition0%0/3
not @{$self->{"_SERVER_RESPONSE"};} and $i < $max_i
!ll&&!rl&&r
000
MC/DC0%0/2
not @{$self->{"_SERVER_RESPONSE"};} and $i < $max_i
not @{$self->{"_SERVER_RESPONSE"};} $i < $max_i
Truth table0%0/3
not(A) and B
A= not @{$self->{"_SERVER_RESPONSE"};}B= $i < $max_i
ABresult
0X0
100
111
60  
61 0 ▶ if ($DEBUG || $self->{_DEBUG}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG or $self->{"_DEBUG"}) 0 0 0
Condition0%0/3
$DEBUG or $self->{"_DEBUG"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$DEBUG or $self->{"_DEBUG"}
$DEBUG $self->{"_DEBUG"}
Truth table0%0/3
A or B
A= $DEBUGB= $self->{"_DEBUG"}
ABresult
1X1
011
000
62 0 print STDOUT "D: response took roughly " . ($i * $self->{_DELAY}) . " milliseconds\n";
63 }
64  
65 0 ▶ $self->print_resp() if ($self->{_PRINT_RESPONSE});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_PRINT_RESPONSE"} 0 0 0
66  
67 0 return @{$self->{_SERVER_RESPONSE}};
68 }
69  
70 0 ▶sub response {
Subroutine response: not called CC=1 SCAR=6.9
Pod documented
71 0 my $self = shift;
72 0 return @{$self->{_SERVER_RESPONSE}};
73 }
74  
75 0 ▶sub print_resp {
Subroutine print_resp: not called CC=3 SCAR=24.8
Pod documented
76 0 my $self = shift;
77 0 foreach (@{$self->{_SERVER_RESPONSE}}) {
78 0 ▶ print STDOUT "" . (($self->{_PREPEND}) ? "S: " : "") . "$_";
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_PREPEND'} ? : 0 0 0
79 }
80 }
81  
82 0 ▶sub is_open {
Subroutine is_open: not called CC=4 SCAR=30.0
Pod documented
83 0 my $self = shift;
84 0 my $bye = $self->{_BYE};
85 0 ▶ $self->{_OPEN} = ($self->{_SOCKET}->connected()) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_SOCKET'}->connected ? : 0 0 0
86 0 foreach (@{$self->{_SERVER_RESPONSE}}) {
87 0 ▶ $self->{_OPEN} = 0 if (/$bye/);
Branch: 1 branch
BranchTrueFalseTotal
if /$bye/ 0 0 0
88 0 last;
89 }
90 0 return $self->{_OPEN};
91 }
92  
93 0 ▶sub send($) {
Subroutine send: not called CC=3 SCAR=24.8
Pod documented
94 0 my $self = shift;
95 0 chomp (my $command = shift);
96 0 $self->{_COMMAND} = $command;
97 0 ▶ print STDOUT "" . ($self->{_PREPEND} ? "C: " : "") . "$command\r\n" if ($self->{_PRINT_RESPONSE});
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"_PRINT_RESPONSE"} 0 0 0
$self->{'_PREPEND'} ? : 0 0 0
98 0 $self->{_SOCKET}->syswrite("$command\r\n");
99 }
100  
101 0 ▶sub prompt {
Subroutine prompt: not called CC=2 SCAR=17.9
Pod documented
102 0 my $self = shift;
103 0 ▶ print STDOUT "C: " if ($self->{_PREPEND}); # client prompt
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_PREPEND"} 0 0 0
104 0 chomp(my $command = <STDIN>);
105 0 $self->{_COMMAND} = $command;
106 0 $self->{_SOCKET}->syswrite("$command\r\n");
107 }
108  
109 0 ▶sub command() {
Subroutine command: not called CC=1 SCAR=6.9
Pod documented
110 0 my $self = shift;
111 0 return $self->{_COMMAND};
112 }
113  
114 0 ▶sub print_response {
Subroutine print_response: not called CC=4 SCAR=30.0
Pod documented
115 0 my $self = shift;
116 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
117 0 my $boolean = shift;
118 0 ▶ if ($boolean and $boolean != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($boolean and $boolean != 1) 0 0 0
Condition0%0/3
$boolean and $boolean != 1
!ll&&!rl&&r
000
MC/DC0%0/2
$boolean and $boolean != 1
$boolean $boolean != 1
Truth table0%0/3
A and B
A= $booleanB= $boolean != 1
ABresult
0X0
100
111
119 0 carp "warning: valid settings for print_response() are 0 or 1 -- setting to $PRINT_RESPONSE";
120 0 $boolean = $PRINT_RESPONSE;
121 }
122 0 $self->{_PRINT_RESPONSE} = $boolean;
123 }
124 0 return $self->{_PRINT_RESPONSE};
125 }
126  
127 0 ▶sub prepend {
Subroutine prepend: not called CC=4 SCAR=30.0
Pod documented
128 0 my $self = shift;
129 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
130 0 my $boolean = shift;
131 0 ▶ if ($boolean and $boolean != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($boolean and $boolean != 1) 0 0 0
Condition0%0/3
$boolean and $boolean != 1
!ll&&!rl&&r
000
MC/DC0%0/2
$boolean and $boolean != 1
$boolean $boolean != 1
Truth table0%0/3
A and B
A= $booleanB= $boolean != 1
ABresult
0X0
100
111
132 0 carp "warning: valid settings for prepend() are 0 or 1 -- setting to $PREPEND";
133 0 $boolean = $PREPEND;
134 }
135 0 $self->{_PREPEND} = $boolean;
136 }
137 0 return $self->{_PREPEND};
138 }
139  
140 0 ▶sub timeout {
Subroutine timeout: not called CC=3 SCAR=24.8
Pod documented
141 0 my $self = shift;
142 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
143 0 my $seconds = shift;
144 0 ▶ if ($seconds < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seconds < 0) 0 0 0
145 0 carp "warning: timeout() must be non-negative -- setting to $TIMEOUT";
146 0 $seconds = $TIMEOUT;
147 }
148 0 $self->{_TIMEOUT} = $seconds;
149 }
150 0 return $self->{_TIMEOUT};
151 }
152  
153 0 ▶sub delay {
Subroutine delay: not called CC=3 SCAR=24.8
Pod documented
154 0 my $self = shift;
155 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
156 0 my $milliseconds = shift;
157 0 ▶ if ($milliseconds < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($milliseconds < 1) 0 0 0
158 0 carp "warning: delay() must be positive -- setting to $DELAY";
159 0 $milliseconds = $DELAY;
160 }
161 0 $self->{_DELAY} = $milliseconds;
162 }
163 0 return $self->{_DELAY};
164 }
165  
166 0 ▶sub bye {
Subroutine bye: not called CC=3 SCAR=24.8
Pod documented
167 0 my $self = shift;
168 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
169 0 my $bye = shift;
170 0 ▶ unless ($bye =~ /\(\?(?:-xism|\^):.*\)/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bye =~ /\(\?(?:-xism|\^):.*\)/) 0 0 0
171 0 carp "warning: bye() must be a regexp-like quote: qr/STRING/ -- setting to '$BYE' instead of '$bye'";
172 0 $bye = $BYE;
173 }
174 0 $self->{_BYE} = $bye;
175 }
176 0 return $self->{_BYE};
177 }
178  
179 0 ▶sub debug {
Subroutine debug: not called CC=4 SCAR=30.0
Pod documented
180 0 my $self = shift;
181 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
182 0 my $boolean = shift;
183 0 ▶ if ($boolean and $boolean != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($boolean and $boolean != 1) 0 0 0
Condition0%0/3
$boolean and $boolean != 1
!ll&&!rl&&r
000
MC/DC0%0/2
$boolean and $boolean != 1
$boolean $boolean != 1
Truth table0%0/3
A and B
A= $booleanB= $boolean != 1
ABresult
0X0
100
111
184 0 carp "warning: valid settings for debug() are 0 or 1 -- setting to 1";
185 0 $boolean = 1;
186 }
187 0 $self->{_DEBUG} = $boolean;
188 }
189 0 return $self->{_DEBUG};
190 }
191  
192 #sub debug {
193 # my $self = shift;
194 # confess 'error: thing->debug($level)' unless @_ == 1;
195 # my $level = shift;
196 # if (ref($self)) {
197 # $self->{_DEBUG} = $level; # just myself
198 # } else {
199 # $DEBUG = $level; # whole class
200 # }
201 #}
202  
203 0 ▶sub socket {
Subroutine socket: not called CC=1 SCAR=6.9
Pod documented
204 0 my $self = shift;
205 0 return $self->{_SOCKET};
206 }
207  
208 0 ▶sub errstr {
Subroutine errstr: not called CC=2 SCAR=17.9
Pod documented
209 0 my $self = shift;
210 0 ▶ if ($self->{_SSL}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_SSL'}) { } 0 0 0
211 0 return $self->{_SOCKET}->errstr();
212 } else {
213 0 return undef;
214 }
215 }
216  
217 0 ▶sub close {
Subroutine close: not called CC=2 SCAR=17.9
Pod documented
218 0 my $self = shift;
219 0 return $self->{_SOCKET}->close();
220 0 ▶ if ($self->{_SSL}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_SSL'}) { } 0 0 0
221 0 return $self->{_SOCKET}->stop_SSL(SSL_ctx_free => 1);
222 } else {
223 0 return $self->{_SOCKET}->close();
224 }
225 }
226  
227 # object destructor
228 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=3 SCAR=24.8
229 0 my $self = shift;
230 0 ▶ if ($DEBUG || $self->{"_DEBUG"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG or $self->{"_DEBUG"}) 0 0 0
Condition0%0/3
$DEBUG or $self->{"_DEBUG"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$DEBUG or $self->{"_DEBUG"}
$DEBUG $self->{"_DEBUG"}
Truth table0%0/3
A or B
A= $DEBUGB= $self->{"_DEBUG"}
ABresult
1X1
011
000
231 0 carp "Destroying $self " . $self->{_HOST} . ":" . $self->{_PORT};
232 }
233 0 $self->close();
234 }
235  
236 # class destructor
237 1 ▶sub END {
Subroutine END: called CC=2 SCAR=9.2
238 1 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG) 0 1 1
239 0 print STDOUT "class destroyed.\n";
240 }
241 }
242  
243 2 1;
244  
245 __END__
↑ Summary CLI/POP3.pm »