| 1 | package Math::Calc::Units::Convert; | ||||||||||
| 2 | 1 | ▶ | use base 'Exporter'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use vars qw(@EXPORT_OK); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | BEGIN { @EXPORT_OK = qw(convert reduce canonical find_top construct); }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use Math::Calc::Units::Convert::Multi qw(to_canonical); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | # convert : value x unit -> value | ||||||||||
| 10 | # | ||||||||||
| 11 | # The lower-level conversion routines really only know how to convert | ||||||||||
| 12 | # things to canonical units. But this routine may be called with eg | ||||||||||
| 13 | # 120 minutes -> hours. So we convert both the current and target to | ||||||||||
| 14 | # canonical units, and divide the first by the second. (Doesn't work | ||||||||||
| 15 | # for adding units that aren't multiples of each other, but that's not | ||||||||||
| 16 | # what this tool is for anyway.) | ||||||||||
| 17 | 172 | ▶ | sub convert { | ||||||||
|
Subroutine
convert: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 18 | 172 | my ($from, $unit) = @_; | |||||||||
| 19 | |||||||||||
| 20 | 172 | my $to = [ 1, $unit ]; | |||||||||
| 21 | |||||||||||
| 22 | 172 | my $canon_from = canonical($from); | |||||||||
| 23 | 172 | my $canon_to = canonical($to); | |||||||||
| 24 | |||||||||||
| 25 | 172 | ▶ | die "conversion between incompatible units" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | if not same_units($canon_from->[1], $canon_to->[1]); | ||||||||||
| 27 | |||||||||||
| 28 | 170 | return [ $canon_from->[0] / $canon_to->[0], $unit ]; | |||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | # Are the (canonical) units compatible? (They must have exactly the | ||||||||||
| 32 | # same base units, and each must be raised to exactly the same power.) | ||||||||||
| 33 | 172 | ▶ | sub same_units { | ||||||||
|
Subroutine
same_units: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||
| 34 | 172 | my ($u1, $u2) = @_; | |||||||||
| 35 | 172 | ▶ | return if keys %$u1 != keys %$u2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 172 | ▶ | while (my ($bu1, $bp1) = each %$u1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 191 | ▶ | return if ! exists $u2->{$bu1}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 189 | ▶ | return if $bp1 != $u2->{$bu1}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | } | ||||||||||
| 40 | 170 | return 1; | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | 481 | ▶ | sub canonical { | ||||||||
|
Subroutine
canonical: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 44 | 481 | my ($v) = @_; | |||||||||
| 45 | 481 | my $c = to_canonical($v->[1]); | |||||||||
| 46 | 481 | my $w = [ $v->[0] * $c->[0], $c->[1] ]; | |||||||||
| 47 | 481 | return $w; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 125 | ▶ | sub reduce { | ||||||||
|
Subroutine
reduce: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 51 | 125 | my ($v) = @_; | |||||||||
| 52 | 125 | return canonical($v, 'reduce, please'); | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 7 | ▶ | sub construct { | ||||||||
|
Subroutine
construct: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 56 | 7 | my ($constructor, $args) = @_; | |||||||||
| 57 | 7 | return Math::Calc::Units::Convert::Multi::construct($constructor, $args); | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | 1 | 1; | |||||||||