Units.pm

filter: Statementstmt 84.2% 48 / 57 Branchbran 42.8% 6 / 14 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 4 totaltotal 75.0% 69 / 92 CC 6 SCAR 5.4
CC
6
Cov
76%
equal
3.0
calc
2.0
readable
2.0
CRAP 1.7
« Units/Convert/Distance.pm ↑ Summary Units/Convert/Base2Metric.pm »
1 package Math::Calc::Units;
2  
3 1 ▶use Math::Calc::Units::Compute qw(compute);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Math::Calc::Units::Rank qw(render render_unit choose_juicy_ones);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Math::Calc::Units::Convert;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use vars qw($VERSION @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 $VERSION = '1.07';
11 1 @EXPORT_OK = qw(calc readable convert equal exact);
12 }
13 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # calc : string -> string
16 # calc : string x true -> magnitude x string
17 5 ▶sub calc ($;$) {
Subroutine calc: called CC=2 SCAR=7.1
Pod undocumented
18 5 my ($expr, $exact) = @_;
19 5 my $v = compute($expr);
20 1 ▶ return $exact ? ($v->[0], render_unit($v->[1])) : render($v);
Branch: 1 branch
BranchTrueFalseTotal
$exact ? : 0 1 1
21 }
22  
23 # readable : string -> ( string )
24 12 ▶sub readable {
Subroutine readable: called CC=2 SCAR=7.1
Pod undocumented
25 12 my $expr = shift;
26 12 my %options;
27 12 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 12 12
28 0 $options{verbose} = shift;
29 } else {
30 12 %options = @_;
31 }
32 12 my $v = compute($expr);
33 12 return map { render($_, \%options) } choose_juicy_ones($v, \%options);
34 }
35  
36 # convert : string x string [ x boolean ] -> string
37 8 ▶sub convert ($$;$) {
Subroutine convert: called CC=2 SCAR=7.0
Pod undocumented
38 8 my ($expr, $units, $exact) = @_;
39 8 my $v = compute($expr);
40 8 my $u = compute("# $units");
41 8 my $c = Math::Calc::Units::Convert::convert($v, $u->[1]);
42 7 ▶ return $exact ? ($c->[0], render_unit($c->[1])) : render($c);
Branch: 1 branch
BranchTrueFalseTotal
$exact ? : 0 7 7
43 }
44  
45 # equal : string x string -> boolean
46 1 ▶use constant EPSILON => 1e-12;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 38 ▶sub equal {
Subroutine equal: called CC=3 SCAR=11.0
Pod undocumented
48 38 my ($u, $v) = @_;
49 38 $u = compute($u);
50 38 $v = compute($v);
51 38 $v = Math::Calc::Units::Convert::convert($v, $u->[1]);
52 37 $u = $u->[0];
53 37 $v = $v->[0];
54 37 ▶ return 1 if ($u == 0) && abs($v) < EPSILON;
Branch: 1 branch
BranchTrueFalseTotal
if $u == 0 and abs $v < "1e-12" 2 35 37
Condition66%2/3
$u == 0 and abs $v < "1e-12"
!ll&&!rl&&r
3502
MC/DC50%1/2
$u == 0 and abs $v < "1e-12"
$u == 0 abs $v < "1e-12"
Truth table66%2/3
A and B
A= $u == 0B= abs $v < "1e-12"
ABresult
0X0
100
111
55 35 return abs(($u-$v)/$u) < EPSILON;
56 }
57  
58 1 ▶if (!(caller)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller) 0 1 1
59 0 my $verbose;
60 my %options;
61 0 ▶ if ($ARGV[0] eq '-v') { shift; $options{verbose} = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($ARGV[0] eq "-v") 0 0 0
62 0 ▶ if ($ARGV[0] eq '-a') { shift; $options{abbreviate} = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($ARGV[0] eq "-a") 0 0 0
63 0 print "$_\n" foreach readable($ARGV[0], %options);
64 }
65  
66 =head1 NAME
67  
68 Math::Calc::Units - Human-readable unit-aware calculator
69  
70 =head1 SYNOPSIS
71  
72 use Math::Calc::Units qw(calc readable convert equal);
73  
74 print "It will take ".calc("10MB/(384Kbps)")." to download\n";
75  
76 my @alternative_descriptions = readable("10MB/(384Kbps)");
77  
78 print "A week is ".convert("1 week", "seconds")." long\n";
79  
80 if (equal("$rate bytes / sec", "1 MB/sec")) { ... };
81  
82 =head1 DESCRIPTION
83  
84 C<Math::Calc::Units> is a simple calculator that keeps track of units. It
85 currently handles combinations of byte sizes and duration only,
86 although adding any other multiplicative types is easy. Any unknown
87 type is treated as a unique user type (with some effort to map English
88 plurals to their singular forms).
89  
90 The primary intended use is via the C<ucalc> script that prints out
91 all of the "readable" variants of a value. For example, C<"3 bytes">
92 will only produce C<"3 byte">, but C<"3 byte / sec"> produces the
93 original along with C<"180 byte / minute">, C<"10.55 kilobyte / hour">,
94 etc.
95  
96 The C<Math::Calc::Units> interface only provides for string-based
97 computations, which could result in a large loss of precision for some
98 applications. If you need the exact result, you may pass in an extra
99 parameter C<'exact'> to C<calc> or C<convert>, causing them to return a
100 2-element list containing the numerical result and a string describing
101 the units of that result:
102  
103 my ($value, $units) = convert("10MB/sec", "GB/day");
104  
105 (In scalar context, they just return the numeric value.)
106  
107 =head2 Examples of use
108  
109 =over 4
110  
111 =item * Estimate transmission rates (e.g., 10MB at 384 kilobit/sec)
112  
113 =item * Estimate performance characteristics (e.g., disk I/O rates)
114  
115 =item * Figure out how long something will take to complete
116  
117 =back
118  
119 I tend to work on performance-sensitive code that involves a lot of
120 network and disk traffic, so I wrote this tool after I became very
121 sick of constantly converting KB/sec to GB/day when trying to figure
122 out how long a run is going to take, or what the theoretical maximum
123 performance would be if we were 100% disk bound. Now I can't live
124 without it.
125  
126 =head2 Contraindications
127  
128 If you are just trying to convert from one unit to another, you'll
129 probably be better off with C<Math::Units> or C<Convert::Units>. This
130 module really only makes sense when you're converting to and from
131 human-readable values.
132  
133 =head1 AUTHOR
134  
135 Steve Fink <sfink@cpan.org>
136  
137 =head1 SEE ALSO
138  
139 ucalc, Math::Units, Convert::Units.
140  
141 =cut
142  
143 1 1;
« Units/Convert/Distance.pm ↑ Summary Units/Convert/Base2Metric.pm »