lib/Ubic/Settings/ConfigFile.pm

filter: Statementstmt 64.7% 22 / 34 Branchbran 25.0% 4 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 4 / 5 Podpod 100% 2 / 2 totaltotal 56.1% 32 / 57 SCAR 30.5
CC
10
Cov
52%
read
30.0
write
6.6
BEGIN
1.0
CRAP 21.1
« lib/Ubic/Daemon/OS/POSIX.pm ↑ Summary blib/lib/Ubic/Ping/Service.pm »
1 package Ubic::Settings::ConfigFile;
2 162 $Ubic::Settings::ConfigFile::VERSION = '1.59';
3 162 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 162 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: single ubic config file
7  
8  
9 162 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 sub read {
12 0 ▶ my ($class, $file) = validate_pos(@_, 1, { type => SCALAR });
Subroutine read: not called CC=5 SCAR=34.0
Pod documented
13 0 ▶ unless (-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 0 0 0
14 0 die "Config file '$file' not found";
15 }
16  
17 0 ▶ open my $fh, '<', $file or die "Can't open '$file': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "Can't open '${file}': $!" 0 0 0
18  
19 0 my $config = {};
20 0 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
21 0 chomp $line;
22 0 my ($key, $value) = $line =~ /^(\w+)\s*=\s*(.*)$/;
23 0 $config->{$key} = $value;
24 }
25  
26 0 ▶ close $fh or die "Can't close '$file': $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Can't close '${file}': $!" 0 0 0
27  
28 0 return $config;
29 }
30  
31 sub write {
32 1 ▶ my ($class, $file, $config) = validate_pos(@_, 1, { type => SCALAR }, { type => HASHREF });
Subroutine write: called CC=6 SCAR=18.8
Pod documented
33  
34 1 my $content = "";
35  
36 1 for my $key (sort keys %$config) {
37 3 my $value = $config->{$key};
38 3 ▶ if ($value =~ /\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\n/) 0 3 3
39 0 die "Invalid config line '$key = $value', values can't contain line breaks";
40 }
41 3 $content .= "$key = $value\n";
42 }
43  
44 # we open file after content is prepared, so that file is not removed if something fails
45 # TODO - should we write to tmp file first?
46 1 ▶ open my $fh, '>', $file or die "Can't open '$file': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or die "Can't open '${file}': $!" 0 1 1
47 1 ▶ print {$fh} $content or die "Can't write to '$file': $!; sorry, old config removed!";
Branch: 1 branch
BranchTrueFalseTotal
print {$fh;} $content or die "Can't write to '${file}': $!; sorry, old config removed!" 0 1 1
48 1 ▶ close $fh or die "Can't close '$file': $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Can't close '${file}': $!" 0 1 1
49  
50 1 return;
51 }
52  
53  
54 162 1;
55  
56 __END__
« lib/Ubic/Daemon/OS/POSIX.pm ↑ Summary blib/lib/Ubic/Ping/Service.pm »