Backend/File.pm

filter: Statementstmt 100% 99 / 99 Branchbran 93.3% 28 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 83.3% 5 / 6 totaltotal 97.9% 141 / 144 CC 19 SCAR 20.2
CC
19
Cov
98%
new
13.0
commit
4.0
del
3.0
CRAP 7.5
↑ Summary Backend/String.pm »
1 package Config::Backend::File;
2  
3 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 ▶sub new {
Subroutine new: called CC=13 SCAR=25.7
Pod documented
7 11 my $class=shift;
8 11 my $filename=shift;
9 11 my $self;
10  
11 # Read in config
12  
13 11 my $str="";
14 11 my @order;
15 my %conf;
16 11 my $invar=0;
17 11 my $open=1;
18 11 my @vars_and_comments;
19  
20 11 ▶ open my $in,"<$filename" or $open=0;;
Branch: 1 branch
BranchTrueFalseTotal
open my $in, "<$filename" or $open = 0 10 1 11
21 11 ▶ if ($open) {
Branch: 1 branch
BranchTrueFalseTotal
if ($open) 10 1 11
22 10 ▶ while (my $line=<$in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $in)) 63 10 73
23 63 $str.=$line;
24 }
25 10 close $in;
26 }
27  
28 # Process Comments and %%
29  
30 11 my $i=0;
31 11 my $k=0;
32 11 my $N=length($str);
33  
34 11 ▶ while ($i<$N) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $N) 753 11 764
35 753 ▶ if (substr($str,$i,3) eq "\n%#") {
Branch: 3 branches
BranchTrueFalseTotal
if (substr($str, $i, 3) eq "\n%#") { } 8 745 753
elsif (substr($str, $i, 3) eq "\n%%") { } 10 735 745
elsif (substr($str, $i, 2) eq "\n%") { } 33 702 735
36 8 ▶ if ($k!=$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k != $i) 8 0 8
37 8 push @vars_and_comments,substr($str,$k,($i-$k));
38 8 $k=$i+1;
39 8 $i+=2;
40 }
41 } elsif (substr($str,$i,3) eq "\n%%") {
42 10 $i+=2;
43 } elsif (substr($str,$i,2) eq "\n%") {
44 33 ▶ if ($k!=$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k != $i) 33 0 33
45 33 push @vars_and_comments,substr($str,$k,($i-$k));
46 33 $k=$i+2;
47 33 $i+=2;
48 }
49 }
50 753 $i+=1;
51 }
52  
53 11 ▶ if ($k!=$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k != $i) 10 1 11
54 10 push @vars_and_comments,substr($str,$k,($N-$k));
55 }
56  
57 11 my $comment=0;
58 11 for my $item (@vars_and_comments) {
59 51 ▶ if ($item=~/^%#/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item =~ /^%#/) { } 12 39 51
60 12 push @order,"%#$comment,$item";
61 12 $comment+=1;
62 } else {
63 39 my ($var,$val)=split /=/,$item,2;
64 39 $var=~s/^%//;
65 39 push @order,$var;
66 39 $val=~s/\n%%/\n%/;
67 39 $conf{$var}=$val;
68 }
69 }
70  
71 # Set config to string
72  
73 11 $self->{"filename"}=$filename;
74 11 $self->{"order"}=\@order;
75 11 $self->{"conf"}=\%conf;
76 11 $self->{"changed"}=0;
77  
78 # bless
79  
80 11 bless $self,$class;
81  
82 11 return $self;
83 }
84  
85 11 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
86 11 my $self=shift;
87 11 $self->commit();
88 }
89  
90 76 ▶sub commit {
Subroutine commit: called CC=4 SCAR=13.9
Pod undocumented
91 76 my $self=shift;
92  
93 # Only commit on changes
94  
95 76 ▶ if (not $self->{"changed"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"changed"}) 4 72 76
96 4 return;
97 }
98  
99 # Commit to file
100  
101 72 my $filename=$self->{"filename"};
102 72 my $firstvar=1;
103 72 open my $out,">$filename";
104 72 my $delim="";
105 72 my $cdelim="";
106 72 for my $var (@{$self->{"order"}}) {
107 597 ▶ if ($var=~/^%#/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var =~ /^%#/) { } 21 576 597
108 21 my ($cm,$val)=split /,/,$var,2;
109 21 print $out "$cdelim$val";
110 }
111 else {
112 576 my $val=$self->{"conf"}->{"$var"};
113 576 $val=~s/\n%/\n%%/g;
114 576 print $out "$delim$var=$val";
115 }
116 597 $delim="\n%";
117 597 $cdelim="\n";
118 }
119  
120 72 close $out;
121 }
122  
123 39 ▶sub set {
Subroutine set: called CC=2 SCAR=6.9
Pod documented
124 39 my $self=shift;
125 39 my $var=shift;
126 39 my $val=shift;
127  
128 39 $self->{"changed"}=1;
129  
130 39 my $oldval=$self->get($var);
131 39 ▶ if (not exists $self->{"conf"}->{$var}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"conf"}{$var}) 29 10 39
132 29 push @{$self->{"order"}},$var;
133 }
134 39 $self->{"conf"}->{$var}=$val;
135  
136 39 $self->commit();
137 }
138  
139 164 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
140 164 my $self=shift;
141 164 my $var=shift;
142 164 return $self->{"conf"}->{$var};
143 }
144  
145 26 ▶sub del {
Subroutine del: called CC=3 SCAR=11.0
Pod documented
146 26 my ($self,$var)=@_;
147  
148 26 $self->{"changed"}=1;
149  
150 26 delete $self->{"conf"}->{$var};
151  
152 26 my @neworder;
153 26 for my $elem (@{$self->{"order"}}) {
154 241 ▶ if ($elem ne $var) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem ne $var) 217 24 241
155 217 push @neworder,$elem;
156 }
157 }
158 26 $self->{"order"}=\@neworder;
159  
160 26 $self->commit();
161 }
162  
163 14 ▶sub variables {
Subroutine variables: called CC=1 SCAR=0.0
Pod documented
164 14 my $self=shift;
165 14 return keys %{$self->{"conf"}};
166 }
167  
168 4 1;
169 __END__
↑ Summary Backend/String.pm »