File Coverage

blib/lib/Locale/Maketext/ManyPluralForms.pm
Criterion Covered Total %
statement 31 33 93.9
branch 7 10 70.0
condition n/a
subroutine 6 7 85.7
pod 1 1 100.0
total 45 51 88.2


line stmt bran cond sub pod time code
1             package Locale::Maketext::ManyPluralForms;
2              
3 1     1   16877 use strict;
  1         2  
  1         20  
4 1     1   3 use warnings;
  1         1  
  1         34  
5              
6             our $VERSION = '0.03';
7              
8             require Locale::Maketext::Lexicon;
9              
10 1     1   359 use parent 'Locale::Maketext';
  1         205  
  1         3  
11              
12             sub import {
13 1     1   86 shift;
14 1         4 Locale::Maketext::Lexicon->import(@_);
15             }
16              
17             sub plural {
18 4     4 1 2552 my ($self, $num, @strings) = @_;
19 4 100       9 unless (defined $num) {
20 1         15 warn 'Use of uninitialized value $num in '. ref($self). " with params: '". join(";", @strings) ."'";
21 1         41 $num = 0;
22             }
23 4 100       8 unless ($self->{_plural}) {
24 1         2 my $class = ref $self;
25 1     1   8733 no strict 'refs'; ## no critic
  1         2  
  1         180  
26 1         0 my $header = ${"${class}::Lexicon"}{"__Plural-Forms"};
  1         4  
27 1 50       2 if ($header) {
28 1         6 $header =~ s/^.*plural\s*=\s*([^;]+);.*$/$1/;
29 1         10 $header =~ s/\[_([0-9]+)\]/%$1/g;
30 1 50       14 die "Invalid expression for plural: $header" if $header =~ /\$|n\s*\(|[A-Za-mo-z]|nn/;
31 1         4 $header =~ s/n/\$_[0]/g;
32 1         94 eval "\$self->{_plural} = sub { return $header }";
33             } else {
34 0     0   0 $self->{_plural} = sub { return $_[0] != 1 };
  0         0  
35             }
36             }
37 4         81 my $pos = $self->{_plural}($num);
38 4 50       7 $pos = $#strings if $pos > $#strings;
39 4         25 return sprintf $strings[$pos], $num;
40             }
41              
42             1;
43              
44             __END__