Format/Number.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Format/Latex.pm ↑ Summary Hardware/Battery.pm »
1 #
2 # $Id$
3 #
4 # format::number Brik
5 #
6 package Metabrik::Format::Number;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use base qw(Metabrik::Shell::Command Metabrik::System::Package);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub brik_properties {
13 return {
14 revision => '$Revision$',
15 tags => [ qw(unstable) ],
16 author => 'GomoR <GomoR[at]metabrik.org>',
17 license => 'http://opensource.org/licenses/BSD-3-Clause',
18 attributes => {
19 thousands_sep => [ qw(char) ],
20 decimal_point => [ qw(char) ],
21 kibi_suffix => [ qw(string) ],
22 kilo_suffix => [ qw(string) ],
23 mebi_suffix => [ qw(string) ],
24 mega_suffix => [ qw(string) ],
25 gibi_suffix => [ qw(string) ],
26 giga_suffix => [ qw(string) ],
27 tebi_suffix => [ qw(string) ],
28 tera_suffix => [ qw(string) ],
29 },
30 attributes_default => {
31 thousands_sep => '.',
32 decimal_point => ',',
33 kibi_suffix => 'Kb',
34 kilo_suffix => 'KB',
35 mebi_suffix => 'Mb',
36 mega_suffix => 'MB',
37 gibi_suffix => 'Gb',
38 giga_suffix => 'GB',
39 tebi_suffix => 'Tb',
40 tera_suffix => 'TB',
41 },
42 commands => {
43 from_number => [ qw(number) ],
44 to_number => [ qw(string) ],
45 },
46 require_modules => {
47 'Number::Format' => [ ],
48 },
49 };
50 }
51  
52 sub _new {
53 my $self = shift;
54  
55 my $x = Number::Format->new(
56 -thousands_sep => $self->thousands_sep,
57 -decimal_point => $self->decimal_point,
58 -kibi_suffix => $self->kibi_suffix,
59 -kilo_suffix => $self->kilo_suffix,
60 -mebi_suffix => $self->mebi_suffix,
61 -mega_suffix => $self->mega_suffix,
62 -gibi_suffix => $self->gibi_suffix,
63 -giga_suffix => $self->giga_suffix,
64 );
65  
66 return $x;
67 }
68  
69 sub from_number {
70 my $self = shift;
71 my ($number) = @_;
72  
73 $self->brik_help_run_undef_arg('from_number', $number) or return;
74  
75 if ($number !~ /^\d+/) {
76 return $self->log->error("from_number: number [$number] is not valid");
77 }
78  
79 my $x = $self->_new;
80  
81 return $x->format_bytes($number);
82 }
83  
84 sub to_number {
85 my $self = shift;
86 my ($string) = @_;
87  
88 $self->brik_help_run_undef_arg('to_number', $string) or return;
89  
90 my $x = $self->_new;
91  
92 my $tebi = $self->tebi_suffix;
93 my $tera = $self->tera_suffix;
94  
95 # Number::Format does not support TB
96 if ($string =~ s{${tera}$}{}) {
97 $string *= 1024;
98 $string .= $self->giga_suffix;
99 }
100 elsif ($string =~ s{${tebi}$}{}) {
101 $string *= 1024 / 8;
102 $string .= $self->gibi_suffix;
103 }
104  
105 return $x->unformat_number($string);
106 }
107  
108 1;
109  
110 __END__
« Format/Latex.pm ↑ Summary Hardware/Battery.pm »