general/file/read.pm

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