perlquestion
ferreira
I just released to CPAN a distribution named [dist://Acme-Roman].
(It's propagating yet, so maybe you can't reach immediately from your
favourite CPAN mirror.) As an
Acme module, it is a simple experience to allow using roman
numerals in Perl programs in a straightforward manner.
<c>
use Acme::Roman;
print I + II; # III, as expected
</c>
That was done with a bit of overload, AUTOLOAD and a part which
I am not satisfied with.
<p>
Some digression first. The idea came to me a long time ago
from the solution to a Ruby quiz, namely
<blockquote>
Ruby Quiz - Roman Numerals (#22)<br/>
http://rubyquiz.com/quiz22.html
</blockquote>
(See the hightlighted solution at the Quiz Summary in the same page.)
The trick in Ruby was done by defining strategic methods <c>method_missing</c>
(which is kind of Perl <c>AUTOLOAD</c>) and <c>const_missing</c>.
The Perl solution was quite natural, and I got to the point where
<c>
use Acme::Roman;
print I() + II(); # III
</c>
worked fine. But when I left the parentheses, it
only barfed at me with
<c>
$ perl -w -Ilib -MAcme::Roman -e 'my $x = I+II'
Argument "II" isn't numeric in addition (+) at -e line 1.
Argument "I" isn't numeric in addition (+) at -e line 1.
</c>
and
<c>
$ perl -w -Ilib -MAcme::Roman -e 'print I+II'
Name "main::I" used only once: possible typo at -e line 1.
print() on unopened filehandle I at -e line 1.
</c>
which revealed the barewords were not resolved with the provided
<c>AUTOLOAD</c>. I learned that if I provided an empty protoype
it worked. And that's how I coded -- but that kind of sucks:
I need to define the prototype of 3999 subs (I .. MMMCMXCIX)
to make it work. I think that's too expensive. (Who would like
to play with an Acme module which is as heavy as a useful module?
;-)
<p>
The Ruby solution is helped by the availability of
<c>const_missing</c> method functionality. The equivalent in Perl would
be to overload barewords, but [doc://overload] documentation says:
<blockquote>
Barewords are not covered by overloaded string constants.
</blockquote>
<p>
Does anyone have any suggestion to make this lazier?
Or a different way to implement that?