File Coverage

blib/lib/Locale/TextDomain/OO/Util/ExtractHeader.pm
Criterion Covered Total %
statement 32 41 78.0
branch 3 12 25.0
condition n/a
subroutine 10 12 83.3
pod 3 3 100.0
total 48 68 70.5


line stmt bran cond sub pod time code
1             package Locale::TextDomain::OO::Util::ExtractHeader; ## no critic (TidyCode)
2            
3 2     2   44729 use strict;
  2         5  
  2         55  
4 2     2   8 use warnings;
  2         6  
  2         62  
5 2     2   10 use Carp qw(confess);
  2         5  
  2         92  
6 2     2   891 use English qw(-no_match_vars $EVAL_ERROR);
  2         4321  
  2         11  
7 2     2   723 use Locale::TextDomain::OO::Util::Constants;
  2         16  
  2         79  
8 2     2   986 use Safe;
  2         30363  
  2         165  
9 2     2   23 use namespace::autoclean;
  2         6  
  2         24  
10            
11             our $VERSION = '3.006';
12            
13             sub instance {
14 1     1 1 15 return __PACKAGE__;
15             }
16            
17             my $safe_already_done;
18             sub patch_safe_module {
19 2     2   377 no warnings qw(redefine); ## no critic (NoWarnings)
  2         9  
  2         1355  
20             # make sure we don't ever double-wrap just in case
21 0 0   0 1 0 if ( ! $safe_already_done++ ) {
22 0         0 my $orig_code = *Safe::share_from{CODE};
23             *Safe::share_from = sub {
24 0     0   0 @{ $_[2] } = grep { length } @{ $_[2] }; # remove the offending zero-length $vars
  0         0  
  0         0  
  0         0  
25 0         0 $orig_code->(@_);
26 0         0 };
27             }
28            
29 0         0 return __PACKAGE__;
30             }
31            
32             my $perlify_plural_forms_ref__code_ref = sub {
33             my $plural_forms_ref = shift;
34            
35             ${$plural_forms_ref} =~ s{ \b ( nplurals | plural | n ) \b }{\$$1}xmsg;
36            
37             return;
38             };
39            
40             my $nplurals__code_ref = sub {
41             my $plural_forms = shift;
42            
43             $perlify_plural_forms_ref__code_ref->(\$plural_forms);
44             my $code = <<"EOC";
45             my \$n = 0;
46             my (\$nplurals, \$plural);
47             $plural_forms;
48             \$nplurals;
49             EOC
50             my $nplurals = Safe->new->reval($code)
51             or confess "Code of Plural-Forms $plural_forms is not safe, $EVAL_ERROR";
52            
53             return $nplurals;
54             };
55            
56             my $plural__code_ref = sub {
57             my $plural_forms = shift;
58            
59             return $plural_forms =~ m{ \b plural= ( [^;\n]+ ) }xms;
60             };
61            
62             my $plural_code__code_ref = sub {
63             my $plural_forms = shift;
64            
65             $perlify_plural_forms_ref__code_ref->(\$plural_forms);
66             my $code = <<"EOC";
67             sub {
68             my \$n = shift;
69            
70             my (\$nplurals, \$plural);
71             $plural_forms;
72            
73             return 0 + \$plural;
74             }
75             EOC
76             my $code_ref = Safe->new->reval($code)
77             or confess "Code $plural_forms is not safe, $EVAL_ERROR";
78            
79             return $code_ref;
80             };
81            
82             sub extract_header_msgstr {
83 1     1 1 10 my ( undef, $header_msgstr ) = @_;
84            
85 1 50       6 defined $header_msgstr
86             or confess 'Header is not defined';
87             ## no critic (ComplexRegexes)
88 1 50       18 my ( $plural_forms ) = $header_msgstr =~ m{
89             ^
90             Plural-Forms:
91             [ ]*
92             (
93             nplurals [ ]* [=] [ ]* \d+ [ ]* [;]
94             [ ]*
95             plural [ ]* [=] [ ]* [^;\n]+ [ ]* [;]?
96             [ ]*
97             )
98             $
99             }xms
100             or confess 'Plural-Forms not found in header';
101             ## use critic (ComplexRegexes)
102 1 50       15 my ( $charset ) = $header_msgstr =~ m{
103             ^
104             Content-Type:
105             [^;]+ [;] [ ]*
106             charset [ ]* = [ ]*
107             ( [^ ]+ )
108             [ ]*
109             $
110             }xms
111             or confess 'Content-Type with charset not found in header';
112 1         8 my ( $lexicon_class ) = $header_msgstr =~ m{
113             ^ X-Lexicon-Class: \s* ( \S* ) \s* $
114             }xms;
115             # ToDo: remove because multiplural was a too complicated idea
116 1         4 my ( $multiplural_nplurals ) = $header_msgstr =~ m{
117             ^ X-Multiplural-Nplurals: [ ]* ( \d+ ) [ ]* $
118             }xms;
119            
120             return {(
121 1 0       6 nplurals => $nplurals__code_ref->($plural_forms),
    0          
122             plural => $plural__code_ref->($plural_forms),
123             plural_code => $plural_code__code_ref->($plural_forms),
124             charset => $charset,
125             ! $lexicon_class ? () : (
126             lexicon_class => $lexicon_class,
127             ),
128             # ToDo: remove because multiplural was a too complicated idea
129             ! $multiplural_nplurals ? () : (
130             multiplural_nplurals => $multiplural_nplurals,
131             ),
132             )};
133             }
134            
135             1;
136            
137             __END__