| 1 | package Config::JSON; | ||||||||||||||||||||||||||||
| 2 | 5 | $Config::JSON::VERSION = '1.5202'; | |||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use JSON 2.0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use List::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use constant FILE_HEADER => "# config-file-type: JSON 1\n"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 13 | has config => ( | ||||||||||||||||||||||||||||
| 14 | is => 'rw', | ||||||||||||||||||||||||||||
| 15 | 18 | ▶ | default => sub {{}}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 5 | ); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 19 | 2 | ▶ | sub getFilePath { | ||||||||||||||||||||||||||
|
Subroutine
getFilePath: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 20 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 21 | 2 | return $self->pathToFile; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 25 | has pathToFile => ( | ||||||||||||||||||||||||||||
| 26 | is => 'ro', | ||||||||||||||||||||||||||||
| 27 | required => 1, | ||||||||||||||||||||||||||||
| 28 | 18 | ▶ | trigger => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 29 | 18 | my ($self, $pathToFile, $old) = @_; | |||||||||||||||||||||||||||
| 30 | 18 | ▶ | if (open(my $FILE, "<", $pathToFile)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | # slurp | ||||||||||||||||||||||||||||
| 32 | 18 | local $/ = undef; | |||||||||||||||||||||||||||
| 33 | 18 | my $json = <$FILE>; | |||||||||||||||||||||||||||
| 34 | 18 | close($FILE); | |||||||||||||||||||||||||||
| 35 | 18 | my $conf = eval { JSON->new->relaxed->utf8->decode($json); }; | |||||||||||||||||||||||||||
| 36 | 18 | ▶ | confess "Couldn't parse JSON in config file '$pathToFile'\n" unless ref $conf; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 18 | $self->config($conf); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # process includes | ||||||||||||||||||||||||||||
| 40 | 5 | ▶ | my @includes = map { glob $_ } @{ $self->get('includes') || [] }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Condition100%2/2
$self->get('includes') || []
MC/DC100%1/1
$self->get('includes') || []
$self->get('includes')
Truth table100%2/2
A
A= $self->get('includes')
| |||||||||||||||||||||||||||||
| 41 | 18 | my @loadedIncludes; | |||||||||||||||||||||||||||
| 42 | 18 | foreach my $include (@includes) { | |||||||||||||||||||||||||||
| 43 | 6 | push @loadedIncludes, __PACKAGE__->new(pathToFile=>$include, isInclude=>1); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | 18 | $self->includes(\@loadedIncludes); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | else { | ||||||||||||||||||||||||||||
| 48 | 0 | confess "Cannot read config file: ".$pathToFile; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | }, | ||||||||||||||||||||||||||||
| 51 | 5 | ); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 54 | has isInclude => ( | ||||||||||||||||||||||||||||
| 55 | is => 'ro', | ||||||||||||||||||||||||||||
| 56 | 12 | ▶ | default => sub {0}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | 5 | ); | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 60 | has includes => ( | ||||||||||||||||||||||||||||
| 61 | is => 'rw', | ||||||||||||||||||||||||||||
| 62 | 18 | ▶ | default => sub {[]}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 63 | 5 | ); | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 66 | 0 | ▶ | sub getIncludes { | ||||||||||||||||||||||||||
|
Subroutine
getIncludes: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 67 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 68 | 0 | return $self->includes; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 72 | 18 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 73 | 18 | my $orig = shift; | |||||||||||||||||||||||||||
| 74 | 18 | my $class = shift; | |||||||||||||||||||||||||||
| 75 | 18 | ▶ | if ( @_ == 1 && ! ref $_[0] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ == 1 and not ref $_[0]
MC/DC50%1/2
@_ == 1 and not ref $_[0]
@_ == 1
not ref $_[0]
Truth table66%2/3
A and not(B)
A= @_ == 1B= not ref $_[0]
| |||||||||||||||||||||||||||||
| 76 | 5 | return $class->$orig(pathToFile => $_[0]); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | else { | ||||||||||||||||||||||||||||
| 79 | 13 | return $class->$orig(@_); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | 5 | }; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 84 | 2 | ▶ | sub addToArray { | ||||||||||||||||||||||||||
|
Subroutine
addToArray: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 2 | my ($self, $property, $value) = @_; | |||||||||||||||||||||||||||
| 86 | 2 | my $array = $self->get($property); | |||||||||||||||||||||||||||
| 87 | 2 | ▶ | unless (defined List::Util::first { $value eq $_ } @{$array}) { # check if it already exists | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | # add it | ||||||||||||||||||||||||||||
| 89 | 2 | push(@{$array}, $value); | |||||||||||||||||||||||||||
| 90 | 2 | $self->set($property, $array); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 95 | 3 | ▶ | sub addToArrayAfter { | ||||||||||||||||||||||||||
|
Subroutine
addToArrayAfter: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 96 | 3 | my ($self, $property, $afterValue, $value) = @_; | |||||||||||||||||||||||||||
| 97 | 3 | my $array = $self->get($property); | |||||||||||||||||||||||||||
| 98 | 3 | ▶ | unless (defined List::Util::first { $value eq $_ } @{ $array }) { # check if it already exists | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 2 | my $idx = 0; | |||||||||||||||||||||||||||
| 100 | 2 | ▶ | for (; $idx < $#{ $array }; $idx++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 5 | ▶ | if ($array->[$idx] eq $afterValue) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 1 | last; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 2 | splice @{ $array }, $idx + 1, 0, $value; | |||||||||||||||||||||||||||
| 106 | 2 | $self->set($property, $array); | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 111 | 3 | ▶ | sub addToArrayBefore { | ||||||||||||||||||||||||||
|
Subroutine
addToArrayBefore: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 112 | 3 | my ($self, $property, $beforeValue, $value) = @_; | |||||||||||||||||||||||||||
| 113 | 3 | my $array = $self->get($property); | |||||||||||||||||||||||||||
| 114 | 3 | ▶ | unless (defined List::Util::first { $value eq $_ } @{ $array }) { # check if it already exists | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 2 | my $idx = $#{ $array }; | |||||||||||||||||||||||||||
| 116 | 2 | ▶ | for (; $idx > 0; $idx--) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 5 | ▶ | if ($array->[$idx] eq $beforeValue) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 1 | last; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | 2 | splice @{ $array }, $idx , 0, $value; | |||||||||||||||||||||||||||
| 122 | 2 | $self->set($property, $array); | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 127 | 3 | ▶ | sub addToHash { | ||||||||||||||||||||||||||
|
Subroutine
addToHash: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 128 | 3 | my ($self, $property, $key, $value) = @_; | |||||||||||||||||||||||||||
| 129 | 3 | $self->set($property."/".$key, $value); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 133 | 7 | ▶ | sub create { | ||||||||||||||||||||||||||
|
Subroutine
create: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 134 | 7 | my ($class, $filename) = @_; | |||||||||||||||||||||||||||
| 135 | 7 | ▶ | if (open(my $FILE,">",$filename)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 7 | print $FILE FILE_HEADER."\n{ }\n"; | |||||||||||||||||||||||||||
| 137 | 7 | close($FILE); | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | else { | ||||||||||||||||||||||||||||
| 140 | 0 | warn "Can't write to config file ".$filename; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | 7 | return $class->new(pathToFile=>$filename); | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 146 | 29 | ▶ | sub delete { | ||||||||||||||||||||||||||
|
Subroutine
delete: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 147 | 29 | my ($self, $param) = @_; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | # inform the includes | ||||||||||||||||||||||||||||
| 150 | 29 | foreach my $include (@{$self->includes}) { | |||||||||||||||||||||||||||
| 151 | 16 | $include->delete($param); | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | # find the directive | ||||||||||||||||||||||||||||
| 155 | 29 | my $directive = $self->config; | |||||||||||||||||||||||||||
| 156 | 29 | my @parts = $self->splitKeyParts($param); | |||||||||||||||||||||||||||
| 157 | 29 | my $lastPart = pop @parts; | |||||||||||||||||||||||||||
| 158 | 29 | foreach my $part (@parts) { | |||||||||||||||||||||||||||
| 159 | 22 | $directive = $directive->{$part}; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | # only delete it if it exists | ||||||||||||||||||||||||||||
| 163 | 29 | ▶ | if (exists $directive->{$lastPart}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 18 | delete $directive->{$lastPart}; | |||||||||||||||||||||||||||
| 165 | 18 | $self->write; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 170 | 2 | ▶ | sub deleteFromArray { | ||||||||||||||||||||||||||
|
Subroutine
deleteFromArray: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 171 | 2 | my ($self, $property, $value) = @_; | |||||||||||||||||||||||||||
| 172 | 2 | my $array = $self->get($property); | |||||||||||||||||||||||||||
| 173 | 2 | ▶ | for (my $i = 0; $i < scalar(@{$array}); $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 8 | ▶ | if ($array->[$i] eq $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 2 | splice(@{$array}, $i, 1); | |||||||||||||||||||||||||||
| 176 | 2 | last; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | 2 | $self->set($property, $array); | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 183 | 2 | ▶ | sub deleteFromHash { | ||||||||||||||||||||||||||
|
Subroutine
deleteFromHash: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 184 | 2 | my ($self, $property, $key) = @_; | |||||||||||||||||||||||||||
| 185 | 2 | $self->delete($property."/".$key); | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 189 | 122 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=8 SCAR=22.8
Pod
documented
| |||||||||||||||||||||||||||||
| 190 | 122 | my ($self, $property) = @_; | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | # they want a specific property | ||||||||||||||||||||||||||||
| 193 | 122 | ▶ | if (defined $property) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | # look in this config | ||||||||||||||||||||||||||||
| 196 | 122 | my $value = $self->config; | |||||||||||||||||||||||||||
| 197 | 122 | foreach my $part ($self->splitKeyParts($property)) { | |||||||||||||||||||||||||||
| 198 | 163 | $value = eval{$value->{$part}}; | |||||||||||||||||||||||||||
| 199 | 163 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | 1 | confess "Can't access $property. $@"; | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | 121 | ▶ | return $value if (defined $value); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | # look through includes | ||||||||||||||||||||||||||||
| 206 | 59 | foreach my $include (@{$self->includes}) { | |||||||||||||||||||||||||||
| 207 | 34 | my $value = $include->get($property); | |||||||||||||||||||||||||||
| 208 | 34 | ▶ | return $value if (defined $value); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | # didn't find it | ||||||||||||||||||||||||||||
| 212 | 47 | return undef; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | # they want the whole properties list | ||||||||||||||||||||||||||||
| 216 | 0 | my %whole = (); | |||||||||||||||||||||||||||
| 217 | 0 | foreach my $include (@{$self->includes}) { | |||||||||||||||||||||||||||
| 218 | 0 | %whole = (%whole, %{$include->get}); | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | 0 | %whole = (%whole, %{$self->config}); | |||||||||||||||||||||||||||
| 221 | 0 | return \%whole; | |||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 225 | 2 | ▶ | sub getFilename { | ||||||||||||||||||||||||||
|
Subroutine
getFilename: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 226 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 227 | 2 | my @path = split "/", $self->pathToFile; | |||||||||||||||||||||||||||
| 228 | 2 | return pop @path; | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 232 | 35 | ▶ | sub set { | ||||||||||||||||||||||||||
|
Subroutine
set: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 233 | 35 | my ($self, $property, $value) = @_; | |||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | # see if the directive exists in this config | ||||||||||||||||||||||||||||
| 236 | 35 | my $directive = $self->config; | |||||||||||||||||||||||||||
| 237 | 35 | my @parts = $self->splitKeyParts($property); | |||||||||||||||||||||||||||
| 238 | 35 | my $numParts = scalar @parts; | |||||||||||||||||||||||||||
| 239 | 35 | ▶ | for (my $i=0; $i < $numParts; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | 41 | my $part = $parts[$i]; | |||||||||||||||||||||||||||
| 241 | 41 | ▶ | if (exists $directive->{$part}) { # exists so we continue | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 242 | 23 | ▶ | if ($i == $numParts - 1) { # we're on the last part | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 243 | 17 | $directive->{$part} = $value; | |||||||||||||||||||||||||||
| 244 | 17 | $self->write; | |||||||||||||||||||||||||||
| 245 | 17 | return 1; | |||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | else { | ||||||||||||||||||||||||||||
| 248 | 6 | $directive = $directive->{$part}; | |||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||
| 251 | else { # doesn't exist so we quit | ||||||||||||||||||||||||||||
| 252 | 18 | last; | |||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | # see if any of the includes have this directive | ||||||||||||||||||||||||||||
| 257 | 18 | foreach my $include (@{$self->includes}) { | |||||||||||||||||||||||||||
| 258 | 9 | my $found = $include->set($property, $value); | |||||||||||||||||||||||||||
| 259 | 9 | ▶ | return 1 if ($found); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | # let's create the directive new in this config if it's not an include | ||||||||||||||||||||||||||||
| 263 | 15 | ▶ | unless ($self->isInclude) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | 9 | $directive = $self->config; | |||||||||||||||||||||||||||
| 265 | 9 | my $lastPart = pop @parts; | |||||||||||||||||||||||||||
| 266 | 9 | foreach my $part (@parts) { | |||||||||||||||||||||||||||
| 267 | 6 | ▶ | unless (exists $directive->{$part}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | 3 | $directive->{$part} = {}; | |||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | 6 | $directive = $directive->{$part}; | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | 9 | $directive->{$lastPart} = $value; | |||||||||||||||||||||||||||
| 273 | 9 | $self->write; | |||||||||||||||||||||||||||
| 274 | 9 | return 1; | |||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | # didn't find a place to write it | ||||||||||||||||||||||||||||
| 278 | 6 | return 0; | |||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 282 | 186 | ▶ | sub splitKeyParts { | ||||||||||||||||||||||||||
|
Subroutine
splitKeyParts: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 283 | 186 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 284 | 186 | my @parts = split /(?<!\\)\//, $key; | |||||||||||||||||||||||||||
| 285 | 186 | map {s{\\\/}{/}} @parts; | |||||||||||||||||||||||||||
| 286 | 186 | return @parts; | |||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 290 | 44 | ▶ | sub write { | ||||||||||||||||||||||||||
|
Subroutine
write: called CC=5 SCAR=16.5
Pod
documented
| |||||||||||||||||||||||||||||
| 291 | 44 | my $self = shift; | |||||||||||||||||||||||||||
| 292 | 44 | my $realfile = $self->pathToFile; | |||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | # convert data to json | ||||||||||||||||||||||||||||
| 295 | 44 | my $json = JSON->new->pretty->utf8->canonical->encode($self->config); | |||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | 44 | my $to_write = FILE_HEADER . "\n" . $json; | |||||||||||||||||||||||||||
| 298 | 44 | my $needed_bytes = length $to_write; | |||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | # open as read/write | ||||||||||||||||||||||||||||
| 301 | 44 | ▶ | open my $fh, '+<:raw', $realfile or confess "Unable to open $realfile for write: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 302 | 44 | my $current_bytes = (stat $fh)[7]; | |||||||||||||||||||||||||||
| 303 | # shrink file if needed | ||||||||||||||||||||||||||||
| 304 | 44 | ▶ | if ($needed_bytes < $current_bytes) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 305 | 22 | truncate $fh, $needed_bytes; | |||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||
| 307 | # make sure we can expand the file to the needed size before we overwrite it | ||||||||||||||||||||||||||||
| 308 | elsif ($needed_bytes > $current_bytes) { | ||||||||||||||||||||||||||||
| 309 | 19 | my $padding = q{ } x ($needed_bytes - $current_bytes); | |||||||||||||||||||||||||||
| 310 | 19 | sysseek $fh, 0, 2; | |||||||||||||||||||||||||||
| 311 | 19 | ▶ | if (! syswrite $fh, $padding) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 312 | 0 | sysseek $fh, 0, 0; | |||||||||||||||||||||||||||
| 313 | 0 | truncate $fh, $current_bytes; | |||||||||||||||||||||||||||
| 314 | 0 | close $fh; | |||||||||||||||||||||||||||
| 315 | 0 | confess "Unable to expand $realfile: $!"; | |||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||
| 317 | 19 | sysseek $fh, 0, 0; | |||||||||||||||||||||||||||
| 318 | 19 | seek $fh, 0, 0; | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | 44 | print {$fh} $to_write; | |||||||||||||||||||||||||||
| 321 | 44 | close $fh; | |||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | 44 | return 1; | |||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | =head1 NAME | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | Config::JSON - A JSON based config file system. | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | =head1 VERSION | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | version 1.5202 | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | use Config::JSON; | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | my $config = Config::JSON->create($pathToFile); | ||||||||||||||||||||||||||||
| 340 | my $config = Config::JSON->new($pathToFile); | ||||||||||||||||||||||||||||
| 341 | my $config = Config::JSON->new(pathToFile=>$pathToFile); | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | my $element = $config->get($directive); | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | $config->set($directive,$value); | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | $config->delete($directive); | ||||||||||||||||||||||||||||
| 348 | $config->deleteFromHash($directive, $key); | ||||||||||||||||||||||||||||
| 349 | $config->deleteFromArray($directive, $value); | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | $config->addToHash($directive, $key, $value); | ||||||||||||||||||||||||||||
| 352 | $config->addToArray($directive, $value); | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | my $path = $config->pathToFile; | ||||||||||||||||||||||||||||
| 355 | my $filename = $config->getFilename; | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | =head2 Example Config File | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | # config-file-type: JSON 1 | ||||||||||||||||||||||||||||
| 360 | { | ||||||||||||||||||||||||||||
| 361 | "dsn" : "DBI:mysql:test", | ||||||||||||||||||||||||||||
| 362 | "user" : "tester", | ||||||||||||||||||||||||||||
| 363 | "password" : "xxxxxx", | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | # some colors to choose from | ||||||||||||||||||||||||||||
| 366 | "colors" : [ "red", "green", "blue" ], | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | # some statistics | ||||||||||||||||||||||||||||
| 369 | "stats" : { | ||||||||||||||||||||||||||||
| 370 | "health" : 32, | ||||||||||||||||||||||||||||
| 371 | "vitality" : 11 | ||||||||||||||||||||||||||||
| 372 | }, | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | # including another file | ||||||||||||||||||||||||||||
| 375 | "includes" : ["macros.conf"] | ||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | This package parses the config files written in JSON. It also does some non-JSON stuff, like allowing for comments in the files. | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | If you want to see it in action, it is used as the config file system in WebGUI L<http://www.webgui.org/>. | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | =head2 Why? | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | Why build yet another config file system? Well there are a number | ||||||||||||||||||||||||||||
| 389 | of reasons: We used to use other config file parsers, but we kept | ||||||||||||||||||||||||||||
| 390 | running into limitations. We already use JSON in our app, so using | ||||||||||||||||||||||||||||
| 391 | JSON to store config files means using less memory because we already | ||||||||||||||||||||||||||||
| 392 | have the JSON parser in memory. In addition, with JSON we can have | ||||||||||||||||||||||||||||
| 393 | any number of hierarchcal data structures represented in the config | ||||||||||||||||||||||||||||
| 394 | file, whereas most config files will give you only one level of | ||||||||||||||||||||||||||||
| 395 | hierarchy, if any at all. JSON parses faster than XML and YAML. | ||||||||||||||||||||||||||||
| 396 | JSON is easier to read and edit than XML. Many other config file | ||||||||||||||||||||||||||||
| 397 | systems allow you to read a config file, but they don't provide any | ||||||||||||||||||||||||||||
| 398 | mechanism or utilities to write back to it. JSON is taint safe. | ||||||||||||||||||||||||||||
| 399 | JSON is easily parsed by languages other than Perl when we need to | ||||||||||||||||||||||||||||
| 400 | do that. | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | =head2 Multi-level Directives | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | You may of course access a directive called "foo", but since the config is basically a hash you can traverse | ||||||||||||||||||||||||||||
| 406 | multiple elements of the hash when specifying a directive name by simply delimiting each level with a slash, like | ||||||||||||||||||||||||||||
| 407 | "foo/bar". For example you may: | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | my $vitality = $config->get("stats/vitality"); | ||||||||||||||||||||||||||||
| 410 | $config->set("stats/vitality", 15); | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | You may do this wherever you specify a directive name. | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | =head2 Comments | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | You can put comments in the config file as long as # is the first non-space character on the line. However, if you use this API to write to the config file, your comments will be eliminated. | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | =head2 Includes | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | There is a special directive called "includes", which is an array of include files that may be brought in to | ||||||||||||||||||||||||||||
| 423 | the config. Even the files you include can have an "includes" directive, so you can do hierarchical includes. | ||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | Any directive in the main file will take precedence over the directives in the includes. Likewise the files | ||||||||||||||||||||||||||||
| 426 | listed first in the "includes" directive will have precedence over the files that come after it. When writing | ||||||||||||||||||||||||||||
| 427 | to the files, the same precedence is followed. | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | If you're setting a new directive that doesn't currently exist, it will only be written to the main file. | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | If a directive is deleted, it will be deleted from all files, including the includes. | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | =head1 INTERFACE | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | =head2 addToArray ( directive, value ) | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | Adds a value to an array directive in the config file. | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | =head3 directive | ||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | The name of the array. | ||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | =head3 value | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | The value to add. | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | =head2 addToArrayBefore ( directive, insertBefore, value ) | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | Inserts a value into an array immediately before another item. If | ||||||||||||||||||||||||||||
| 450 | that item can't be found, inserts at the beginning on the array. | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | =head3 directive | ||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | The name of the array. | ||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | =head3 insertBefore | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | The value to search for and base the positioning on. | ||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | =head3 value | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | The value to insert. | ||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | =head2 addToArrayAfter ( directive, insertAfter, value ) | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | Inserts a value into an array immediately after another item. If | ||||||||||||||||||||||||||||
| 468 | that item can't be found, inserts at the end on the array. | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | =head3 directive | ||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||
| 472 | The name of the array. | ||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | =head3 insertAfter | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | The value to search for and base the positioning on. | ||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | =head3 value | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | The value to insert. | ||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||
| 484 | =head2 addToHash ( directive, key, value ) | ||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | Adds a value to a hash directive in the config file. B<NOTE:> This is really the same as | ||||||||||||||||||||||||||||
| 487 | $config->set("directive/key", $value); | ||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | =head3 directive | ||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||
| 491 | The name of the hash. | ||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||
| 493 | =head3 key | ||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | The key to add. | ||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||
| 497 | =head3 value | ||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||
| 499 | The value to add. | ||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||
| 502 | =head2 create ( pathToFile ) | ||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||
| 504 | Constructor. Creates a new empty config file. | ||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | =head3 pathToFile | ||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||
| 508 | The path and filename of the file to create. | ||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | =head2 delete ( directive ) | ||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||
| 514 | Deletes a key from the config file. | ||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||
| 516 | =head3 directive | ||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||
| 518 | The name of the directive to delete. | ||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||
| 521 | =head2 deleteFromArray ( directive, value ) | ||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||
| 523 | Deletes a value from an array directive in the config file. | ||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||
| 525 | =head3 directive | ||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||
| 527 | The name of the array. | ||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||
| 529 | =head3 value | ||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||
| 531 | The value to delete. | ||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||
| 535 | =head2 deleteFromHash ( directive, key ) | ||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||
| 537 | Delete a key from a hash directive in the config file. B<NOTE:> This is really just the same as doing | ||||||||||||||||||||||||||||
| 538 | $config->delete("directive/key"); | ||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||
| 540 | =head3 directive | ||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | The name of the hash. | ||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||
| 544 | =head3 key | ||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||
| 546 | The key to delete. | ||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||
| 550 | =head2 get ( directive ) | ||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||
| 552 | Returns the value of a particular directive from the config file. | ||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||
| 554 | =head3 directive | ||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||
| 556 | The name of the directive to return. | ||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||
| 560 | =head2 getFilename ( ) | ||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||
| 562 | Returns the filename for this config. | ||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||
| 566 | =head2 pathToFile ( ) | ||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||
| 568 | Returns the filename and path for this config. May also be called as C<getFilePath> for backward campatibility sake. | ||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||
| 572 | =head2 includes ( ) | ||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||
| 574 | Returns an array reference of Config::JSON objects that are files included by this config. May also be called as C<getIncludes> for backward compatibility sake. | ||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||
| 577 | =head2 new ( pathToFile ) | ||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||
| 579 | Constructor. Builds an object around a config file. | ||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||
| 581 | =head3 pathToFile | ||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||
| 583 | A string representing a path such as "/etc/my-cool-config.conf". | ||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||
| 587 | =head2 set ( directive, value ) | ||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||
| 589 | Creates a new or updates an existing directive in the config file. | ||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||
| 591 | =head3 directive | ||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||
| 593 | A directive name. | ||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||
| 595 | =head3 value | ||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||
| 597 | The value to set the paraemter to. Can be a scalar, hash reference, or array reference. | ||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||
| 601 | =head2 splitKeyParts ( key ) | ||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||
| 603 | Returns an array of key parts. | ||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||
| 605 | =head3 key | ||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||
| 607 | A key string. Could be 'foo' (simple key), 'foo/bar' (a multilevel key referring to the bar key as a child of foo), or 'foo\/bar' (a simple key that contains a slash in the key). Don't forget to double escape in your perl code if you have a slash in your key parts like this: | ||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||
| 609 | $config->get('foo\\/bar'); | ||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||
| 611 | =cut | ||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||
| 615 | =head2 write ( ) | ||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||
| 617 | Writes the file to the filesystem. Normally you'd never need to call this as it's called automatically by the other methods when a change occurs. | ||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||
| 620 | =head1 DIAGNOSTICS | ||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||
| 622 | =over | ||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||
| 624 | =item C<< Couldn't parse JSON in config file >> | ||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||
| 626 | This means that the config file does not appear to be formatted properly as a JSON file. Common mistakes are missing commas or trailing commas on the end of a list. | ||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||
| 628 | =item C<< Cannot read config file >> | ||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||
| 630 | We couldn't read the config file. This usually means that the path specified in the constructor is incorrect. | ||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||
| 632 | =item C<< Can't write to config file >> | ||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||
| 634 | We couldn't write to the config file. This usually means that the file system is full, or the that the file is write protected. | ||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||
| 636 | =back | ||||||||||||||||||||||||||||
| 637 | |||||||||||||||||||||||||||||
| 638 | =head1 PREREQS | ||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||
| 640 | L<JSON> L<Moo> L<List::Util> L<Test::More> L<Test::Deep> | ||||||||||||||||||||||||||||
| 641 | |||||||||||||||||||||||||||||
| 642 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||
| 644 | =over | ||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||
| 646 | =item Repository | ||||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||
| 648 | L<http://github.com/plainblack/Config-JSON> | ||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||
| 650 | =item Bug Reports | ||||||||||||||||||||||||||||
| 651 | |||||||||||||||||||||||||||||
| 652 | L<http://rt.cpan.org/Public/Dist/Display.html?Name=Config-JSON> | ||||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||
| 654 | =back | ||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||
| 656 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 657 | |||||||||||||||||||||||||||||
| 658 | JT Smith <jt-at-plainblack-dot-com> | ||||||||||||||||||||||||||||
| 659 | |||||||||||||||||||||||||||||
| 660 | =head1 LEGAL | ||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||
| 662 | Config::JSON is Copyright 2009 Plain Black Corporation (L<http://www.plainblack.com/>) and is licensed under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||
| 664 | =cut | ||||||||||||||||||||||||||||
| 665 | |||||||||||||||||||||||||||||
| 666 | 5 | 1; | |||||||||||||||||||||||||||