Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I just released to CPAN a distribution named 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.
use Acme::Roman; print I + II; # III, as expected
That was done with a bit of overload, AUTOLOAD and a part which I am not satisfied with.

Some digression first. The idea came to me a long time ago from the solution to a Ruby quiz, namely

Ruby Quiz - Roman Numerals (#22)
http://rubyquiz.com/quiz22.html
(See the hightlighted solution at the Quiz Summary in the same page.) The trick in Ruby was done by defining strategic methods method_missing (which is kind of Perl AUTOLOAD) and const_missing. The Perl solution was quite natural, and I got to the point where
use Acme::Roman; print I() + II(); # III
worked fine. But when I left the parentheses, it only barfed at me with
$ 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.
and
$ 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.
which revealed the barewords were not resolved with the provided AUTOLOAD. 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? ;-)

The Ruby solution is helped by the availability of const_missing method functionality. The equivalent in Perl would be to overload barewords, but overload documentation says:

Barewords are not covered by overloaded string constants.

Does anyone have any suggestion to make this lazier? Or a different way to implement that?


In reply to AUTOLOAD, overload, and Roman numerals for Perl by ferreira

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (3)
    As of 2020-12-04 01:48 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      How often do you use taint mode?





      Results (58 votes). Check out past polls.

      Notices?