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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|