| 1 | package Config::Dot; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use Class::Utils qw(set_params); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use Config::Utils qw(hash); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use English qw(-no_match_vars); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use Error::Pure qw(err); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use Readonly; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | # Constants. | ||||||||||||||||||||||||||||
| 13 | 6 | Readonly::Scalar my $EMPTY_STR => q{}; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 6 | our $VERSION = 0.09; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # Constructor. | ||||||||||||||||||||||||||||
| 18 | sub new { | ||||||||||||||||||||||||||||
| 19 | 16 | ▶ | my ($class, @params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 16 | my $self = bless {}, $class; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # Callback. | ||||||||||||||||||||||||||||
| 23 | 16 | $self->{'callback'} = undef; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # Config hash. | ||||||||||||||||||||||||||||
| 26 | 16 | $self->{'config'} = {}; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # Set conflicts detection as error. | ||||||||||||||||||||||||||||
| 29 | 16 | $self->{'set_conflicts'} = 1; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # Process params. | ||||||||||||||||||||||||||||
| 32 | 16 | set_params($self, @params); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # Check config hash. | ||||||||||||||||||||||||||||
| 35 | 14 | ▶ | if (! $self->_check($self->{'config'})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 2 | err 'Bad \'config\' parameter.'; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # Check callback. | ||||||||||||||||||||||||||||
| 40 | 12 | ▶ | if (defined $self->{'callback'} && ref $self->{'callback'} ne 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self->{"callback"} and ref $self->{"callback"} ne "CODE"
MC/DC100%2/2
defined $self->{"callback"} and ref $self->{"callback"} ne "CODE"
defined $self->{"callback"}
ref $self->{"callback"} ne "CODE"
Truth table100%3/3
A and B
A= defined $self->{"callback"}B= ref $self->{"callback"} ne "CODE"
| |||||||||||||||||||||||||||||
| 41 | 1 | err 'Parameter \'callback\' isn\'t code reference.'; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # Count of lines. | ||||||||||||||||||||||||||||
| 45 | 11 | $self->{'count'} = 0; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # Stack. | ||||||||||||||||||||||||||||
| 48 | 11 | $self->{'stack'} = []; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # Object. | ||||||||||||||||||||||||||||
| 51 | 11 | return $self; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # Parse text or array of texts. | ||||||||||||||||||||||||||||
| 55 | sub parse { | ||||||||||||||||||||||||||||
| 56 | 8 | ▶ | my ($self, $string_or_array_ref) = @_; | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | 8 | my @text; | |||||||||||||||||||||||||||
| 58 | 8 | ▶ | if (ref $string_or_array_ref eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 2 | @text = @{$string_or_array_ref}; | |||||||||||||||||||||||||||
| 60 | } else { | ||||||||||||||||||||||||||||
| 61 | 6 | @text = split m/$INPUT_RECORD_SEPARATOR/sm, | |||||||||||||||||||||||||||
| 62 | $string_or_array_ref; | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 8 | foreach my $line (@text) { | |||||||||||||||||||||||||||
| 65 | 15 | $self->{'count'}++; | |||||||||||||||||||||||||||
| 66 | 15 | $self->_parse($line); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 6 | return $self->{'config'}; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # Reset content. | ||||||||||||||||||||||||||||
| 72 | sub reset { | ||||||||||||||||||||||||||||
| 73 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 4 | $self->{'config'} = {}; | |||||||||||||||||||||||||||
| 75 | 4 | $self->{'count'} = 0; | |||||||||||||||||||||||||||
| 76 | 4 | return; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # Serialize. | ||||||||||||||||||||||||||||
| 80 | sub serialize { | ||||||||||||||||||||||||||||
| 81 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
serialize: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | return join $INPUT_RECORD_SEPARATOR, | ||||||||||||||||||||||||||||
| 83 | 5 | $self->_serialize($self->{'config'}); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # Check structure. | ||||||||||||||||||||||||||||
| 87 | sub _check { | ||||||||||||||||||||||||||||
| 88 | 17 | ▶ | my ($self, $config_hr) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_check: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 89 | 17 | ▶ | if (ref $config_hr eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 15 | foreach my $key (sort keys %{$config_hr}) { | |||||||||||||||||||||||||||
| 91 | 9 | ▶ | if (ref $config_hr->{$key} ne '' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $config_hr->{$key} ne "" and not $self->_check($config_hr->{$key})
MC/DC100%2/2
ref $config_hr->{$key} ne "" and not $self->_check($config_hr->{$key})
ref $config_hr->{$key} ne ""
not $self->_check($config_hr->{$key})
Truth table100%3/3
A and not(B)
A= ref $config_hr->{$key} ne ""B= not $self->_check($config_hr->{$key})
| |||||||||||||||||||||||||||||
| 92 | && ! $self->_check($config_hr->{$key})) { | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 1 | return 0; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | 14 | return 1; | |||||||||||||||||||||||||||
| 98 | } else { | ||||||||||||||||||||||||||||
| 99 | 2 | return 0; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # Parse string. | ||||||||||||||||||||||||||||
| 104 | sub _parse { | ||||||||||||||||||||||||||||
| 105 | 15 | ▶ | my ($self, $string) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_parse: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # Remove comments on single line. | ||||||||||||||||||||||||||||
| 108 | 15 | $string =~ s/^\s*#.*$//sm; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # Blank space. | ||||||||||||||||||||||||||||
| 111 | 15 | ▶ | if ($string =~ m/^\s*$/sm) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 2 | return 0; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # Split. | ||||||||||||||||||||||||||||
| 116 | 13 | my ($key, $val) = split m/=/sm, $string, 2; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | # Not a key. | ||||||||||||||||||||||||||||
| 119 | 13 | ▶ | if (length $key < 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 1 | return 0; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # Bad key. | ||||||||||||||||||||||||||||
| 124 | 12 | ▶ | if ($key !~ m/^[-\w\.:,]+\+?$/sm) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 1 | err "Bad key '$key' in string '$string' at line ". | |||||||||||||||||||||||||||
| 126 | "'$self->{'count'}'."; | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 11 | my @tmp = split m/\./sm, $key; | |||||||||||||||||||||||||||
| 130 | 11 | hash($self, \@tmp, $val); | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # Ok. | ||||||||||||||||||||||||||||
| 133 | 10 | return 1; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # Serialize. | ||||||||||||||||||||||||||||
| 137 | sub _serialize { | ||||||||||||||||||||||||||||
| 138 | 6 | ▶ | my ($self, $config_hr) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_serialize: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 139 | 6 | my @ret; | |||||||||||||||||||||||||||
| 140 | 6 | foreach my $key (sort keys %{$config_hr}) { | |||||||||||||||||||||||||||
| 141 | 5 | ▶ | if (ref $config_hr->{$key} eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 1 | my @subkey = $self->_serialize($config_hr->{$key}); | |||||||||||||||||||||||||||
| 143 | 1 | foreach my $subkey (@subkey) { | |||||||||||||||||||||||||||
| 144 | 1 | push @ret, $key.'.'.$subkey; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | } else { | ||||||||||||||||||||||||||||
| 147 | 4 | ▶ | if ($config_hr->{$key} =~ m/\n/ms) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 1 | err 'Unsupported stay with newline in value.'; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | 3 | push @ret, $key.'='.$config_hr->{$key}; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | 5 | return @ret; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 6 | 1; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | __END__ | ||||||||||||||||||||||||||||