Vitals/Util.pm

filter: Statementstmt 100% 23 / 23 Branchbran 75.0% 3 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 97.1% 34 / 35 CC 3 SCAR 4.1
CC
3
Cov
96%
inch2cm
2.0
cm2inch
2.0
BEGIN
1.0
CRAP 1.5
« Vitals/Circumference.pm ↑ Summary Vitals.pm »
1 package Data::Vitals::Util;
2  
3 =pod
4  
5 =head1 NAME
6  
7 Data::Vitals::Util - Utility methods for the Perl "Vital Statistics" Library
8  
9 =head1 DESCRIPTION
10  
11 Data::Vitals::Util defines a set of functions that are used in various
12 places within the L<Data::Vitals> classes.
13  
14 All functions are importable using the standard Exporter mechanism
15  
16 use Data::Vitals::Util 'cm2inch';
17  
18 =head1 FUNCTIONS
19  
20 =cut
21  
22 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 11 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 11 ▶use vars qw{$VERSION @ISA @EXPORT_OK};
Subroutine BEGIN: called CC=1 SCAR=0.0
26 11 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
27 11 $VERSION = '0.05';
28 11 @ISA = 'Exporter';
29 11 @EXPORT_OK = qw{cm2inch inch2cm};
30 }
31  
32  
33  
34  
35  
36 # A pair of cm <-> inch conversion functions.
37 # These are specialised DWIM converters for body measurements.
38 # A key requirement for the integrity of the data is that the conversion
39 # path inch->cm->inch is always guarenteed to be the same value.
40 # The same is NOT true for cm->inch->cm, but since we always store and
41 # calculate in cm this should not be a problem.
42  
43 =pod
44  
45 =head2 cm2inch $cm
46  
47 The C<cm2inch> function is a specialised method for converting a centimetre
48 measurement to inchs. It converts using the standard 2.54004 multiplier,
49 but rounds down to the half-inch.
50  
51 The algorithm used is specifically designed so that, when used as a pair
52 with the inch2cm method, any value that C<cm2inch(inch2cm($x)) == $x> is
53 always true, although it may not be true of the reverse.
54  
55 It takes as argument a number without any 'cm' indicator, and returns the
56 number of inches as a similar plain number.
57  
58 =cut
59  
60 204 ▶sub cm2inch {
Subroutine cm2inch: called CC=2 SCAR=6.9
Pod documented
61 204 my $cm = 0 + shift;
62  
63 # We round down slightly, but it should be less than half
64 # an inch and so not a big issue from a fitting perspective.
65 204 my $inch = int($cm / 2.54004);
66  
67 # Support half-inches
68 204 my $part = ($cm / 2.54004) - $inch;
69 204 ▶ $inch += 0.5 if $part >= 0.5;
Branch: 1 branch
BranchTrueFalseTotal
if $part >= "0.5" 95 109 204
70  
71 204 $inch;
72 }
73  
74 =pod
75  
76 =head2 inch2cm $cm
77  
78 The C<inch2cm> function is a specialised method for converting an inch
79 measurement to centimetres. It converts using the standard 2.54004
80 multiplier, but rounds up to the nearest centimetre.
81  
82 The algorithm used is specifically designed so that, when used as a pair
83 with the cm2inch method, any value that C<cm2inch(inch2cm($x)) == $x> is
84 always true, although it may not be true of the reverse.
85  
86 It takes as argument a number without any 'inch' indicator, and returns the
87 number of centimetres as a similar plain number.
88  
89 =cut
90  
91 188 ▶sub inch2cm {
Subroutine inch2cm: called CC=2 SCAR=7.0
Pod documented
92 188 my $inch = 0 + shift;
93  
94 # We round up slightly, but it should be less than a cm
95 # and so not a big issue from a fitting perspective.
96 188 my $cm = $inch * 2.54004;
97 188 ▶ if ( $cm - int($cm) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cm - int($cm)) 188 0 188
98 188 $cm = int($cm) + 1;
99 }
100  
101 188 $cm;
102 }
103  
104 11 1;
105  
106 =pod
107  
108 =head1 SUPPORT
109  
110 Bugs should always be reported via the CPAN bug tracker
111  
112 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-Vitals>
113  
114 For other issues, contact the maintainer.
115  
116 =head1 AUTHORS
117  
118 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
119  
120 =head1 ACKNOWLEGEMENTS
121  
122 Thank you to Phase N (L<http://phase-n.com/>) for permitting
123 the open sourcing and release of this distribution.
124  
125 =head1 COPYRIGHT
126  
127 Copyright 2004 - 2008 Adam Kennedy.
128  
129 This program is free software; you can redistribute
130 it and/or modify it under the same terms as Perl itself.
131  
132 The full text of the license can be found in the
133 LICENSE file included with this module.
134  
135 =cut
« Vitals/Circumference.pm ↑ Summary Vitals.pm »