line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::XLogfile; # git description: f0adf57 |
2
|
|
|
|
|
|
|
# ABSTRACT: Read and write xlogfiles |
3
|
|
|
|
|
|
|
|
4
|
12
|
|
|
12
|
|
268414
|
use strict; |
|
12
|
|
|
|
|
50
|
|
|
12
|
|
|
|
|
347
|
|
5
|
12
|
|
|
12
|
|
59
|
use warnings; |
|
12
|
|
|
|
|
23
|
|
|
12
|
|
|
|
|
383
|
|
6
|
12
|
|
|
12
|
|
70
|
use base 'Exporter'; |
|
12
|
|
|
|
|
24
|
|
|
12
|
|
|
|
|
1312
|
|
7
|
12
|
|
|
12
|
|
62
|
use Carp 'croak'; |
|
12
|
|
|
|
|
22
|
|
|
12
|
|
|
|
|
11367
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our @EXPORT_OK = qw(read_xlogfile parse_xlogline each_xlogline write_xlogfile make_xlogline); |
10
|
|
|
|
|
|
|
our %EXPORT_TAGS = (all => \@EXPORT_OK); |
11
|
|
|
|
|
|
|
our $VERSION = '0.06'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub read_xlogfile { |
14
|
4
|
|
|
4
|
1
|
5038
|
my $filename = shift; |
15
|
4
|
|
|
|
|
9
|
my @entries; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
each_xlogline($filename => sub { |
18
|
12
|
|
|
12
|
|
54
|
push @entries, $_; |
19
|
4
|
|
|
|
|
35
|
}); |
20
|
|
|
|
|
|
|
|
21
|
4
|
|
|
|
|
25
|
return @entries; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub parse_xlogline { |
25
|
25
|
|
|
25
|
1
|
197
|
my $input = shift; |
26
|
25
|
|
|
|
|
45
|
my $output = {}; |
27
|
|
|
|
|
|
|
|
28
|
25
|
|
|
|
|
57
|
chomp $input; |
29
|
|
|
|
|
|
|
|
30
|
25
|
|
|
|
|
156
|
my @fields = split /:/, $input; |
31
|
|
|
|
|
|
|
|
32
|
25
|
|
|
|
|
52
|
for my $field (@fields) { |
33
|
100
|
|
|
|
|
250
|
my ($key, $value) = split /=/, $field; |
34
|
100
|
100
|
|
|
|
293
|
return if !defined($value); # no = found |
35
|
|
|
|
|
|
|
|
36
|
98
|
|
|
|
|
208
|
$output->{$key} = $value; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
23
|
|
|
|
|
90
|
return $output; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub each_xlogline { |
43
|
5
|
|
|
5
|
1
|
903
|
my $filename = shift; |
44
|
5
|
|
|
|
|
11
|
my $code = shift; |
45
|
|
|
|
|
|
|
|
46
|
5
|
50
|
|
|
|
182
|
open my $handle, '<', $filename |
47
|
|
|
|
|
|
|
or croak "Unable to read $filename for reading: $!"; |
48
|
|
|
|
|
|
|
|
49
|
5
|
|
|
|
|
75
|
while (<$handle>) { |
50
|
15
|
|
100
|
|
|
44
|
local $_ = parse_xlogline($_) || {}; |
51
|
15
|
|
|
|
|
41
|
$code->($_); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
5
|
50
|
|
|
|
67
|
close $handle |
55
|
|
|
|
|
|
|
or croak "Unable to close filehandle: $!"; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub write_xlogfile { |
59
|
2
|
|
|
2
|
1
|
2019
|
my $entries = shift; |
60
|
2
|
|
|
|
|
8
|
my $filename = shift; |
61
|
|
|
|
|
|
|
|
62
|
2
|
50
|
|
|
|
138
|
open my $handle, '>', $filename |
63
|
|
|
|
|
|
|
or croak "Unable to open '$filename' for writing: $!"; |
64
|
|
|
|
|
|
|
|
65
|
2
|
|
|
|
|
9
|
for my $entry (@$entries) { |
66
|
6
|
50
|
|
|
|
10
|
print {$handle} make_xlogline($entry, 1), "\n" |
|
6
|
|
|
|
|
22
|
|
67
|
|
|
|
|
|
|
or croak "Error occurred during print: $!"; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
2
|
50
|
|
|
|
130
|
close $handle |
71
|
|
|
|
|
|
|
or croak "Unable to close filehandle: $!"; |
72
|
|
|
|
|
|
|
|
73
|
2
|
|
|
|
|
14
|
return; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub make_xlogline { |
77
|
24
|
|
|
24
|
1
|
26336
|
my $input = shift; |
78
|
24
|
|
|
|
|
48
|
my $correct = shift; |
79
|
24
|
|
|
|
|
42
|
my @fields; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# code duplication is bad, but not that much is being duplicated |
82
|
24
|
100
|
|
|
|
118
|
if (!$correct) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
83
|
8
|
|
|
|
|
47
|
while (my ($key, $value) = each %$input) { |
84
|
41
|
100
|
|
|
|
103
|
if ($key =~ /([=:\n])/) { |
85
|
3
|
100
|
|
|
|
7
|
my $bad = $1; $bad = $bad eq "\n" ? "newline" : "'$bad'"; |
|
3
|
|
|
|
|
10
|
|
86
|
3
|
|
|
|
|
6
|
$key =~ s/\n/\\n/; |
87
|
3
|
|
|
|
|
393
|
croak "Key '$key' contains invalid character: $bad."; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
38
|
100
|
|
|
|
93
|
if ($value =~ /([:\n])/) { |
91
|
2
|
100
|
|
|
|
5
|
my $bad = $1; $bad = $bad eq "\n" ? "newline" : "'$bad'"; |
|
2
|
|
|
|
|
6
|
|
92
|
2
|
|
|
|
|
3
|
$key =~ s/\n/\\n/; $value =~ s/\n/\\n/; |
|
2
|
|
|
|
|
4
|
|
93
|
2
|
|
|
|
|
195
|
croak "Value '$value' (of key '$key') contains invalid character: $bad."; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
36
|
|
|
|
|
132
|
push @fields, "$key=$value"; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
elsif ($correct == -1) { |
100
|
5
|
|
|
|
|
24
|
while (my ($key, $value) = each %$input) { |
101
|
5
|
|
|
|
|
24
|
push @fields, "$key=$value"; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
elsif ($correct == 1) { |
105
|
11
|
|
|
|
|
284
|
while (my ($key, $value) = each %$input) { |
106
|
23
|
|
|
|
|
42
|
$key =~ y/\n:=/ __/; |
107
|
23
|
|
|
|
|
41
|
$value =~ y/\n:/ _/; |
108
|
23
|
|
|
|
|
113
|
push @fields, "$key=$value"; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
19
|
|
|
|
|
427
|
return join ':', @fields; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
1; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
__END__ |