line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Format::LongNumber; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 NAME |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
Format::LongNumber - Format long numbers to human readable view. |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=cut |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 SYNOPSIS |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
use Format::LongNumber; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my $seconds = 3600*24*3 + 3600*4 + 60*5 + 11; |
16
|
|
|
|
|
|
|
print full_time($seconds); # '3d 4h 5m 11s' |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my $bytes = 123456789; |
19
|
|
|
|
|
|
|
print full_traffic($bytes); # '117Mb 755Kb 277b' |
20
|
|
|
|
|
|
|
print short_traffic($bytes); # '117.74Mb' |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# You may create custom formats by functions: |
23
|
|
|
|
|
|
|
# short_value(%grade_table, $value); |
24
|
|
|
|
|
|
|
# full_value(%grade_table, $value); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# For example: |
27
|
|
|
|
|
|
|
my %my_time_grade = ( |
28
|
|
|
|
|
|
|
3600*24 => " day ", |
29
|
|
|
|
|
|
|
3600 => " hour ", |
30
|
|
|
|
|
|
|
60 => " min ", |
31
|
|
|
|
|
|
|
1 => " sec " |
32
|
|
|
|
|
|
|
); |
33
|
|
|
|
|
|
|
print full_value(\%my_time_grade, 121); # '2 min 1 sec' |
34
|
|
|
|
|
|
|
); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=cut |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
|
40
|
2
|
|
|
2
|
|
57173
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
71
|
|
41
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
1534
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
require Exporter; |
45
|
|
|
|
|
|
|
our @ISA = qw| Exporter |; |
46
|
|
|
|
|
|
|
our @EXPORT = (qw| |
47
|
|
|
|
|
|
|
full_time |
48
|
|
|
|
|
|
|
full_traffic |
49
|
|
|
|
|
|
|
full_number |
50
|
|
|
|
|
|
|
full_value |
51
|
|
|
|
|
|
|
short_time |
52
|
|
|
|
|
|
|
short_traffic |
53
|
|
|
|
|
|
|
short_value |
54
|
|
|
|
|
|
|
short_number |
55
|
|
|
|
|
|
|
|); |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
my %_TIME_GRADE = ( |
58
|
|
|
|
|
|
|
3600*24 => "d ", |
59
|
|
|
|
|
|
|
3600 => "h ", |
60
|
|
|
|
|
|
|
60 => "m ", |
61
|
|
|
|
|
|
|
1 => "s " |
62
|
|
|
|
|
|
|
); |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
my %_TRAFFIC_GRADE = ( |
65
|
|
|
|
|
|
|
1024**4 => "Tb ", |
66
|
|
|
|
|
|
|
1024**3 => "Gb ", |
67
|
|
|
|
|
|
|
1024**2 => "Mb ", |
68
|
|
|
|
|
|
|
1024 => "Kb ", |
69
|
|
|
|
|
|
|
1 => "b" |
70
|
|
|
|
|
|
|
); |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
my %_NUMBER_GRADE = ( |
73
|
|
|
|
|
|
|
1000**3 => ".", |
74
|
|
|
|
|
|
|
1000**2 => ".", |
75
|
|
|
|
|
|
|
#1000 => ".", |
76
|
|
|
|
|
|
|
#1 => "" |
77
|
|
|
|
|
|
|
); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head1 DESCRIPTION |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=over |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=item * full_value(\%grade_table, $total) |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
Abstract function of the final value |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
Params: |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
%grade_table - hash with dimensions, where the key is a value dimension, and the value is the symbol dimension |
90
|
|
|
|
|
|
|
$total - value to bring us to the desired mean |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=back |
93
|
|
|
|
|
|
|
=cut |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub full_value { |
96
|
3
|
|
|
3
|
1
|
5
|
my ($grade_table, $total) = @_; |
97
|
|
|
|
|
|
|
|
98
|
3
|
|
50
|
|
|
8
|
$total ||= 0; |
99
|
|
|
|
|
|
|
|
100
|
3
|
100
|
|
|
|
6
|
$total = 0 if $total < 0; |
101
|
|
|
|
|
|
|
|
102
|
3
|
|
|
|
|
4
|
my $result = ""; |
103
|
3
|
|
|
|
|
11
|
my @grades = sort { $b <=> $a } keys %$grade_table; |
|
16
|
|
|
|
|
23
|
|
104
|
3
|
|
|
|
|
8
|
for my $grade (@grades) { |
105
|
13
|
|
|
|
|
19
|
my $value = int($total / $grade); |
106
|
13
|
100
|
|
|
|
24
|
if ($value) { |
107
|
7
|
|
|
|
|
8
|
$total = $total % $grade; |
108
|
7
|
|
|
|
|
81
|
$result .= $value. $grade_table->{$grade};#. " "; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
3
|
100
|
|
|
|
8
|
unless ($result) { |
113
|
1
|
|
|
|
|
3
|
$result = "0". $grade_table->{$grades[$#grades]}; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
#else { |
116
|
|
|
|
|
|
|
# chop $result; |
117
|
|
|
|
|
|
|
#} |
118
|
|
|
|
|
|
|
|
119
|
3
|
|
|
|
|
13
|
$result =~ s/\s+$//; |
120
|
|
|
|
|
|
|
|
121
|
3
|
|
|
|
|
13
|
return $result; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
# |
124
|
|
|
|
|
|
|
# Wrapper for full_value(time_value) |
125
|
|
|
|
|
|
|
# |
126
|
|
|
|
|
|
|
sub full_time { |
127
|
2
|
|
|
2
|
0
|
3
|
my $seconds = shift; |
128
|
|
|
|
|
|
|
|
129
|
2
|
|
|
|
|
8
|
return full_value(\%_TIME_GRADE, $seconds); |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
# |
132
|
|
|
|
|
|
|
# Wrapper for full_value(traffic_value) |
133
|
|
|
|
|
|
|
# |
134
|
|
|
|
|
|
|
sub full_traffic { |
135
|
1
|
|
|
1
|
0
|
14
|
my $bytes = shift; |
136
|
|
|
|
|
|
|
|
137
|
1
|
|
|
|
|
4
|
return full_value(\%_TRAFFIC_GRADE, $bytes); |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
# |
140
|
|
|
|
|
|
|
# Wrapper for full_value(number_value) |
141
|
|
|
|
|
|
|
# |
142
|
|
|
|
|
|
|
sub full_number { |
143
|
0
|
|
|
0
|
0
|
0
|
my $number = shift; |
144
|
|
|
|
|
|
|
|
145
|
0
|
|
|
|
|
0
|
return full_value(\%_NUMBER_GRADE, $number); |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=over |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=item * short_value(\%grade_table, $total) |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Converts the given value only to the largest grade-value |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=cut |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub short_value { |
157
|
3
|
|
|
3
|
1
|
6
|
my ($grade_table, $total) = @_; |
158
|
|
|
|
|
|
|
|
159
|
3
|
|
50
|
|
|
7
|
$total ||= 0; |
160
|
|
|
|
|
|
|
|
161
|
3
|
100
|
|
|
|
7
|
$total = 0 if $total < 0; |
162
|
|
|
|
|
|
|
|
163
|
3
|
|
|
|
|
4
|
my $result = ""; |
164
|
3
|
|
|
|
|
16
|
my @grades = sort { $b <=> $a } keys %$grade_table; |
|
16
|
|
|
|
|
25
|
|
165
|
3
|
|
|
|
|
6
|
for my $grade (@grades) { |
166
|
8
|
|
|
|
|
61
|
my $value = sprintf("%.2f", $total / $grade); |
167
|
8
|
|
|
|
|
8
|
my $fraction = $total % $grade; |
168
|
8
|
100
|
|
|
|
26
|
if (int $value) { |
169
|
2
|
50
|
|
|
|
5
|
$value = int $value if ($fraction == 0); |
170
|
2
|
|
|
|
|
6
|
$result = $value. $grade_table->{$grade}; |
171
|
2
|
|
|
|
|
7
|
last; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
3
|
100
|
|
|
|
9
|
unless ($result) { |
176
|
1
|
|
|
|
|
3
|
$result = "0". $grade_table->{$grades[$#grades]}; |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
|
179
|
3
|
|
|
|
|
14
|
$result =~ s/\s+$//; |
180
|
|
|
|
|
|
|
|
181
|
3
|
|
|
|
|
19
|
return $result; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
# |
184
|
|
|
|
|
|
|
# Wrapper for short_value(time_value) |
185
|
|
|
|
|
|
|
# |
186
|
|
|
|
|
|
|
sub short_time { |
187
|
2
|
|
|
2
|
0
|
11
|
my $seconds = shift; |
188
|
|
|
|
|
|
|
|
189
|
2
|
|
|
|
|
9
|
return short_value(\%_TIME_GRADE, $seconds); |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
# |
192
|
|
|
|
|
|
|
# Wrapper for short_value(traffic_value) |
193
|
|
|
|
|
|
|
# |
194
|
|
|
|
|
|
|
sub short_traffic { |
195
|
1
|
|
|
1
|
0
|
2
|
my $bytes = shift; |
196
|
|
|
|
|
|
|
|
197
|
1
|
|
|
|
|
3
|
return short_value(\%_TRAFFIC_GRADE, $bytes); |
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
# |
200
|
|
|
|
|
|
|
# Wrapper for short_value(number_value)Í |
201
|
|
|
|
|
|
|
# |
202
|
|
|
|
|
|
|
sub short_number { |
203
|
0
|
|
|
0
|
0
|
|
my $number = shift; |
204
|
|
|
|
|
|
|
|
205
|
0
|
|
|
|
|
|
return short_value(\%_NUMBER_GRADE, $number); |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
# |
208
|
|
|
|
|
|
|
# The End |
209
|
|
|
|
|
|
|
# |
210
|
|
|
|
|
|
|
1; |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=head1 AUTHOR |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
Mikhail N Bogdanov C<< >> |
215
|
|
|
|
|
|
|
|