| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Format::Human::Bytes; |
|
2
|
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
116659
|
use warnings; |
|
|
3
|
|
|
|
|
8
|
|
|
|
3
|
|
|
|
|
107
|
|
|
4
|
3
|
|
|
3
|
|
17
|
use strict; |
|
|
3
|
|
|
|
|
7
|
|
|
|
3
|
|
|
|
|
1977
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 NAME |
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
Format::Human::Bytes - Format a bytecount and make it human readable |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 VERSION |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Version 0.06 |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=cut |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $VERSION = '0.06'; |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
Ever showed 12345678 bytes to the user instead of just saying 11MB? |
|
21
|
|
|
|
|
|
|
This module returns you a printable string which is more readable by |
|
22
|
|
|
|
|
|
|
humans than a simple bytecount. |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
use Format::Human::Bytes; |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
$readable = Format::Human::Bytes::base2($bytecount[,$decimals]); |
|
27
|
|
|
|
|
|
|
$readable = Format::Human::Bytes::base10($bytecount[,$decimals]); |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
$readable = Format::Human::Bytes->base2($bytecount[,$decimals]); |
|
30
|
|
|
|
|
|
|
$readable = Format::Human::Bytes->base10($bytecount[,$decimals]); |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
my $fhb = Format::Human::Bytes->new(); |
|
33
|
|
|
|
|
|
|
$readable = $fhb->base2($bytecount[,$decimals]); |
|
34
|
|
|
|
|
|
|
$readable = $fhb->base10($bytecount[,$decimals]); |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
All functions do "intelligent" switching to the next unit, for example: |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
1000 => 1000B |
|
39
|
|
|
|
|
|
|
[...] |
|
40
|
|
|
|
|
|
|
8000 => 8000B |
|
41
|
|
|
|
|
|
|
9000 => 9kB |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
The difference between 1000 bytes and 1500 bytes is usually bigger (for |
|
44
|
|
|
|
|
|
|
example because of a slow link) than between 95kB and 95,5kB. The same |
|
45
|
|
|
|
|
|
|
applies to 8000kB vs. 9 MB and for the other units. |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
Depending on your usage, you may want to specify how many decimals should |
|
48
|
|
|
|
|
|
|
be shown (defaults to no decimals). |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=head1 FUNCTIONS / METHODS |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=head2 new |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
my $fhb = Format::Human::Bytes->new(); |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Creates and returns a Format::Human::Bytes - object. |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=cut |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub new { # URL |
|
61
|
|
|
|
|
|
|
|
|
62
|
2
|
|
|
2
|
1
|
1034
|
my $class = shift; |
|
63
|
2
|
|
|
|
|
4
|
my $Scalar; |
|
64
|
2
|
|
|
|
|
7
|
my $self = bless \$Scalar, $class; |
|
65
|
|
|
|
|
|
|
|
|
66
|
2
|
|
|
|
|
10
|
return $self; |
|
67
|
|
|
|
|
|
|
} |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head2 base2 |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
Callable as a function: |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
$readable = Format::Human::Bytes::base2($bytecount[,$decimals]); |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
Callable as a class method: |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
$readable = Format::Human::Bytes->base2($bytecount[,$decimals]); |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
Callable as a object method: |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
$readable = $fhb->base2($bytecount[,$decimals]); |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
Returns the correct readable form of the given bytecount. |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
Correct in this case means that 1kB are 1024 Bytes which is |
|
86
|
|
|
|
|
|
|
how computers see the world. |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
If you specify a decimal parameter, the result number will have the |
|
89
|
|
|
|
|
|
|
number of decimal numbers you specified. |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=cut |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub base2 { |
|
94
|
102
|
100
|
|
102
|
1
|
25320
|
shift if ref( $_[0] ) ne ''; # Use me as a method if you like |
|
95
|
|
|
|
|
|
|
shift |
|
96
|
102
|
100
|
66
|
|
|
1872
|
if defined( $_[0] ) |
|
97
|
|
|
|
|
|
|
and ( $_[0] eq 'Format::Human::Bytes' ) |
|
98
|
|
|
|
|
|
|
; # Use me as a method if you like |
|
99
|
|
|
|
|
|
|
|
|
100
|
102
|
|
100
|
|
|
265
|
my $Bytes = $_[0] || 0; |
|
101
|
102
|
50
|
|
|
|
222
|
defined($Bytes) or $Bytes = 0; |
|
102
|
|
|
|
|
|
|
|
|
103
|
102
|
|
100
|
|
|
291
|
my $Decimal = $_[1] || 0; |
|
104
|
|
|
|
|
|
|
|
|
105
|
102
|
100
|
|
|
|
422
|
if ( $Bytes > 8192000000000 ) { |
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
106
|
15
|
|
|
|
|
206
|
return sprintf( '%0.' . $Decimal . 'f', $Bytes / 1099511627776 ) . "TB"; |
|
107
|
|
|
|
|
|
|
} |
|
108
|
|
|
|
|
|
|
elsif ( $Bytes > 8192000000 ) { |
|
109
|
15
|
|
|
|
|
192
|
return sprintf( '%0.' . $Decimal . 'f', $Bytes / 1073741824 ) . "GB"; |
|
110
|
|
|
|
|
|
|
} |
|
111
|
|
|
|
|
|
|
elsif ( $Bytes > 8192000 ) { |
|
112
|
12
|
|
|
|
|
405
|
return sprintf( '%0.' . $Decimal . 'f', $Bytes / 1048576 ) . "MB"; |
|
113
|
|
|
|
|
|
|
} |
|
114
|
|
|
|
|
|
|
elsif ( $Bytes > 8192 ) { |
|
115
|
27
|
|
|
|
|
336
|
return sprintf( '%0.' . $Decimal . 'f', $Bytes / 1024 ) . "kB"; |
|
116
|
|
|
|
|
|
|
} |
|
117
|
18
|
|
|
|
|
244
|
elsif ( $Bytes == 0 ) { return sprintf( '%0.' . $Decimal . 'f', 0 ); } |
|
118
|
15
|
|
|
|
|
181
|
else { return sprintf( '%0.' . $Decimal . 'f', $Bytes ) . "B"; } |
|
119
|
|
|
|
|
|
|
} |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head2 base10 |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Callable as a function: |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
$readable = Format::Human::Bytes::base10($bytecount[,$decimals]); |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
Callable as a class method: |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
$readable = Format::Human::Bytes->base10($bytecount[,$decimals]); |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Callable as a object method: |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
$readable = $fhb->base10($bytecount[,$decimals]); |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Returns the incorrect readable form of the given bytecount. |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
Incorrect in this case means that 1kB is 1000 Bytes and 1 MB is |
|
138
|
|
|
|
|
|
|
1000000 bytes which is how some (many) people see the world, but |
|
139
|
|
|
|
|
|
|
it's wrong for computers. |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
If you specify a decimal parameter, the result number will have the |
|
142
|
|
|
|
|
|
|
number of decimal numbers you specified. |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=cut |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub base10 { |
|
147
|
111
|
100
|
|
111
|
1
|
30743
|
shift if ref( $_[0] ) ne ''; # Use me as a method if you like |
|
148
|
|
|
|
|
|
|
shift |
|
149
|
111
|
100
|
66
|
|
|
605
|
if defined( $_[0] ) |
|
150
|
|
|
|
|
|
|
and ( $_[0] eq 'Format::Human::Bytes' ) |
|
151
|
|
|
|
|
|
|
; # Use me as a method if you like |
|
152
|
|
|
|
|
|
|
|
|
153
|
111
|
|
100
|
|
|
288
|
my $Bytes = $_[0] || 0; |
|
154
|
111
|
50
|
|
|
|
229
|
defined($Bytes) or $Bytes = 0; |
|
155
|
|
|
|
|
|
|
|
|
156
|
111
|
|
100
|
|
|
290
|
my $Decimal = $_[1] || 0; |
|
157
|
|
|
|
|
|
|
|
|
158
|
111
|
100
|
|
|
|
437
|
if ( $Bytes > 8192000000000 ) { |
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
159
|
15
|
|
|
|
|
213
|
return sprintf( '%0.' . $Decimal . 'f', $Bytes / 1000000000000 ) . "TB"; |
|
160
|
|
|
|
|
|
|
} |
|
161
|
|
|
|
|
|
|
elsif ( $Bytes > 8192000000 ) { |
|
162
|
15
|
|
|
|
|
273
|
return sprintf( '%0.' . $Decimal . 'f', $Bytes / 1000000000 ) . "GB"; |
|
163
|
|
|
|
|
|
|
} |
|
164
|
|
|
|
|
|
|
elsif ( $Bytes > 8192000 ) { |
|
165
|
12
|
|
|
|
|
174
|
return sprintf( '%0.' . $Decimal . 'f', $Bytes / 1000000 ) . "MB"; |
|
166
|
|
|
|
|
|
|
} |
|
167
|
|
|
|
|
|
|
elsif ( $Bytes > 8192 ) { |
|
168
|
36
|
|
|
|
|
546
|
return sprintf( '%0.' . $Decimal . 'f', $Bytes / 1000 ) . "kB"; |
|
169
|
|
|
|
|
|
|
} |
|
170
|
18
|
|
|
|
|
260
|
elsif ( $Bytes == 0 ) { return sprintf( '%0.' . $Decimal . 'f', 0 ); } |
|
171
|
15
|
|
|
|
|
203
|
else { return sprintf( '%0.' . $Decimal . 'f', $Bytes ) . "B"; } |
|
172
|
|
|
|
|
|
|
} |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=head1 AUTHOR |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
Sebastian Willing, C<< >> |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head1 BUGS |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
|
181
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
|
182
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=head1 SUPPORT |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
perldoc Format::Human::Bytes |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
You can also look for information at: |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=over 4 |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
L |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
L |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=item * CPAN Ratings |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
L |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=item * Search CPAN |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
L |
|
211
|
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=back |
|
213
|
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=head1 HISTORY |
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
The functions are in use since late 2003 or early 2004 but I didn't pack them |
|
218
|
|
|
|
|
|
|
for CPAN before 2009. |
|
219
|
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=head1 LICENSE |
|
222
|
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or |
|
224
|
|
|
|
|
|
|
modify it under the same terms as Perl 5 itself. |
|
225
|
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=cut |
|
228
|
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
1; # End of Format::Human::Bytes |