The problem with using the units(1) database is that it only deals with 
multiplicative relations -- so, e.g., it won't handle temperature.
Units resolvers are not so hard to come by -- the strategy is to try to break
each compound unit out into a collection of fundamental quantities that
are interconvertible (e.g. all lengths get converted to "meters"), while 
keeping track of the conversion constant.  I haven't looked at units(1) in
a while, but it used to do this to both the original units and the destination
units, then take the ratio of the accumulated constants.  That's why the 
"conformability" error messages are sometimes a little weird: they always
refer to the fully expanded SI representation of each unit.

Quoth Aaron Sherman on Monday 28 March 2005 03:40 pm,
> On Mon, 2005-03-28 at 16:00, Larry Wall wrote:
> > I've always thought that we should make use of the database of the
> > "units" program for standardized names of units.  The units database
> > has a pretty good list of which units are just differently scaled
> > units of the actual underlying fundamental dimensions, and a lot
> > of encoded experience in distinguishing ambiguous units names.  It'd
> > be a shame to reinvent all that.
> That makes fine sense, and I think it would be fairly trivial to
> generate a set of roles from the Units database at run-time,
> pre-compiled with the source or both (selectable in some way).
> Of course, there are going to be people who have to re-define chunks of
> that namespace because they have special needs (e.g. money -- this is
> such a huge bear of a problem that it can only be solved for the
> domain-specific cases), but that's fine, and does not preclude your
> suggestion.
> here's a start:
> perl -nle 'while(<>) {print("# $_"),next if /^\s*($|\#)/;$c="";s/\s+\#.*//
> && ($c=$&);($unit,$def)=split /\s+/, $_, 2;if ($def eq "!")
> {$base{$unit}=1;print "class units::$unit does unit { ... }$c"} elsif
> ($unit =~ /-$/){print "# No handling for prefixes yet
> ($unit=$def)$c"}elsif($base{$def}){print "class units::$unit is
> units::$def;$c"}else{print "# No handling for derived units yet
> ($unit=$def)$c"}}' < /usr/share/units.dat

Reply via email to