# if (units) specified after text unit description # (which should understand most normal abbrevs.) # then if the input is a string it is inspected for units, # and the conversion done in the appropriate direction # If the input is purely numeric (if ONLY Perl_looks_like_number() was accessible!) # then the conversion is in the direction specified by the order of declaration time parameters. use Units::Convert FT_IN_2_MTRS => q[ft(')inches(") meters(m)]; print FT_IN_2_MTRS q{5'10"}; # prints '1.7773m' print FT_IN_2_MTRS 5.8333; # prints 1.77773 # No (units) specified on delclaration, input must be numeric, conversion works in 1 direction only. use Units::Convert MPH_2_KPH => q[mph kph]; print MPH_2_KPH 70; # prints 112 print MPH_2_KPH '70mph'; # Causes warn or die my @limits = qw(30 40 50 60 70); print "@{[ MPH_2_KPH @limits ]}"; # prints 50 64 80 96 112 # An extension would be for the user to supply a sprintf-style format string # that is used for the formating/precision of the output. # Once we get string .v. numeric contexts, the sub could determine when to append the units or not use Units::Convert CI_2_CC => 'inch^3(%.2f ci) cm^3(%.f cc)'; print CI_2_CC 500; # prints 8183 print CI_2_CC '500 ci'; # prints '8183 cc' # If an itermediate conversion is required, this could be specified on the declaration # I'm not sure this is a good example, but it's the one that came to mind. use Units::Convert UK_2_METRIC_WEIGHTS => 'stones(st)_pounds(lbs) lbs kilograms(%.1f kilos)'; print UK_2_METRIC_WEIGHTS '11st 7lbs'; # prints '73.2 kilos' print UK_2_METRIC_WEIGHTS 11.5; # prints 73.2 print UK_2_METRIC_WEIGHTS '11.5_'; # prints '73.2 kilos' maybe? # The presence of an underscore forces output formattting (if supplied)?