blib/lib/DB/Color/Config.pm

filter: Statementstmt 21.0% 12 / 57 Branchbran 7.8% 3 / 38 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 44.4% 4 / 9 Podpod 100% 6 / 6 totaltotal 22.1% 25 / 113 SCAR 59.2
CC
24
Cov
15%
read_string
72.0
write_string
72.0
read
25.8
CRAP 372.0
↑ Summary blib/lib/DB/Color.pm »
1 package DB::Color::Config;
2  
3 # If you thought Config::Simple was small...
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 BEGIN {
7 1 ▶ require 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 $DB::Color::Config::VERSION = '0.10';
9 1 $DB::Color::Config::errstr = '';
10 }
11  
12 # Create an empty object
13 0 ▶sub new { bless {}, shift }
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
14  
15 # Create an object from a file
16 sub read {
17 1 ▶ my $class = ref $_[0] ? ref shift : shift;
Subroutine read: called CC=7 SCAR=32.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 1 1
18  
19 # Check the file
20 1 ▶ my $file = shift or return $class->_error( 'You did not specify a file name' );
Branch: 1 branch
BranchTrueFalseTotal
my $file = shift() or return $class->_error("You did not specify a file name") 0 1 1
21 1 ▶ return $class->_error( "File '$file' does not exist" ) unless -e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 1 0 1
22 0 ▶ return $class->_error( "'$file' is a directory, not a file" ) unless -f _;
Branch: 1 branch
BranchTrueFalseTotal
unless -f _ 0 0 0
23 0 ▶ return $class->_error( "Insufficient permissions to read '$file'" ) unless -r _;
Branch: 1 branch
BranchTrueFalseTotal
unless -r _ 0 0 0
24  
25 # Slurp in the file
26 0 local $/ = undef;
27 0 ▶ open( CFG, $file ) or return $class->_error( "Failed to open file '$file': $!" );
Branch: 1 branch
BranchTrueFalseTotal
open CFG, $file or return $class->_error("Failed to open file '${file}': $!") 0 0 0
28 0 my $contents = <CFG>;
29 0 close( CFG );
30  
31 0 $class->read_string( $contents );
32 }
33  
34 # Create an object from a string
35 sub read_string {
36 0 ▶ my $class = ref $_[0] ? ref shift : shift;
Subroutine read_string: not called CC=8 SCAR=42.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
37 0 my $self = bless {}, $class;
38 0 ▶ return undef unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 0 0
39  
40 # Parse the file
41 0 my $ns = '_';
42 0 my $counter = 0;
43 0 foreach ( split /(?:\015{1,2}\012|\015|\012)/, shift ) {
44 0 $counter++;
45  
46 # Skip comments and empty lines
47 0 ▶ next if /^\s*(?:\#|\;|$)/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*(?:\#|\;|$)/ 0 0 0
48  
49 # Remove inline comments
50 0 s/\s\;\s.+$//g;
51  
52 # Handle section headers
53 0 ▶ if ( /^\s*\[\s*(.+?)\s*\]\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*\[\s*(.+?)\s*\]\s*$/) 0 0 0
54 # Create the sub-hash if it doesn't exist.
55 # Without this sections without keys will not
56 # appear at all in the completed struct.
57 0 ▶ $self->{$ns = $1} ||= {};
Condition0%0/2
$$self{$ns = $1} ||= {}
l!l
00
MC/DC0%0/1
$$self{$ns = $1} ||= {}
$$self{$ns = $1}
Truth table0%0/2
A
A= $$self{$ns = $1}
Aresult
11
00
58 0 next;
59 }
60  
61 # Handle properties
62 0 ▶ if ( /^\s*([^=]+?)\s*=\s*(.*?)\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*([^=]+?)\s*=\s*(.*?)\s*$/) 0 0 0
63 0 $self->{$ns}->{$1} = $2;
64 0 next;
65 }
66  
67 0 return $self->_error( "Syntax error at line $counter: '$_'" );
68 }
69  
70 0 $self;
71 }
72  
73 # Save an object to a file
74 sub write {
75 0 ▶ my $self = shift;
Subroutine write: not called CC=4 SCAR=30.0
Pod documented
76 0 ▶ my $file = shift or return $self->_error(
Branch: 1 branch
BranchTrueFalseTotal
my $file = shift() or return $self->_error("No file name provided") 0 0 0
77 'No file name provided'
78 );
79  
80 # Write it to the file
81 0 my $string = $self->write_string;
82 0 ▶ return undef unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 0 0
83 0 ▶ open( CFG, '>' . $file ) or return $self->_error(
Branch: 1 branch
BranchTrueFalseTotal
open CFG, '>' . $file or return $self->_error("Failed to open file '${file}' for writing: $!") 0 0 0
84 "Failed to open file '$file' for writing: $!"
85 );
86 0 print CFG $string;
87 0 close CFG;
88 }
89  
90 # Save an object to a string
91 sub write_string {
92 0 ▶ my $self = shift;
Subroutine write_string: not called CC=8 SCAR=42.8
Pod documented
93  
94 0 my $contents = '';
95 0 ▶ foreach my $section ( sort { (($b eq '_') <=> ($a eq '_')) || ($a cmp $b) } keys %$self ) {
Branch: 1 branch
BranchTrueFalseTotal
($b eq "_") <=> ($a eq "_") or $a cmp $b 0 0 0
96 # Check for several known-bad situations with the section
97 # 1. Leading whitespace
98 # 2. Trailing whitespace
99 # 3. Newlines in section name
100 0 ▶ return $self->_error(
Branch: 1 branch
BranchTrueFalseTotal
if $section =~ /(?:^\s|\n|\s$)/s 0 0 0
101 "Illegal whitespace in section name '$section'"
102 ) if $section =~ /(?:^\s|\n|\s$)/s;
103 0 my $block = $self->{$section};
104 0 ▶ $contents .= "\n" if length $contents;
Branch: 1 branch
BranchTrueFalseTotal
if length $contents 0 0 0
105 0 ▶ $contents .= "[$section]\n" unless $section eq '_';
Branch: 1 branch
BranchTrueFalseTotal
unless $section eq "_" 0 0 0
106 0 foreach my $property ( sort keys %$block ) {
107 return $self->_error(
108 "Illegal newlines in property '$section.$property'"
109 0 ▶ ) if $block->{$property} =~ /(?:\012|\015)/s;
Branch: 1 branch
BranchTrueFalseTotal
if $block->{$property} =~ /(?:\012|\015)/s 0 0 0
110 0 $contents .= "$property=$block->{$property}\n";
111 }
112 }
113
114 0 $contents;
115 }
116  
117 # Error handling
118 0 ▶sub errstr { $DB::Color::Config::errstr }
Subroutine errstr: not called CC=1 SCAR=6.9
Pod documented
119 1 ▶sub _error { $DB::Color::Config::errstr = $_[1]; undef }
Subroutine _error: called CC=1 SCAR=0.0
120  
121 1 1;
122  
123 __END__
↑ Summary blib/lib/DB/Color.pm »