Created
March 12, 2012 16:45
-
-
Save pjlsergeant/2023260 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Roman; | |
use Moose; | |
sub AUTOLOAD { | |
our $AUTOLOAD; | |
(my $digits = $AUTOLOAD) =~ s/.+:://g; | |
$digits =~ s/$_->[0]/$_->[1]/ | |
for | |
[ IV => 'IIII' ], | |
[ IX => 'VIIII' ], | |
[ XL => 'XXXX' ], | |
[ XC => 'LXXXX' ]; | |
return $_[0]->_count(reverse split(//, $digits)); | |
} | |
sub _count { | |
my ( $class, $head, @tail ) = @_; | |
return 0 unless defined $head; | |
return | |
{ I => 1, V => 5, X => 10, L => 50, C => 100 }->{ $head } + | |
$class->_count( @tail ) | |
} | |
warn Roman->IX; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment