| 1 | package Math::Calc::Units::Convert::Distance; | ||||||||||||||||||||
| 2 | 1 | ▶ | use base 'Math::Calc::Units::Convert::Metric'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 1 | my %total_unit_map; | |||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 1 | my %ranges = ( default => [ 1, 999 ] ); | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 1 | my %distance_units = ( inch => [ 2.54, 'centimeter' ], | |||||||||||||||||||
| 10 | foot => [ 12, 'inch' ], | ||||||||||||||||||||
| 11 | yard => [ 3, 'foot' ], | ||||||||||||||||||||
| 12 | mile => [ 5280, 'foot' ], | ||||||||||||||||||||
| 13 | ); | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 1 | my %distance_pref = ( meter => 1.1, | |||||||||||||||||||
| 16 | inch => 0.7, | ||||||||||||||||||||
| 17 | foot => 0.9, | ||||||||||||||||||||
| 18 | yard => 0, | ||||||||||||||||||||
| 19 | mile => 1.0, | ||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 1 | my %aliases = ( 'feet' => 'foot', | |||||||||||||||||||
| 23 | ); | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | # Perform all math in terms of meters | ||||||||||||||||||||
| 26 | 147 | ▶ | sub canonical_unit { return 'meter'; } | ||||||||||||||||||
|
Subroutine
canonical_unit: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | # Metric.pm uses this to construct unit names | ||||||||||||||||||||
| 29 | 1 | ▶ | sub abbreviated_canonical_unit { return 'm'; } | ||||||||||||||||||
|
Subroutine
abbreviated_canonical_unit: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | # Preference for this class's canonical unit to be the "major" unit | ||||||||||||||||||||
| 32 | # used. The major unit is the one that determines the range of values | ||||||||||||||||||||
| 33 | # to use for computing the overall preference. For example, if you | ||||||||||||||||||||
| 34 | # have 240000 meters/day, you would want to pick "240km/hour", which | ||||||||||||||||||||
| 35 | # is based on the number "240" being a decent one to use for meters. | ||||||||||||||||||||
| 36 | # If you had instead chosen 'hour' as the major unit, then you | ||||||||||||||||||||
| 37 | # wouldn't like using 240 because 240 hours should really be described | ||||||||||||||||||||
| 38 | # as 10 days. | ||||||||||||||||||||
| 39 | # | ||||||||||||||||||||
| 40 | # Note that the above example is not realistic, because the only units | ||||||||||||||||||||
| 41 | # that are eligible for being chosen as the major unit are the ones in | ||||||||||||||||||||
| 42 | # the numerator. So major_pref() is really only used for something | ||||||||||||||||||||
| 43 | # like "square meter seconds", where you want to choose between | ||||||||||||||||||||
| 44 | # "meter" and "second". | ||||||||||||||||||||
| 45 | 0 | ▶ | sub major_pref { return 1; } | ||||||||||||||||||
|
Subroutine
major_pref: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 0 | ▶ | sub major_variants { | ||||||||||||||||||
|
Subroutine
major_variants: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 48 | 0 | my ($self) = @_; | |||||||||||||||||||
| 49 | 0 | return $self->variants('meter'); | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 34 | ▶ | sub get_ranges { | ||||||||||||||||||
|
Subroutine
get_ranges: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 53 | 34 | return \%ranges; | |||||||||||||||||||
| 54 | } | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | # Return the relative preference of different units. Meters are | ||||||||||||||||||||
| 57 | # preferred over miles, miles over feet. | ||||||||||||||||||||
| 58 | 34 | ▶ | sub get_prefs { | ||||||||||||||||||
|
Subroutine
get_prefs: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 59 | 34 | return \%distance_pref; | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 15 | ▶ | sub singular { | ||||||||||||||||||
|
Subroutine
singular: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 63 | 15 | my ($self, $unit) = @_; | |||||||||||||||||||
| 64 | 15 | $unit = $self->SUPER::singular($unit); | |||||||||||||||||||
| 65 | 15 | ▶ | return $aliases{$unit} || $unit; | ||||||||||||||||||
|
Condition66%2/3
$aliases{$unit} || $unit
MC/DC0%0/2
$aliases{$unit} || $unit
$aliases{$unit}
$unit
Truth table66%2/3
A || B
A= $aliases{$unit}B= $unit
| |||||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 45 | ▶ | sub unit_map { | ||||||||||||||||||
|
Subroutine
unit_map: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 69 | 45 | my ($self) = @_; | |||||||||||||||||||
| 70 | 45 | ▶ | if (keys %total_unit_map == 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | 1 | %total_unit_map = (%{$self->SUPER::unit_map()}, %distance_units); | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | 45 | return \%total_unit_map; | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | # simple_convert : unitName x unitName -> multiplier | ||||||||||||||||||||
| 77 | # | ||||||||||||||||||||
| 78 | 102 | ▶ | sub simple_convert { | ||||||||||||||||||
|
Subroutine
simple_convert: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||
| 79 | 102 | my ($self, $from, $to) = @_; | |||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | # 'm', 'meter', or 'meters' | ||||||||||||||||||||
| 82 | 102 | ▶ | return 1 if $from =~ /^m(eter(s?))?$/i; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 95 | ▶ | if (my $easy = $self->SUPER::simple_convert($from, $to)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 85 | 82 | return $easy; | |||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | # km == kilometer | ||||||||||||||||||||
| 89 | 13 | ▶ | if ($from =~ /^(.)m(eter(s?))?$/i) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 90 | 2 | ▶ | if (my ($prefix) = $self->expand($1)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | 2 | return $self->simple_convert($prefix . "meter", $to); | |||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 11 | return; # Failed | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | # Override Metric::variants because only meters should be given metric | ||||||||||||||||||||
| 99 | # prefixes, not inches, feet, etc. | ||||||||||||||||||||
| 100 | 2 | ▶ | sub variants { | ||||||||||||||||||
|
Subroutine
variants: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 101 | 2 | my ($self, $base) = @_; | |||||||||||||||||||
| 102 | 2 | my $canon = $self->canonical_unit(); | |||||||||||||||||||
| 103 | return ($base, | ||||||||||||||||||||
| 104 | 2 | keys %{ $self->unit_map() }, | |||||||||||||||||||
| 105 | 2 | map { "$_$canon" } $self->get_prefixes()); | |||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 1 | 1; | |||||||||||||||||||