line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# $Id: JSON.pm 12 2014-06-04 22:14:15Z adam $ |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package Config::Trivial::JSON; |
4
|
|
|
|
|
|
|
|
5
|
10
|
|
|
10
|
|
173305
|
use base qw( Config::Trivial ); |
|
10
|
|
|
|
|
25
|
|
|
10
|
|
|
|
|
10760
|
|
6
|
|
|
|
|
|
|
|
7
|
10
|
|
|
10
|
|
74345
|
use 5.010; |
|
10
|
|
|
|
|
44
|
|
|
10
|
|
|
|
|
422
|
|
8
|
10
|
|
|
10
|
|
115
|
use strict; |
|
10
|
|
|
|
|
28
|
|
|
10
|
|
|
|
|
381
|
|
9
|
10
|
|
|
10
|
|
62
|
use Carp; |
|
10
|
|
|
|
|
20
|
|
|
10
|
|
|
|
|
9773
|
|
10
|
10
|
|
|
10
|
|
69
|
use warnings; |
|
10
|
|
|
|
|
23
|
|
|
10
|
|
|
|
|
295
|
|
11
|
10
|
|
|
10
|
|
10260
|
use JSON::MaybeXS; |
|
10
|
|
|
|
|
142275
|
|
|
10
|
|
|
|
|
6068
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '0.02_01'; |
14
|
|
|
|
|
|
|
my ( $_package, $_file ) = caller; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# |
17
|
|
|
|
|
|
|
# STORE |
18
|
|
|
|
|
|
|
# |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub store { |
21
|
9
|
|
|
9
|
1
|
19150
|
my $self = shift; |
22
|
9
|
|
|
|
|
29
|
my %args = @_; |
23
|
|
|
|
|
|
|
|
24
|
9
|
|
66
|
|
|
81
|
my $file = $args{'config_file'} |
25
|
|
|
|
|
|
|
|| $self->{_json_file} |
26
|
|
|
|
|
|
|
|| $self->{_config_file}; |
27
|
|
|
|
|
|
|
|
28
|
9
|
100
|
66
|
|
|
168
|
if ( ( ( $self->{_self} ) |
|
|
|
66
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
66
|
|
|
|
|
29
|
|
|
|
|
|
|
&& ( ( $file =~ '\(eval ' ) || ( $file =~ 'base.pm' ) ) ) |
30
|
|
|
|
|
|
|
|| ( $_file eq $file ) |
31
|
|
|
|
|
|
|
|| ( $0 eq $file ) ) |
32
|
|
|
|
|
|
|
{ |
33
|
4
|
|
|
|
|
17
|
return $self->_raise_error( |
34
|
|
|
|
|
|
|
'Not allowed to store to the calling file.'); |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
5
|
100
|
|
|
|
65
|
if ( -e $file ) { |
38
|
1
|
50
|
|
|
|
13
|
croak "ERROR: Insufficient permissions to write to: $file" |
39
|
|
|
|
|
|
|
unless ( -w $file ); |
40
|
1
|
50
|
|
|
|
72
|
rename $file, $file . $self->{_backup_char} |
41
|
|
|
|
|
|
|
or croak "ERROR: Unable to rename $file."; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
5
|
|
100
|
|
|
589
|
my $settings = $args{'configuration'} || $self->{_configuration}; |
45
|
|
|
|
|
|
|
|
46
|
5
|
100
|
100
|
|
|
28
|
if ( ! ( $settings && ref $settings eq 'HASH') ) { |
47
|
2
|
|
|
|
|
9
|
return $self->_raise_error( |
48
|
|
|
|
|
|
|
q{Configuration object isn't a HASH reference.}) |
49
|
|
|
|
|
|
|
}; |
50
|
|
|
|
|
|
|
|
51
|
3
|
|
|
|
|
33
|
my $json_text = encode_json( $settings ); |
52
|
3
|
50
|
|
|
|
9
|
if ( $json_text ) { |
53
|
3
|
50
|
|
|
|
244
|
open my $json_file, '>', $file or croak "Unable to store to $file"; |
54
|
3
|
|
|
|
|
25
|
print $json_file $json_text; |
55
|
3
|
|
|
|
|
128
|
close $json_file; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
else { |
58
|
0
|
|
|
|
|
0
|
croak 'Unable to encode Per structure into JSON'; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
3
|
|
|
|
|
17
|
return 1; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# |
65
|
|
|
|
|
|
|
# RETRIEVE |
66
|
|
|
|
|
|
|
# |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub retrieve { |
69
|
15
|
|
|
15
|
1
|
13745
|
my $self = shift; |
70
|
15
|
|
|
|
|
27
|
my $key = shift; # If there is a key, return only it's value |
71
|
15
|
|
|
|
|
37
|
my $retrieved_hash_ref; |
72
|
|
|
|
|
|
|
my $file; |
73
|
|
|
|
|
|
|
|
74
|
15
|
100
|
100
|
|
|
84
|
if ( $self->{_config_file} && $self->{_json_file} ) { |
75
|
4
|
100
|
|
|
|
98
|
if ( $self->{_config_file} eq $self->{_json_file} ) { |
|
|
100
|
|
|
|
|
|
76
|
2
|
|
|
|
|
5
|
$file = $self->{_config_file}; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
elsif ( ( stat $self->{_config_file} )[9] |
79
|
|
|
|
|
|
|
> ( stat $self->{_json_file} )[9] ) |
80
|
|
|
|
|
|
|
{ |
81
|
1
|
50
|
|
|
|
9
|
return $self->read($key) if $key; |
82
|
0
|
|
|
|
|
0
|
return $self->read; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
else { |
85
|
1
|
|
|
|
|
3
|
$file = $self->{_json_file}; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
else { |
89
|
11
|
100
|
|
|
|
25
|
if ( $self->{_json_file} ) { |
90
|
2
|
|
|
|
|
31
|
$file = $self->{_json_file}; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
else { |
93
|
10
|
|
|
10
|
|
111
|
no warnings qw( uninitialized ); |
|
10
|
|
|
|
|
23
|
|
|
10
|
|
|
|
|
4881
|
|
94
|
9
|
100
|
100
|
|
|
119
|
if (( ( $self->{_self} ) |
|
|
|
66
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
66
|
|
|
|
|
95
|
|
|
|
|
|
|
&& ( defined $self->{_config_file} ) |
96
|
|
|
|
|
|
|
&& ( ( $self->{_config_file} =~ '\(eval ' ) |
97
|
|
|
|
|
|
|
|| ( $self->{_config_file} =~ 'base.pm' ) ) |
98
|
|
|
|
|
|
|
) |
99
|
|
|
|
|
|
|
|| ( $_file eq $self->{_config_file} ) |
100
|
|
|
|
|
|
|
|| ( $0 eq $self->{_config_file} ) |
101
|
|
|
|
|
|
|
) |
102
|
|
|
|
|
|
|
{ |
103
|
2
|
|
|
|
|
11
|
return $self->_raise_error( |
104
|
|
|
|
|
|
|
q{Can't retrieve store from the calling file.}); |
105
|
|
|
|
|
|
|
} |
106
|
7
|
|
|
|
|
15
|
$file = $self->{_config_file}; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
12
|
100
|
|
|
|
36
|
return unless $self->_check_file( $file ); |
111
|
|
|
|
|
|
|
|
112
|
9
|
50
|
|
|
|
761
|
open my $json_file, '<', $file |
113
|
|
|
|
|
|
|
or croak "ERROR: Unable to open JSON file: $file"; |
114
|
9
|
|
|
|
|
12
|
my $json_lines; |
115
|
9
|
|
|
|
|
174
|
while ( <$json_file> ) { |
116
|
24
|
|
|
|
|
77
|
$json_lines .= $_; |
117
|
|
|
|
|
|
|
}; |
118
|
9
|
|
|
|
|
95
|
close $json_file; |
119
|
|
|
|
|
|
|
|
120
|
9
|
|
|
|
|
17
|
eval { $retrieved_hash_ref = decode_json( $json_lines ); }; |
|
9
|
|
|
|
|
101
|
|
121
|
|
|
|
|
|
|
|
122
|
9
|
100
|
|
|
|
28
|
if ( $@ ) { |
123
|
1
|
|
|
|
|
5
|
return $self->_raise_error( |
124
|
|
|
|
|
|
|
qq{$@} ) |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
8
|
100
|
66
|
|
|
90
|
if ( ! ( $retrieved_hash_ref && ref $retrieved_hash_ref eq 'HASH') ) { |
128
|
1
|
|
|
|
|
19
|
return $self->_raise_error( |
129
|
|
|
|
|
|
|
q{Retrieved object isn't a HASH reference.}) |
130
|
|
|
|
|
|
|
}; |
131
|
|
|
|
|
|
|
|
132
|
7
|
|
|
|
|
15
|
$self->{_configuration} = $retrieved_hash_ref; |
133
|
|
|
|
|
|
|
|
134
|
7
|
100
|
|
|
|
49
|
return $self->{_configuration}->{$key} if $key; |
135
|
2
|
|
|
|
|
12
|
return $self->{_configuration}; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
# |
139
|
|
|
|
|
|
|
# SET JSON FILE |
140
|
|
|
|
|
|
|
# |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
sub set_json_file { |
143
|
6
|
|
|
6
|
1
|
4561
|
my $self = shift; |
144
|
6
|
|
|
|
|
12
|
my $configuration_file = shift; |
145
|
|
|
|
|
|
|
|
146
|
6
|
100
|
|
|
|
33
|
if ( $self->_check_file( $configuration_file ) ) { |
147
|
5
|
|
|
|
|
135
|
$self->{_json_file} = $configuration_file; |
148
|
5
|
|
|
|
|
11
|
$self->{_self} = 0; |
149
|
5
|
100
|
66
|
|
|
81
|
if ( ( $self->{_config_file} =~ '\(eval ' ) |
150
|
|
|
|
|
|
|
|| ($self->{_config_file} =~ 'base.pm' ) ) { |
151
|
3
|
|
|
|
|
8
|
delete $self->{_config_file}; |
152
|
|
|
|
|
|
|
} |
153
|
5
|
|
|
|
|
21
|
return $self; |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
else { |
156
|
1
|
|
|
|
|
224
|
return; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
1; |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
__END__ |