blib/lib/Chemistry/PeriodicTable.pm

filter: Statementstmt 100% 93 / 93 Branchbran 83.3% 20 / 24 Conditioncond 88.8% 16 / 18 MC/DCmcdc 80.0% 8 / 10 Subroutinesub 100% 15 / 15 Podpod 100% 5 / 5 totaltotal 95.1% 157 / 165 CC 23 SCAR 12.9
CC
23
Cov
96%
name
5.0
symbol
5.0
value
5.0
CRAP 3.6
↑ Summary
1 package Chemistry::PeriodicTable;
2 2 our $AUTHORITY = 'cpan:GENE';
3  
4 # ABSTRACT: Provide access to chemical element properties
5  
6 2 our $VERSION = '0.0502';
7  
8 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use File::ShareDir qw(dist_dir);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use List::SomeUtils qw(first_index);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Text::CSV_XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 2 has symbols => (is => 'lazy', init_args => undef);
18  
19 1 ▶sub _build_symbols {
Subroutine _build_symbols: called CC=4 SCAR=13.9
20 1 my ($self) = @_;
21  
22 1 my $file = $self->as_file;
23  
24 1 my %data;
25  
26 1 my $csv = Text::CSV_XS->new({ binary => 1 });
27  
28 1 ▶ open my $fh, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "Can't read ${file}: $!" 1 0 1
29 or die "Can't read $file: $!";
30  
31 1 my $counter = 0;
32  
33 1 ▶ while (my $row = $csv->getline($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $csv->getline($fh)) 119 1 120
34 119 $counter++;
35  
36 # skip the first row
37 119 ▶ next if $counter == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $counter == 1 1 118 119
38  
39 118 $data{ $row->[2] } = $row;
40 }
41  
42 1 close $fh;
43  
44 1 return \%data;
45 }
46  
47  
48 2 has header => (is => 'lazy', init_args => undef);
49  
50 1 ▶sub _build_header {
Subroutine _build_header: called CC=3 SCAR=11.1
51 1 my ($self) = @_;
52  
53 1 my $file = $self->as_file;
54  
55 1 my @headers;
56  
57 1 my $csv = Text::CSV_XS->new({ binary => 1 });
58  
59 1 ▶ open my $fh, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "Can't read ${file}: $!" 1 0 1
60 or die "Can't read $file: $!";
61  
62 1 ▶ while (my $row = $csv->getline($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $csv->getline($fh)) 1 0 1
63 1 push @headers, @$row;
64 1 last;
65 }
66  
67 1 close $fh;
68  
69 1 return \@headers;
70 }
71  
72  
73 3 ▶sub as_file {
Subroutine as_file: called CC=3 SCAR=11.8
Pod documented
74 3 my ($self) = @_;
75  
76 3 my $file = eval { dist_dir('Chemistry-PeriodicTable') . '/Periodic-Table.csv' };
77 3 ▶ $file = 'share/Periodic-Table.csv'
Branch: 1 branch
BranchTrueFalseTotal
unless $file and -e $file 0 3 3
Condition33%1/3
$file and -e $file
!ll&&!rl&&r
003
MC/DC0%0/2
$file and -e $file
$file -e $file
Truth table33%1/3
A and B
A= $fileB= -e $file
ABresult
0X0
100
111
78 unless $file && -e $file;
79  
80 3 return $file;
81 }
82  
83  
84 2 ▶sub number {
Subroutine number: called CC=4 SCAR=13.9
Pod documented
85 2 my ($self, $string) = @_;
86 2 my $n;
87 # looking for a symbol
88 2 ▶ if (length $string < 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $string < 4) { } 1 1 2
89 1 $n = $self->symbols->{ ucfirst $string }[0];
90 }
91 # looking for an element name
92 else {
93 1 for my $symbol (keys %{ $self->symbols }) {
94 98 ▶ if (lc $self->symbols->{$symbol}[1] eq lc $string) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $self->symbols->{$symbol}[1] eq lc $string) 1 97 98
95 1 $n = $self->symbols->{$symbol}[0];
96 1 last;
97 }
98 }
99 }
100 2 return $n;
101 }
102  
103  
104 2 ▶sub name {
Subroutine name: called CC=5 SCAR=16.1
Pod documented
105 2 my ($self, $string) = @_;
106 2 my $n;
107 2 for my $symbol (keys %{ $self->symbols }) {
108 196 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^\d+$/ and $self->symbols->{$symbol}[0] == $string or lc $self->symbols->{$symbol}[2] eq lc $string) 2 194 196
Condition100%6/6
$string =~ /^\d+$/ and $self->symbols->{$symbol}[0] == $string or lc $self->symbols->{$symbol}[2] eq lc $string
l!l&&r!l&&!r
11194
$string =~ /^\d+$/ and $self->symbols->{$symbol}[0] == $string
!ll&&!rl&&r
98971
MC/DC100%3/3
$string =~ /^\d+$/ and $self->symbols->{$symbol}[0] == $string or lc $self->symbols->{$symbol}[2] eq lc $string
$string =~ /^\d+$/ $self->symbols->{$symbol}[0] == $string lc $self->symbols->{$symbol}[2] eq lc $string
Truth table80%4/5
A and B or C
A= $string =~ /^\d+$/B= $self->symbols->{$symbol}[0] == $stringC= lc $self->symbols->{$symbol}[2] eq lc $string
ABCresult
11X1
0X11
1011
0X00
1000
109 ($string =~ /^\d+$/ && $self->symbols->{$symbol}[0] == $string)
110 ||
111 (lc $self->symbols->{$symbol}[2] eq lc $string)
112 ) {
113 2 $n = $self->symbols->{$symbol}[1];
114 2 last;
115 }
116 }
117 2 return $n;
118 }
119  
120  
121 4 ▶sub symbol {
Subroutine symbol: called CC=5 SCAR=16.1
Pod documented
122 4 my ($self, $string) = @_;
123 4 my $s;
124 4 for my $symbol (keys %{ $self->symbols }) {
125 372 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^\d+$/ and $self->symbols->{$symbol}[0] == $string or lc $self->symbols->{$symbol}[1] eq lc $string) 4 368 372
Condition100%6/6
$string =~ /^\d+$/ and $self->symbols->{$symbol}[0] == $string or lc $self->symbols->{$symbol}[1] eq lc $string
l!l&&r!l&&!r
22368
$string =~ /^\d+$/ and $self->symbols->{$symbol}[0] == $string
!ll&&!rl&&r
1961742
MC/DC100%3/3
$string =~ /^\d+$/ and $self->symbols->{$symbol}[0] == $string or lc $self->symbols->{$symbol}[1] eq lc $string
$string =~ /^\d+$/ $self->symbols->{$symbol}[0] == $string lc $self->symbols->{$symbol}[1] eq lc $string
Truth table80%4/5
A and B or C
A= $string =~ /^\d+$/B= $self->symbols->{$symbol}[0] == $stringC= lc $self->symbols->{$symbol}[1] eq lc $string
ABCresult
11X1
0X11
1011
0X00
1000
126 ($string =~ /^\d+$/ && $self->symbols->{$symbol}[0] == $string)
127 ||
128 (lc $self->symbols->{$symbol}[1] eq lc $string)
129 ) {
130 4 $s = $symbol;
131 4 last;
132 }
133 }
134 4 return $s;
135 }
136  
137  
138 3 ▶sub value {
Subroutine value: called CC=5 SCAR=16.1
Pod documented
139 3 my ($self, $key, $string) = @_;
140 3 my $v;
141 3 ▶ my $idx = first_index { $_ =~ /$string/i } @{ $self->header };
Subroutine __ANON__: called CC=1 SCAR=0.0
142 3 ▶ if ($key !~ /^\d+$/ && length $key < 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $key =~ /^\d+$/ and length $key < 4) { } 1 2 3
Condition100%3/3
not $key =~ /^\d+$/ and length $key < 4
!ll&&!rl&&r
111
MC/DC100%2/2
not $key =~ /^\d+$/ and length $key < 4
not $key =~ /^\d+$/ length $key < 4
Truth table100%3/3
not(A) and B
A= not $key =~ /^\d+$/B= length $key < 4
ABresult
0X0
100
111
143 1 $v = $self->symbols->{$key}[$idx];
144 }
145 else {
146 2 $key = $self->symbol($key);
147 2 for my $symbol (keys %{ $self->symbols }) {
148 176 ▶ next unless $symbol eq $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $symbol eq $key 174 2 176
149 2 $v = $self->symbols->{$symbol}[$idx];
150 2 last;
151 }
152 }
153 3 return $v;
154 }
155  
156 2 1;
157  
158 __END__
↑ Summary