blib/lib/Ubic/Settings/ConfigFile.pm untested

filter: Statementstmt 0.0% 0 / 35 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 10 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 56 SCAR 24.8
CC
3
Cov
0%
read
6.0
write
6.0
BEGIN
2.0
CRAP 12.0
1 package Ubic::Settings::ConfigFile;
2 $Ubic::Settings::ConfigFile::VERSION = '1.59';
3 use strict;
4 use warnings;
5  
6 # ABSTRACT: single ubic config file
7  
8  
9 use Params::Validate qw(:all);
10  
11 sub read {
12 my ($class, $file) = validate_pos(@_, 1, { type => SCALAR });
13 unless (-e $file) {
14 die "Config file '$file' not found";
15 }
16  
17 open my $fh, '<', $file or die "Can't open '$file': $!";
18  
19 my $config = {};
20 while (my $line = <$fh>) {
21 chomp $line;
22 my ($key, $value) = $line =~ /^(\w+)\s*=\s*(.*)$/;
23 $config->{$key} = $value;
24 }
25  
26 close $fh or die "Can't close '$file': $!";
27  
28 return $config;
29 }
30  
31 sub write {
32 my ($class, $file, $config) = validate_pos(@_, 1, { type => SCALAR }, { type => HASHREF });
33  
34 my $content = "";
35  
36 for my $key (sort keys %$config) {
37 my $value = $config->{$key};
38 if ($value =~ /\n/) {
39 die "Invalid config line '$key = $value', values can't contain line breaks";
40 }
41 $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 open my $fh, '>', $file or die "Can't open '$file': $!";
47 print {$fh} $content or die "Can't write to '$file': $!; sorry, old config removed!";
48 close $fh or die "Can't close '$file': $!";
49  
50 return;
51 }
52  
53  
54 1;
55  
56 __END__