File Coverage

blib/lib/List/Analyse/Sequence/Analyser/OL/RomanNumerals.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package List::Analyse::Sequence::Analyser::OL::RomanNumerals;
2              
3 1     1   31030 use strict;
  1         2  
  1         42  
4 1     1   6 use warnings;
  1         2  
  1         33  
5              
6 1     1   5 use List::Util qw( first );
  1         6  
  1         383  
7 1     1   942 use Roman;
  0            
  0            
8              
9             our $VERSION = 0.01;
10              
11             sub new {
12             return bless {}, shift;
13             }
14              
15             sub analyse {
16             my $self = shift;
17             my $datum = shift;
18              
19             use Data::Dumper;
20             my %pairs; # For finding multiple possible numerals.
21              
22             while ( $datum =~ /\G(.*?)([divxmlc]+)/ig ) {
23             my ($prefix, $numeral) = ($1, $2);
24              
25             # The prefix should be everything from start of string, which
26             # means we have to keep concatenating the previous one
27             if( %pairs ){
28             # The last prefix we found is the longest one, by definition.
29             my $prev_prefix = (sort { length $a <=> length $b } keys %pairs)[-1];
30            
31             # The last one was not really a number and so remove it.
32             delete $pairs{$prev_prefix} unless $pairs{$prev_prefix};
33              
34             no warnings 'uninitialized'; # sorry.
35             $prefix = $prev_prefix . $pairs{$prev_prefix} . $prefix;
36             }
37              
38             if( isroman( $numeral ) ) {
39             $pairs{$prefix} = $numeral;
40             }
41             else {
42             # If it was not an actual numeral, use the whole lot, and we will delete it next time.
43             $pairs{$prefix . $numeral} = "";
44             }
45             }
46              
47             unless( exists $self->{prefix} ) {
48             # No point doing the rest of this sub if we've not done it before.
49             if( %pairs ) {
50             $self->{prefix} = \%pairs;
51             return 1;
52             }
53              
54             return;
55             }
56             # Now we have found all potential prefix-numeral combinations we can compare
57             # them against the previous set.
58             return unless keys %{ $self->{prefix} };
59              
60             if (exists $self->{prefix}) {
61             for (keys %{ $self->{potential_pairs} }) {
62             delete $self->{prefix}->{$_} unless exists $pairs{$_};
63             }
64              
65             for my $prefix (keys %{ $self->{potential_pairs} }) {
66             my $new_numeral = $pairs{$prefix};
67             my $previous_version = $self->{potential_pairs}->{$prefix};
68              
69             if ( arabic( $new_numeral ) != arabic( $previous_version ) + 1 ) {
70             delete $self->{prefix}->{$prefix};
71             next;
72             }
73              
74             $self->{prefix}->{$prefix} = $new_numeral;
75             }
76              
77              
78             return unless keys %{ $self->{prefix} };
79             }
80             else {
81             $self->{prefix} = \%pairs;
82             }
83              
84             return 1;
85             }
86              
87             sub prefix {
88             return shift->{prefix};
89             }
90              
91             sub done {
92             my $self = shift;
93             my $shortest_prefix = (sort { length $a <=> length $b } keys %{ $self->{prefix} })[-1];
94              
95             $self->{last_numer} = $self->{prefixes}->{$shortest_prefix};
96             $self->{prefix} = $shortest_prefix;
97             }
98              
99             1;
100              
101             __END__