File Coverage

blib/lib/Locale/Maketext/ManyPluralForms.pm
Criterion Covered Total %
statement 28 30 93.3
branch 5 8 62.5
condition n/a
subroutine 6 7 85.7
pod 1 1 100.0
total 40 46 86.9


line stmt bran cond sub pod time code
1             package Locale::Maketext::ManyPluralForms;
2              
3 1     1   13827 use strict;
  1         1  
  1         22  
4 1     1   3 use warnings;
  1         1  
  1         39  
5              
6             our $VERSION = '0.02';
7              
8             require Locale::Maketext::Lexicon;
9              
10 1     1   362 use parent 'Locale::Maketext';
  1         221  
  1         3  
11              
12             sub import {
13 1     1   96 shift;
14 1         4 Locale::Maketext::Lexicon->import(@_);
15             }
16              
17             sub plural {
18 3     3 1 2334 my ($self, $num, @strings) = @_;
19 3 100       9 unless ($self->{_plural}) {
20 1         2 my $class = ref $self;
21 1     1   8655 no strict 'refs'; ## no critic
  1         1  
  1         181  
22 1         1 my $header = ${"${class}::Lexicon"}{"__Plural-Forms"};
  1         4  
23 1 50       2 if ($header) {
24 1         6 $header =~ s/^.*plural\s*=\s*([^;]+);.*$/$1/;
25 1         12 $header =~ s/\[_([0-9]+)\]/%$1/g;
26 1 50       14 die "Invalid expression for plural: $header" if $header =~ /\$|n\s*\(|[A-Za-mo-z]|nn/;
27 1         9 $header =~ s/n/\$_[0]/g;
28 1         91 eval "\$self->{_plural} = sub { return $header }";
29             } else {
30 0     0   0 $self->{_plural} = sub { return $_[0] != 1 };
  0         0  
31             }
32             }
33 3         62 my $pos = $self->{_plural}($num);
34 3 50       5 $pos = $#strings if $pos > $#strings;
35 3         19 return sprintf $strings[$pos], $num;
36             }
37              
38             1;
39              
40             __END__