Units::Height::DWIM - Parses heights of people
use Units::Height::DWIM qw(parse_height height2metric);
my($meters, $system)=parse_height($some_text); die "Unable to parse $some_text\n" unless defined $meters; print "$some_text is $meters meters in $system units";
$meters=height2metric("6'3"); print "6 foot 3 is $meters meters\n";
This module parses text and attemps to extract a height. It assumes you are looking at a human height. The following forms are accepted:
Feet and inches: 3 4' 4'10 5'3"
Centimeters: 120
Meters: 1.35 1,5 2
my($meters, $system)=height2meters($some_string);
Attemps to parse $some_string
and returns undef()
if it can't. The height in meters. $system
is one of imperial
or metric
, depending on wether $some_string
was in metric or not.
Parsing a lone number can be ambiguous. Any number smaller than 3 is assumed to be in meters. Any number between 3 and 10 is assumed to be feet. Any number greater then 10 is assumed to be centimeters. This means that
3 is 3 meters 3.1 is 3.1 feet (ie 0.94488 meters) 6 is 6 feet (ie 1.8288 meters) 10 is 10 feet (ie 3.048 meters) 10.1 is 10.1 cm (ie 0.101 meters) 130 is 130 cm (ie 1.3 meters)
Also, this means you will have to be smart if you want people to input the height of their small children.
print pretty_height($meters, $unit);
Converts the height $meters
into a pretty-printed string.
$unit
can be one of 'metric' or 'm' (in which case the height is returned with 2
digits after the decimal), 'centimeter' or 'cm' (in which case the height
is multiplied by 100 and rounded) , or 'imperial' (in which case the height
is returned in either feet' or feet'inches`` format).
my $meters=height2meters($some_string);
Parses $some_string
and returns the height in meters, or undef()
if
$some_string
can't be parsed.
Philip Gwyn, perl at pied.nu
perl(1).