| 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
| |||||||||||
| 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
| |||||||||||
| 18 | |||||||||||
| 19 | 0 | my $config = {}; | |||||||||
| 20 | 0 | ▶ | while (my $line = <$fh>) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 47 | 1 | ▶ | print {$fh} $content or die "Can't write to '$file': $!; sorry, old config removed!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 1 | ▶ | close $fh or die "Can't close '$file': $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | |||||||||||
| 50 | 1 | return; | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | |||||||||||
| 54 | 162 | 1; | |||||||||
| 55 | |||||||||||
| 56 | __END__ | ||||||||||