line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package JCONF::Writer; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
1037
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
65
|
|
4
|
2
|
|
|
2
|
|
8
|
use Carp; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
154
|
|
5
|
2
|
|
|
2
|
|
9
|
use B; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
90
|
|
6
|
2
|
|
|
2
|
|
984
|
use JCONF::Writer::Error; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
1981
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub new { |
11
|
2
|
|
|
2
|
1
|
21
|
my ($class, %opts) = @_; |
12
|
|
|
|
|
|
|
|
13
|
2
|
|
|
|
|
9
|
my $self = { |
14
|
|
|
|
|
|
|
autodie => delete $opts{autodie} |
15
|
|
|
|
|
|
|
}; |
16
|
|
|
|
|
|
|
|
17
|
2
|
50
|
|
|
|
8
|
%opts and croak 'unrecognized options: ', join(', ', keys %opts); |
18
|
|
|
|
|
|
|
|
19
|
2
|
|
|
|
|
10
|
bless $self, $class; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub _err { |
23
|
4
|
|
|
4
|
|
7
|
my ($self, $msg) = @_; |
24
|
|
|
|
|
|
|
|
25
|
4
|
100
|
|
|
|
11
|
unless (defined $msg) { |
26
|
2
|
|
|
|
|
9
|
$self->{last_error} = undef; |
27
|
2
|
|
|
|
|
6
|
return; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
2
|
|
|
|
|
14
|
$self->{last_error} = JCONF::Writer::Error->new($msg); |
31
|
2
|
100
|
|
|
|
8
|
if ($self->{autodie}) { |
32
|
1
|
|
|
|
|
6
|
$self->{last_error}->throw(); |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
1
|
|
|
|
|
9
|
return; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub last_error { |
39
|
1
|
|
|
1
|
1
|
8
|
return $_[0]->{last_error}; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub from_hashref { |
43
|
2
|
|
|
2
|
1
|
25
|
my ($self, $ref) = @_; |
44
|
|
|
|
|
|
|
|
45
|
2
|
|
|
|
|
7
|
$self->_err(undef); |
46
|
|
|
|
|
|
|
|
47
|
2
|
50
|
|
|
|
7
|
if (ref $ref ne 'HASH') { |
48
|
2
|
|
|
|
|
4
|
return $self->_err('Root element should be reference to a HASH'); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
0
|
|
|
|
|
|
my $rv; |
52
|
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
while (my ($name, $value) = each %$ref) { |
54
|
0
|
0
|
|
|
|
|
unless ($name =~ /^\w+$/) { |
55
|
0
|
|
|
|
|
|
return $self->_err("Root key should be bareword, got `$name'"); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
$rv .= $name; |
59
|
0
|
|
|
|
|
|
$rv .= " = "; |
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
|
$self->_write(\$rv, $value, 0); |
62
|
|
|
|
|
|
|
|
63
|
0
|
|
|
|
|
|
$rv .= "\n"; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
|
return $rv; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub _write { |
70
|
0
|
|
|
0
|
|
|
my ($self, $rv_ref, $value, $indents) = @_; |
71
|
|
|
|
|
|
|
|
72
|
0
|
|
|
|
|
|
$indents++; |
73
|
|
|
|
|
|
|
|
74
|
0
|
0
|
|
|
|
|
if (my $ref = ref $value) { |
75
|
0
|
0
|
|
|
|
|
if ($ref eq 'HASH') { |
76
|
0
|
|
|
|
|
|
return $self->_write_hash($rv_ref, $value, $indents); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
0
|
0
|
|
|
|
|
if ($ref eq 'ARRAY') { |
80
|
0
|
|
|
|
|
|
return $self->_write_array($rv_ref, $value, $indents); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
0
|
0
|
0
|
|
|
|
if ($ref eq 'Parse::JCONF::Boolean' || $ref eq 'JCONF::Writer::Boolean') { |
84
|
0
|
|
|
|
|
|
return $self->_write_boolean($rv_ref, $value); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
0
|
0
|
|
|
|
|
if (!defined $value) { |
89
|
0
|
|
|
|
|
|
return $self->_write_null($rv_ref); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
0
|
0
|
0
|
|
|
|
if (B::svref_2object(\$value)->FLAGS & (B::SVp_IOK | B::SVp_NOK) && 0 + $value eq $value && $value * 0 == 0) { |
|
|
|
0
|
|
|
|
|
93
|
0
|
|
|
|
|
|
return $self->_write_number($rv_ref, $value); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
|
$self->_write_string($rv_ref, $value); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub _write_hash { |
100
|
0
|
|
|
0
|
|
|
my ($self, $rv_ref, $value, $indents) = @_; |
101
|
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
|
$$rv_ref .= "{\n"; |
103
|
|
|
|
|
|
|
|
104
|
0
|
|
|
|
|
|
while (my ($k, $v) = each %$value) { |
105
|
0
|
|
|
|
|
|
$$rv_ref .= "\t"x$indents; |
106
|
0
|
|
|
|
|
|
$self->_write_string($rv_ref, $k); |
107
|
0
|
|
|
|
|
|
$$rv_ref .= ": "; |
108
|
0
|
|
|
|
|
|
$self->_write($rv_ref, $v, $indents); |
109
|
0
|
|
|
|
|
|
$$rv_ref .= ",\n"; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
0
|
|
|
|
|
|
$$rv_ref .= "\t"x($indents-1); |
113
|
0
|
|
|
|
|
|
$$rv_ref .= "}"; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub _write_array { |
117
|
0
|
|
|
0
|
|
|
my ($self, $rv_ref, $value, $indents) = @_; |
118
|
|
|
|
|
|
|
|
119
|
0
|
|
|
|
|
|
$$rv_ref .= "[\n"; |
120
|
|
|
|
|
|
|
|
121
|
0
|
|
|
|
|
|
for my $v (@$value) { |
122
|
0
|
|
|
|
|
|
$$rv_ref .= "\t"x$indents; |
123
|
0
|
|
|
|
|
|
$self->_write($rv_ref, $v, $indents); |
124
|
0
|
|
|
|
|
|
$$rv_ref .= ",\n"; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
0
|
|
|
|
|
|
$$rv_ref .= "\t"x($indents-1); |
128
|
0
|
|
|
|
|
|
$$rv_ref .= "]" |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub _write_boolean { |
132
|
0
|
|
|
0
|
|
|
my ($self, $rv_ref, $value) = @_; |
133
|
0
|
0
|
|
|
|
|
$$rv_ref .= $value ? 'true' : 'false'; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub _write_null { |
137
|
0
|
|
|
0
|
|
|
my ($self, $rv_ref) = @_; |
138
|
0
|
|
|
|
|
|
$$rv_ref .= 'null'; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub _write_number { |
142
|
0
|
|
|
0
|
|
|
my ($self, $rv_ref, $value) = @_; |
143
|
0
|
|
|
|
|
|
$$rv_ref .= $value; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub _write_string { |
147
|
0
|
|
|
0
|
|
|
my ($self, $rv_ref, $value) = @_; |
148
|
|
|
|
|
|
|
|
149
|
0
|
|
|
|
|
|
$value =~ s/\x5c/\x5c\x5c/g; |
150
|
0
|
|
|
|
|
|
$value =~ s/"/\x5c"/g; |
151
|
|
|
|
|
|
|
|
152
|
0
|
|
|
|
|
|
$$rv_ref .= '"' . $value . '"'; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
1; |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
__END__ |