I haven't looked at your code in detail yet, nor tried to use the interface you've got already, specifically so that I would not be influenced.
My first thought on how I would like to use a conversions module is that I would pass the source and destination units and it would create a named sub in my package namespace (like use constant does).
In use, it might look something like this:
# 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 de
+claration 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, convers
+ion 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 whe
+n 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 m
+ind.
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
+)?
Final thought on the precision and under/overflow thing. Perhaps, if a flag is set, the routines could return BigInt/Floats if the standrad precisions will cause accuracy loss? I haven't thought that through, so I don't know what the implications are.
Now I'll read your code and see if I'm completly off-base, but I like to look at things from my own perspective first when I can :^).
If you decide not to go ahead with teh module, let me know and I will.
Examine what is said, not who speaks.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.