blib/lib/Text/Netstring.pm

filter: Statementstmt 54.9% 28 / 51 Branchbran 30.5% 11 / 36 Conditioncond 76.1% 16 / 21 MC/DCmcdc 57.1% 8 / 14 Subroutinesub 83.3% 5 / 6 Podpod 100% 4 / 4 totaltotal 54.5% 72 / 132 CC 27 SCAR 43.5
CC
27
Cov
51%
netstring_read
182.0
netstring_verify
7.1
netstring_decode
6.5
CRAP 77.3
↑ Summary
1 package Text::Netstring;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw($VERSION @ISA @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 require Exporter;
7  
8 #
9 # Copyright (c) 2003-2006 James Raftery <james@now.ie>. All rights reserved.
10 # This program is free software; you can redistribute it and/or
11 # modify it under the same terms as Perl itself.
12 # Please submit bug reports, patches and comments to the author.
13 # Latest information at http://romana.now.ie/
14 #
15 # $Id: Netstring.pm,v 1.13 2006/11/20 18:28:49 james Exp $
16 #
17 # See the Text::Netstring man page that was installed with this module for
18 # information on how to use the module.
19 #
20  
21 1 @ISA = qw(Exporter);
22 # Items to export into caller's namespace by request.
23 1 @EXPORT_OK = qw(
24 netstring_encode netstring_decode netstring_verify netstring_read
25 );
26  
27 1 $VERSION = '0.07';
28  
29  
30 6 ▶sub netstring_encode {
Subroutine netstring_encode: called CC=4 SCAR=13.9
Pod documented
31  
32 # is argument a list reference?
33 6 ▶ @_ = @{$_[0]} if (scalar(@_)==1 and ref($_[0]) eq "ARRAY");
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ == 1 and ref $_[0] eq "ARRAY" 2 4 6
Condition100%3/3
scalar @_ == 1 and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
222
MC/DC100%2/2
scalar @_ == 1 and ref $_[0] eq "ARRAY"
scalar @_ == 1 ref $_[0] eq "ARRAY"
Truth table100%3/3
A and B
A= scalar @_ == 1B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
34  
35 6 my @enc = map { length($_).":${_}," } @_;
36 6 ▶ wantarray ? @enc : join("", @enc);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 4 6
37 }
38  
39 5 ▶sub netstring_decode {
Subroutine netstring_decode: called CC=6 SCAR=18.7
Pod documented
40  
41 # is argument a list reference?
42 0 ▶ @_ = @{$_[0]} if (scalar(@_)==1 and ref($_[0]) eq "ARRAY");
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ == 1 and ref $_[0] eq "ARRAY" 0 5 5
Condition66%2/3
scalar @_ == 1 and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
140
MC/DC0%0/2
scalar @_ == 1 and ref $_[0] eq "ARRAY"
scalar @_ == 1 ref $_[0] eq "ARRAY"
Truth table66%2/3
A and B
A= scalar @_ == 1B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
43  
44 5 ▶ my @dec = map { /^(\d+):(.*),$/s and length($2)==$1 ? $2 : "" } @_;
Branch: 1 branch
BranchTrueFalseTotal
length $2 == $1 ? : 5 0 5
Condition100%3/3
/^(\d+):(.*),$/s && (length $2 == $1 ? $2 : '')
!ll&&!rl&&r
114
MC/DC100%2/2
/^(\d+):(.*),$/s && (length $2 == $1 ? $2 : '')
/^(\d+):(.*),$/s (length $2 == $1 ? $2 : '')
Truth table100%3/3
A && B
A= /^(\d+):(.*),$/sB= (length $2 == $1 ? $2 : '')
ABresult
0X0
100
111
45 5 ▶ wantarray ? @dec : join("", @dec);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 4 5
46 }
47  
48 17 ▶sub netstring_verify {
Subroutine netstring_verify: called CC=7 SCAR=19.6
Pod documented
49  
50 # is argument a list reference?
51 0 ▶ @_ = @{$_[0]} if (scalar(@_)== 1 and ref($_[0]) eq "ARRAY");
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ == 1 and ref $_[0] eq "ARRAY" 0 17 17
Condition66%2/3
scalar @_ == 1 and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
5120
MC/DC0%0/2
scalar @_ == 1 and ref $_[0] eq "ARRAY"
scalar @_ == 1 ref $_[0] eq "ARRAY"
Truth table66%2/3
A and B
A= scalar @_ == 1B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
52  
53 17 ▶ my @ver = map { /^(\d+):(.*),$/s and length($2)==$1 } @_;
Condition100%3/3
/^(\d+):(.*),$/s && length $2 == $1
!ll&&!rl&&r
1079
MC/DC100%2/2
/^(\d+):(.*),$/s && length $2 == $1
/^(\d+):(.*),$/s length $2 == $1
Truth table100%3/3
A && B
A= /^(\d+):(.*),$/sB= length $2 == $1
ABresult
0X0
100
111
54 17 ▶ wantarray ? @ver : do { my $i=shift(@ver); foreach (@ver) {$i &&= $_}; $i };
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 15 17
Condition100%3/3
$i &&= $_
!ll&&!rl&&r
311
MC/DC100%2/2
$i &&= $_
$i $_
Truth table100%3/3
A &&= B
A= $iB= $_
ABresult
0X0
100
111
55 }
56  
57 0 ▶sub netstring_read {
Subroutine netstring_read: not called CC=13 SCAR=52.0
Pod documented
58 0 ▶ my $sock = shift or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $sock = shift() or return undef 0 0 0
59  
60 0 my($r, $ns);
61 0 my $s = "";
62 0 my $len = 0;
63  
64 # read the length
65 0 for (;;) {
66 0 ▶ defined($r = read($sock, $s, 1)) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
defined($r = read($sock, $s, 1)) or return undef 0 0 0
67  
68 0 ▶ return "" if !$r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 0 0 0
69 0 ▶ last if $s eq ":";
Branch: 1 branch
BranchTrueFalseTotal
if $s eq ":" 0 0 0
70 0 ▶ return undef if $s !~ /^[0-9]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $s =~ /^[0-9]$/ 0 0 0
71  
72 0 $len = 10 * $len + $s;
73 0 ▶ return undef if $len > 200000000;
Branch: 1 branch
BranchTrueFalseTotal
if $len > 200000000 0 0 0
74 }
75  
76 0 $ns = $len . ":";
77 0 $s = "";
78  
79 # read the string 'body'
80 0 ▶ defined($r = read($sock, $s, $len)) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
defined($r = read($sock, $s, $len)) or return undef 0 0 0
81 0 ▶ return "" if (!$r and $len != 0); # zero length is OK
Branch: 1 branch
BranchTrueFalseTotal
if not $r and $len != 0 0 0 0
Condition0%0/3
not $r and $len != 0
!ll&&!rl&&r
000
MC/DC0%0/2
not $r and $len != 0
not $r $len != 0
Truth table0%0/3
not(A) and B
A= not $rB= $len != 0
ABresult
0X0
100
111
82 0 $ns .= $s;
83  
84 # read the trailing comma
85 0 ▶ defined($r = read($sock, $s, 1)) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
defined($r = read($sock, $s, 1)) or return undef 0 0 0
86 0 ▶ return "" if !$r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 0 0 0
87 0 ▶ return undef if $s ne ",";
Branch: 1 branch
BranchTrueFalseTotal
if $s ne "," 0 0 0
88 0 $ns .= $s;
89  
90 0 return $ns;
91 }
92  
93 1 1;
94  
95 __END__
↑ Summary