line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package NKTI::general::file::read; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
9
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
37
|
|
4
|
1
|
|
|
1
|
|
8
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
35
|
|
5
|
1
|
|
|
1
|
|
537
|
use JSON; |
|
1
|
|
|
|
|
10404
|
|
|
1
|
|
|
|
|
7
|
|
6
|
1
|
|
|
1
|
|
166
|
use Data::Dumper; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
1092
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
# Define Version : |
9
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
10
|
|
|
|
|
|
|
our $VERSION = '0.15'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# Create Constructor Module : |
13
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
14
|
|
|
|
|
|
|
# |
15
|
|
|
|
|
|
|
=head1 CONSTRUCTOR MODULE new() |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Parameter Modul : |
18
|
|
|
|
|
|
|
---------------------------------------- |
19
|
|
|
|
|
|
|
_loc_file => Parameter yang berisi Lokasi File yang akan dibuka |
20
|
|
|
|
|
|
|
_type => Parameter yang berisi tipe pembacaan file. |
21
|
|
|
|
|
|
|
Ex : just | dbconf. |
22
|
|
|
|
|
|
|
=head2 OUTPUT : |
23
|
|
|
|
|
|
|
Output dari Constructor tergantung dari parameter _type yang dimasukkan. |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=cut |
26
|
|
|
|
|
|
|
sub new { |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# Define Parameter Module : |
29
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
30
|
0
|
|
|
0
|
0
|
|
my $class = shift; |
31
|
0
|
|
|
|
|
|
my $self = { |
32
|
|
|
|
|
|
|
_loc_file => shift, |
33
|
|
|
|
|
|
|
_type => shift |
34
|
|
|
|
|
|
|
}; |
35
|
0
|
|
|
|
|
|
bless $self, $class; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# Declare scalr for parameter module : |
38
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
39
|
0
|
|
|
|
|
|
my $loc_files = $self->{_loc_file}; |
40
|
0
|
|
|
|
|
|
my $type = $self->{_type}; |
41
|
0
|
|
|
|
|
|
my $data; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# Check IF $type == 'just' : |
44
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
45
|
0
|
0
|
|
|
|
|
if ($type eq 'just') |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
46
|
|
|
|
|
|
|
{ |
47
|
|
|
|
|
|
|
# Read Files : |
48
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
49
|
0
|
|
|
|
|
|
$data = &read($loc_files); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
# End of check IF $type == 'just'. |
52
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# for IF $type == 'json' : |
55
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
56
|
|
|
|
|
|
|
elsif ($type eq 'json') { |
57
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
58
|
|
|
|
|
|
|
# Read Files : |
59
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
60
|
0
|
|
|
|
|
|
$data = json_berkas($loc_files); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# Check IF $type == 'dbconf' : |
64
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
65
|
|
|
|
|
|
|
elsif ($type eq 'dbconf') |
66
|
|
|
|
|
|
|
{ |
67
|
|
|
|
|
|
|
# Read Files : |
68
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
69
|
0
|
|
|
|
|
|
$data = dbconf($loc_files); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
# End of check IF $type == 'dbconf'. |
72
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# Check IF $type != 'dbconf' OR $type != 'just' : |
75
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
76
|
|
|
|
|
|
|
else { |
77
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
78
|
|
|
|
|
|
|
# Debug Files : |
79
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
80
|
0
|
|
|
|
|
|
$data = 'Unsupported type file'; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
# Check IF $type != 'dbconf' OR $type != 'just'. |
83
|
|
|
|
|
|
|
# ================================================================ |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# Return result : |
86
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
87
|
0
|
|
|
|
|
|
return $data; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
# End of Create Constructor Module. |
90
|
|
|
|
|
|
|
# =========================================================================================================== |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
# Create Subroutine for Just Read Files : |
93
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
94
|
|
|
|
|
|
|
=head1 SUBROUTINE read() |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
Parameter Modul : |
97
|
|
|
|
|
|
|
---------------------------------------- |
98
|
|
|
|
|
|
|
_loc_file => Parameter yang berisi |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head2 OUTPUT : |
101
|
|
|
|
|
|
|
Output dari subroutine ini dalam bentuk String. |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=cut |
104
|
|
|
|
|
|
|
sub read { |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# Define parameter module : |
107
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
108
|
0
|
|
|
0
|
0
|
|
my ($loc_files) = @_; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# Define scalar for result : |
111
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
112
|
0
|
|
|
|
|
|
my $data = ''; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
# Open File : |
115
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
116
|
0
|
0
|
|
|
|
|
open(FH, '<', $loc_files) or die $! . " - " . $loc_files; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
# While Loop for : |
119
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------------------- |
120
|
0
|
|
|
|
|
|
while (my $lines = <FH>) |
121
|
|
|
|
|
|
|
{ |
122
|
|
|
|
|
|
|
# CLean white space in first and end : |
123
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
124
|
0
|
|
|
|
|
|
chomp $lines; |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
# Placing fill file into scalar $pre_data : |
127
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
128
|
0
|
|
|
|
|
|
$data .= $lines; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
# End of While loop for . |
131
|
|
|
|
|
|
|
# ======================================================================================== |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
# CLose File : |
134
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
135
|
0
|
|
|
|
|
|
close (FH); |
136
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
137
|
|
|
|
|
|
|
# Return Result : |
138
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
139
|
0
|
|
|
|
|
|
return $data; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
# End of Create Subroutine for Just Read Files. |
142
|
|
|
|
|
|
|
# =========================================================================================================== |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
# Subroutine for read JSON File : |
145
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
146
|
|
|
|
|
|
|
=head1 json_berkas() |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Deskripsi subroutine json_berkas() : |
149
|
|
|
|
|
|
|
---------------------------------------- |
150
|
|
|
|
|
|
|
Subroutine yang berfungsi untuk membaca file JSON. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Parameter subroutine json_berkas() : |
153
|
|
|
|
|
|
|
---------------------------------------- |
154
|
|
|
|
|
|
|
No Parameter Subroutine : |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head2 OUTPUT : |
157
|
|
|
|
|
|
|
Output dari subroutine ini yaitu dalam bentuk Hashref; |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=cut |
160
|
|
|
|
|
|
|
sub json_berkas { |
161
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
162
|
|
|
|
|
|
|
# Define parameter subroutine : |
163
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
164
|
0
|
|
|
0
|
0
|
|
my ($loc_file) = @_; |
165
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
166
|
|
|
|
|
|
|
# Define scalar for place result : |
167
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
168
|
0
|
|
|
|
|
|
my $pre_data = ''; |
169
|
0
|
|
|
|
|
|
my $data = {}; |
170
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
171
|
|
|
|
|
|
|
# Action open files : |
172
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
173
|
0
|
0
|
|
|
|
|
open(FH, "<", $loc_file) or die $!; |
174
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
175
|
|
|
|
|
|
|
# While loop for get fill file : |
176
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
177
|
0
|
|
|
|
|
|
while (my $lines = <FH>) { |
178
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
179
|
|
|
|
|
|
|
# Clean First and End Space : |
180
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
181
|
0
|
|
|
|
|
|
chomp ($lines); |
182
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
183
|
|
|
|
|
|
|
# Placing result files into scalar $pre1_data : |
184
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
185
|
0
|
|
|
|
|
|
$pre_data .= $lines; |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
188
|
|
|
|
|
|
|
# Action close Files : |
189
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
190
|
0
|
|
|
|
|
|
close (FH); |
191
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
192
|
|
|
|
|
|
|
# Regex : |
193
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
194
|
0
|
|
|
|
|
|
$pre_data =~ s/(\{)\s+/\{/g; |
195
|
0
|
|
|
|
|
|
$pre_data =~ s/(\})\s+/\}/g; |
196
|
0
|
|
|
|
|
|
$pre_data =~ s/(\[)\s+/\[/g; |
197
|
0
|
|
|
|
|
|
$pre_data =~ s/(\])\s+/\]/g; |
198
|
0
|
|
|
|
|
|
$pre_data =~ s/(\]\,)\s+/\]\,/g; |
199
|
0
|
|
|
|
|
|
$pre_data =~ s/^\s+//g; |
200
|
0
|
|
|
|
|
|
$pre_data =~ s/(\,)\s+/\,/g; |
201
|
0
|
|
|
|
|
|
$pre_data =~ s/\s+[\{]/\{/g; |
202
|
0
|
|
|
|
|
|
$pre_data =~ s/\s+[\}]/\}/g; |
203
|
0
|
|
|
|
|
|
$pre_data =~ s/\s+[\[]/\[/g; |
204
|
0
|
|
|
|
|
|
$pre_data =~ s/\s+[\]]/\]/g; |
205
|
0
|
|
|
|
|
|
$pre_data =~ s/(\,\])/\]/g; |
206
|
0
|
|
|
|
|
|
$pre_data =~ s/(\,\})/\}/g; |
207
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
208
|
|
|
|
|
|
|
# decode JSON : |
209
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
210
|
0
|
|
|
|
|
|
$data = decode_json $pre_data; |
211
|
|
|
|
|
|
|
#print $pre_data."\n"; |
212
|
|
|
|
|
|
|
#print 'Lokasi File : '.$loc_file."\n"; |
213
|
|
|
|
|
|
|
#print 'data'; |
214
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
215
|
|
|
|
|
|
|
# Return result : |
216
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
217
|
0
|
|
|
|
|
|
return $data; |
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
# End of Subroutine for read JSON File |
220
|
|
|
|
|
|
|
# =========================================================================================================== |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
# Create Subroutine for Read File DB Config and data db config : |
223
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
224
|
|
|
|
|
|
|
=head1 SUBROUTINE dbconf() |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
Parameter Modul : |
227
|
|
|
|
|
|
|
---------------------------------------- |
228
|
|
|
|
|
|
|
Tidak ada parameter Subroutine, aksi dalam module berdasarkan input parameter dari Constructor. |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=head2 OUTPUT : |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
Output dari subroutine ini dalam bentuk String JSON. |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=cut |
235
|
|
|
|
|
|
|
sub dbconf { |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
# Define parameter module : |
238
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
239
|
0
|
|
|
0
|
0
|
|
my ($loc_file) = @_; |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
# Define scalar for result files : |
242
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
243
|
0
|
|
|
|
|
|
my $pre1_data = ''; |
244
|
0
|
|
|
|
|
|
my @pre2_data; |
245
|
|
|
|
|
|
|
my %pre3_data; |
246
|
0
|
|
|
|
|
|
my $data = ''; |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
# Action open files : |
249
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
250
|
0
|
0
|
|
|
|
|
open(FH, "<", $loc_file) or die $!; |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
# While loop for get fill file : |
253
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
254
|
0
|
|
|
|
|
|
while (my $lines = <FH>) { |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
# Clean First and End Space : |
257
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
258
|
0
|
|
|
|
|
|
chomp ($lines); |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
# Placing result files into scalar $pre1_data : |
261
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
262
|
0
|
|
|
|
|
|
$pre1_data .= $lines; |
263
|
|
|
|
|
|
|
} |
264
|
|
|
|
|
|
|
# End of while loop for get fill file. |
265
|
|
|
|
|
|
|
# ================================================================ |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
# Action close Files : |
268
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
269
|
0
|
|
|
|
|
|
close (FH); |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
# Clean Serveral String : |
272
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
273
|
0
|
|
|
|
|
|
$pre1_data =~ s/(?:\<\?php)//g; |
274
|
0
|
|
|
|
|
|
$pre1_data =~ s/(?:\?\>)//g; |
275
|
0
|
|
|
|
|
|
$pre1_data =~ s/[\s+]//g; |
276
|
0
|
|
|
|
|
|
$pre1_data =~ s/\/\*([^*]|[\r\n]|(\*([^\/]|[\r\n])))*\*\///g; |
277
|
0
|
|
|
|
|
|
$pre1_data =~ s/(\;\$dbconfig_data=array)(.*)//g; |
278
|
0
|
|
|
|
|
|
$pre1_data =~ m/(\$.*)(\()(.*)(\))/; |
279
|
0
|
|
|
|
|
|
$pre1_data = $3; |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
# Split Result Clean String and convert to array : |
282
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
283
|
0
|
|
|
|
|
|
@pre2_data = split(/,/, $pre1_data); |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
# Prepare To While loop for Filter result clean : |
286
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
287
|
0
|
|
|
|
|
|
my $i = 0; |
288
|
0
|
|
|
|
|
|
my $key_pre2_data = keys (@pre2_data); |
289
|
0
|
|
|
|
|
|
my $until_loop = $key_pre2_data; |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
# While Loop for Filter result clean : |
292
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------------------- |
293
|
0
|
|
|
|
|
|
while ($i < $until_loop) { |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
# Define scalar for index array : |
296
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
297
|
0
|
|
|
|
|
|
my $index = $pre2_data[$i]; |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
# Get data form result regex : |
300
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
301
|
0
|
|
|
|
|
|
$index =~ m/(\')(\w+)(\'\=\>\')(.*)([\'])/g; |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
# Placing result into hash %pre3_data : |
304
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
305
|
0
|
|
|
|
|
|
$pre3_data{$2} = $4; |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
# Auto Increment : |
308
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
309
|
0
|
|
|
|
|
|
$i++; |
310
|
|
|
|
|
|
|
} |
311
|
|
|
|
|
|
|
# End of While loop for Filter result clean. |
312
|
|
|
|
|
|
|
# ======================================================================================== |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
# Placing result into hash %data : |
315
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
316
|
0
|
|
|
|
|
|
$data = encode_json \%pre3_data; |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
# Return Result : |
319
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
320
|
0
|
|
|
|
|
|
return $data; |
321
|
|
|
|
|
|
|
} |
322
|
|
|
|
|
|
|
# End of Create Subroutine for Read File DB Config and data db config. |
323
|
|
|
|
|
|
|
# =========================================================================================================== |
324
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
1; |
326
|
|
|
|
|
|
|
__END__ |
327
|
|
|
|
|
|
|
=head1 AUTHOR |
328
|
|
|
|
|
|
|
Achmad Yusri Afandi, (linuxer08@gmail.com) |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
331
|
|
|
|
|
|
|
Copyright (c) 2016, Achmad Yusri Afandi, All Rights reserved. |
332
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
Pustaka yang berfungsi untuk membuka File. |
334
|
|
|
|
|
|
|
|
335
|
|
|
|
|
|
|
=cut |