blib/lib/Parse/CPinfo.pm

filter: Statementstmt 91.7% 111 / 121 Branchbran 88.2% 30 / 34 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 93.3% 14 / 15 Podpod 100% 7 / 7 totaltotal 89.5% 163 / 182 CC 25 SCAR 18.0
CC
25
Cov
90%
_parseinterfacelist
11.0
_ipv4_msk2cidr
6.8
readfile
6.0
CRAP 6.1
↑ Summary
1 package Parse::CPinfo;
2  
3 3 ▶use 5.006_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use base qw/Exporter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 require Exporter;
10  
11 3 our $VERSION = '0.882';
12  
13 # extracted from Regexp::Common
14 3 my $re_mac =
15 '(?:(?:[0-9a-fA-F]{1,2}):(?:[0-9a-fA-F]{1,2}):(?:[0-9a-fA-F]{1,2}):(?:[0-9a-fA-F]{1,2}):(?:[0-9a-fA-F]{1,2}):(?:[0-9a-fA-F]{1,2}))';
16 3 my $re_net_ipv4 =
17 '(?:(?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2}))';
18  
19 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
20 2 my $class = shift;
21 2 my $self = {};
22 2 bless $self, $class;
23 2 return $self;
24 }
25  
26 2 ▶sub readfile {
Subroutine readfile: called CC=6 SCAR=17.9
Pod documented
27 2 my $self = shift;
28 2 my $filename = shift;
29  
30 # keep copy of filename in object
31 2 $self->{'_filename'} = $filename;
32 2 my $fh = new IO::File($filename, 'r');
33 2 ▶ if (!defined($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fh) 1 1 2
34 1 croak "Unable to open $filename for reading";
35 }
36  
37 # ensure we are in binary mode as some system (win32 for example)
38 # will assume we are handling text otherwise.
39 1 binmode $fh, ':raw';
40  
41 1 my @lines = <$fh>;
42 1 chomp @lines;
43 1 my $linenumber = 0;
44 1 ▶ while ($linenumber < $#lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($linenumber < $#lines) 459 1 460
45 459 $linenumber++;
46 459 ▶ if ($lines[$linenumber] =~ m/^\={46}$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines[$linenumber] =~ /^\={46}$/o) 18 441 459
47 18 $linenumber++;
48 18 my $section = $lines[$linenumber];
49 18 $linenumber++;
50  
51 18 foreach (0 .. $linenumber) {
52 1801 $linenumber++;
53 1801 ▶ if ($lines[$linenumber] !~ m/^\={46}$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $lines[$linenumber] =~ /^\={46}$/o) { } 1786 15 1801
54 1786 $lines[$linenumber] =~ s/\r\n//g;
55 1786 $lines[$linenumber] =~ s/\r//g;
56 1786 $lines[$linenumber] =~ s/\n//g;
57 1786 $self->{'config'}->{$section} = $self->{'config'}->{$section} . "$lines[$linenumber]\n";
58 }
59 else {
60 15 $linenumber--;
61 15 last;
62 }
63 }
64 }
65 }
66 1 $self->_parseinterfacelist();
67 1 return 1;
68 }
69  
70 1 ▶sub _parseinterfacelist {
Subroutine _parseinterfacelist: called CC=11 SCAR=24.0
71 1 my $self = shift;
72 1 my $ifconfigtext = $self->{'config'}->{'IP Interfaces'};
73 1 ▶ if (!$ifconfigtext) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ifconfigtext) 0 1 1
74 0 return;
75 }
76 1 my @s = split /\n/, $ifconfigtext;
77 1 my ($int);
78 1 foreach my $line (@s) {
79 20 chomp $line;
80 20 ▶ if ($line =~ m/^(\w+)\s+/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(\w+)\s+/o) 3 17 20
81 3 my $match = $1;
82 3 ▶ if ($match !~ m/ifconfig/o) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($match =~ /ifconfig/o) 2 1 3
83 2 $int = $1;
84 2 $self->{'interface'}->{$int}->{'name'} = $int;
85 }
86 }
87 20 ▶ if ($line =~ m/Link encap:(\w+)\s+/io) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /Link encap:(\w+)\s+/io) 2 18 20
88 2 $self->{'interface'}->{$int}->{'encap'} = $1;
89 }
90 20 ▶ if ($line =~ m/HWaddr ($re_mac)/io) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /HWaddr ($re_mac)/io) 1 19 20
91 1 $self->{'interface'}->{$int}->{'hwaddr'} = $1;
92 }
93 20 ▶ if ($line =~ m/inet addr:($re_net_ipv4)/io) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /inet addr:($re_net_ipv4)/io) 2 18 20
94 2 $self->{'interface'}->{$int}->{'inetaddr'} = $1;
95 }
96 20 ▶ if ($line =~ m/bcast:($re_net_ipv4)/io) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /bcast:($re_net_ipv4)/io) 1 19 20
97 1 $self->{'interface'}->{$int}->{'broadcast'} = $1;
98 }
99 20 ▶ if ($line =~ m/mask:($re_net_ipv4)/io) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /mask:($re_net_ipv4)/io) 2 18 20
100 2 $self->{'interface'}->{$int}->{'mask'} = $1;
101 2 $self->{'interface'}->{$int}->{'masklength'} = $self->_ipv4_msk2cidr($self->{'interface'}->{$int}->{'mask'});
102 }
103 20 ▶ if ($line =~ m/MTU:(\d+)/io) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /MTU:(\d+)/io) 2 18 20
104 2 $self->{'interface'}->{$int}->{'mtu'} = $1;
105 }
106 }
107 1 return 1;
108 }
109  
110 2 ▶sub _ipv4_msk2cidr {
Subroutine _ipv4_msk2cidr: called CC=6 SCAR=19.2
111 2 my $self = shift;
112 2 my $mask = shift;
113 2 ($mask) = $mask =~ m/(\d+\.\d+\.\d+\.\d+)/o;
114  
115 2 ▶ if (! defined($mask)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $mask) 0 2 2
116 0 return undef;
117 }
118  
119 2 for (split /\./, $mask) {
120 8 ▶ if ($_ < 0 or $_ > 255) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ < 0 or $_ > 255) 0 8 8
Condition33%1/3
$_ < 0 or $_ > 255
l!l&&r!l&&!r
008
MC/DC0%0/2
$_ < 0 or $_ > 255
$_ < 0 $_ > 255
Truth table33%1/3
A or B
A= $_ < 0B= $_ > 255
ABresult
1X1
011
000
121 0 return undef;
122 }
123 }
124 2 my @bytes = split /\./, $mask;
125  
126 2 my $cidr = 0;
127 2 for (@bytes) {
128 8 my $bits = unpack("B*", pack("C", $_));
129 8 $cidr += $bits =~ tr /1/1/;
130 }
131 2 return $cidr;
132 }
133  
134 1 ▶sub getInterfaceList {
Subroutine getInterfaceList: called CC=1 SCAR=0.0
Pod documented
135 1 my $self = shift;
136 1 return keys %{$self->{'interface'}};
137 }
138  
139 2 ▶sub getInterfaceInfo {
Subroutine getInterfaceInfo: called CC=1 SCAR=0.0
Pod documented
140 2 my $self = shift;
141 2 my $interface = shift;
142 2 return $self->{'interface'}{$interface};
143 }
144  
145 0 ▶sub getSectionList {
Subroutine getSectionList: not called CC=2 SCAR=17.9
Pod documented
146 0 my $self = shift;
147 0 my @r;
148 0 foreach my $section (sort keys %{$self->{config}}) {
149 0 push @r, $section;
150 }
151 0 return @r;
152 }
153  
154 1 ▶sub getSection {
Subroutine getSection: called CC=1 SCAR=0.0
Pod documented
155 1 my $self = shift;
156 1 my $query = shift;
157 1 my $section = $self->{'config'}->{$query};
158 1 return $section;
159 }
160  
161 1 ▶sub getHostname {
Subroutine getHostname: called CC=4 SCAR=14.0
Pod documented
162 1 my $self = shift;
163 1 my @section = split(/\n/, $self->getSection('System Information'));
164 1 my $hostname;
165 1 foreach my $linenumber (0 .. $#section) {
166 14 ▶ if ($section[$linenumber] =~ m/Issuing 'hostname'/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($section[$linenumber] =~ /Issuing 'hostname'/i) 1 13 14
167 1 $hostname = $section[$linenumber + 2];
168 1 chomp $hostname;
169 1 last;
170 }
171 }
172 1 ▶ if (defined($hostname)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hostname) { } 1 0 1
173 1 return $hostname;
174 }
175 else {
176 0 return undef;
177 }
178 }
179  
180 3 1;
181 __END__
↑ Summary