blib/lib/Astro/App/Satpass2/Locale.pm

filter: Statementstmt 88.1% 89 / 101 Branchbran 76.0% 35 / 46 Conditioncond 60.0% 12 / 20 MC/DCmcdc 30.7% 4 / 13 Subroutinesub 93.7% 15 / 16 Podpod n/a totaltotal 79.0% 155 / 196 CC 37 SCAR 22.0
CC
37
Cov
81%
__localize
16.8
_load
12.4
__message
4.8
CRAP 9.0
« blib/lib/Astro/App/Satpass2/Utils.pm ↑ Summary blib/lib/Astro/App/Satpass2/FormatValue.pm »
1 package Astro::App::Satpass2::Locale;
2  
3 22 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 22 ▶use Astro::App::Satpass2::Utils qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
9 expand_tilde instance
10 ARRAY_REF CODE_REF HASH_REF
11 @CARP_NOT
12 22 };
13 22 ▶use Exporter qw{ import };
Subroutine BEGIN: called CC=1 SCAR=0.0
14 22 ▶use I18N::LangTags ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 22 ▶use I18N::LangTags::Detect ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 22 our $VERSION = '0.059';
18  
19 22 our @EXPORT_OK = qw{ __localize __message __preferred };
20  
21 22 my @lang;
22 my $locale;
23  
24 {
25  
26 my %deref = (
27 51 ▶ ARRAY_REF() => sub {
Subroutine __ANON__: called CC=3 SCAR=14.2
28 51 my ( $data, $inx ) = @_;
29 51 ▶ defined $inx
Branch: 1 branch
BranchTrueFalseTotal
defined $inx and exists $data->[$inx] and return $data->[$inx] 51 0 51
Condition33%1/3
defined $inx and exists $data->[$inx]
!ll&&!rl&&r
0051
MC/DC0%0/2
defined $inx and exists $data->[$inx]
defined $inx exists $data->[$inx]
Truth table33%1/3
A and B
A= defined $inxB= exists $data->[$inx]
ABresult
0X0
100
111
30 and exists $data->[$inx]
31 and return $data->[$inx];
32 0 return;
33 },
34 1 ▶ CODE_REF() => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
35 1 my ( $code, $key, $arg ) = @_;
36 1 my $rslt = $code->( $key, $arg );
37 1 return $rslt;
38 },
39 33799 ▶ HASH_REF() => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
40 33799 my ( $data, $key ) = @_;
41 defined $key
42 and exists $data->{$key}
43 33799 ▶ and return $data->{$key};
Branch: 1 branch
BranchTrueFalseTotal
defined $key and exists $data->{$key} and return $data->{$key} 19494 14305 33799
Condition66%2/3
defined $key and exists $data->{$key}
!ll&&!rl&&r
01430519494
MC/DC50%1/2
defined $key and exists $data->{$key}
defined $key exists $data->{$key}
Truth table66%2/3
A and B
A= defined $keyB= exists $data->{$key}
ABresult
0X0
100
111
44 14305 return;
45 },
46 0 ▶ '' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
47 0 return;
48 },
49 22 );
50  
51 17881 ▶ sub __localize {
Subroutine __localize: called CC=15 SCAR=28.2
52  
53 # Keys used:
54 # {argument} = argument for code reference
55 # {default} = the default value
56 # {text} = the text to localize, as scalar or array ref. REQUIRED.
57 # {locale} = fallback locales, as hash ref or ref to array of hash refs.
58  
59 17881 my %arg = @_;
60 17881 ▶ unless ( $arg{text} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg{"text"}) 0 17881 17881
61 0 require Carp;
62 0 Carp::confess( q<Argument 'text' is required> );
63 }
64 ref $arg{text}
65 17881 ▶ or $arg{text} = [ $arg{text} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $arg{"text"} or $arg{"text"} = [$arg{"text"}] 17853 28 17881
66 17881 ▶ $arg{locale} ||= [];
Condition100%2/2
$arg{"locale"} ||= []
l!l
117880
MC/DC100%1/1
$arg{"locale"} ||= []
$arg{"locale"}
Truth table100%2/2
A
A= $arg{"locale"}
Aresult
11
00
67 HASH_REF eq ref $arg{locale}
68 17881 ▶ and $arg{locale} = [ $arg{locale} ];
Branch: 1 branch
BranchTrueFalseTotal
"HASH" eq ref $arg{"locale"} and $arg{"locale"} = [$arg{"locale"}] 1 17880 17881
69 17881 ▶ $locale ||= _load();
Condition66%2/3
$locale ||= _load()
l!l&&r!l&&!r
1787380
MC/DC0%0/2
$locale ||= _load()
$locale _load()
Truth table66%2/3
A ||= B
A= $localeB= _load()
ABresult
1X1
011
000
70  
71 17881 my @rslt;
72 17881 foreach my $lc ( @lang ) {
73 SOURCE_LOOP:
74 17893 foreach my $source ( @{ $locale }, @{ $arg{locale} } ) {
75 35784 ▶ unless ( HASH_REF eq ref $source ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("HASH" eq ref $source) 0 35784 35784
76 0 require Carp;
77 0 Carp::confess( "\$source is '$source'" );
78 }
79 35784 ▶ my $data = $source->{$lc}
Branch: 1 branch
BranchTrueFalseTotal
my $data = $source->{$lc} or next 17888 17896 35784
80 or next;
81 17888 foreach my $key ( @{ $arg{text} } ) {
82 my $code = $deref{ ref $data }
83 33851 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
my $code = $deref{ref $data} or do { require Carp; Carp::confess("Programming error - Locale systen can ", "not handle ", ref $data, " as a container") } 33851 0 33851
84 0 require Carp;
85 0 Carp::confess(
86 'Programming error - Locale systen can ',
87 'not handle ', ref $data, ' as a container'
88 );
89 };
90 ( $data ) = $code->( $data, $key, $arg{argument} )
91 33851 ▶ or next SOURCE_LOOP;
Branch: 1 branch
BranchTrueFalseTotal
($data) = &$code($data, $key, $arg{"argument"}) or next SOURCE_LOOP 19546 14305 33851
92 }
93 wantarray
94 3583 ▶ or return $data;
Branch: 1 branch
BranchTrueFalseTotal
wantarray or return $data 27 3556 3583
95 27 push @rslt, $data;
96 }
97 }
98 wantarray
99 14325 ▶ or return $arg{default};
Branch: 1 branch
BranchTrueFalseTotal
wantarray or return $arg{"default"} 26 14299 14325
100 26 return ( @rslt, $arg{default} );
101  
102 }
103  
104 }
105  
106 =begin comment
107  
108 {
109 my %stringify_ref = map { $_ => 1 } qw{ Template::Exception };
110  
111 sub __message {
112 # My OpenBSD 5.5 system seems not to stringify the arguments in
113 # the normal course of events, though my Mac OS 10.9 system
114 # does. The OpenBSD system gives instead a stringified hash
115 # reference (i.e. "HASH{0x....}").
116 my @raw_arg = @_;
117 my ( $msg, @arg ) =
118 map { $stringify_ref{ ref $_ } ? '' . $_ : $_ } @raw_arg;
119 my $lcl = __localize(
120 text => [ '+message', $msg ],
121 default => $msg,
122 );
123  
124 CODE_REF eq ref $lcl
125 and return $lcl->( $msg, @arg );
126  
127 $lcl =~ m/ \[ % /smx
128 or return join ' ', $lcl, @arg;
129  
130 grep { instance( $_, 'Template::Exception' ) } @raw_arg
131 and return join ' ', $lcl, @arg;
132  
133 my $tt = Template->new();
134  
135 my $output;
136 $tt->process( \$lcl, {
137 arg => \@arg,
138 }, \$output );
139  
140 return $output;
141 }
142 }
143  
144 =end comment
145  
146 =cut
147  
148 22 ▶sub __message {
Subroutine __message: called CC=4 SCAR=15.8
149 28 my ( $msg, @arg ) = @_;
150  
151 28 ▶ instance( $msg, 'Template::Exception' )
Branch: 1 branch
BranchTrueFalseTotal
instance($msg, "Template::Exception") and return _message_template_exception($msg, @arg) 1 27 28
152 and return _message_template_exception( $msg, @arg );
153  
154 27 my $lcl = __localize(
155 text => [ '+message', $msg ],
156 default => $msg,
157 );
158  
159 27 ▶ CODE_REF eq ref $lcl
Branch: 1 branch
BranchTrueFalseTotal
"CODE" eq ref $lcl and return &$lcl($msg, @arg) 0 27 27
160 and return $lcl->( $msg, @arg );
161  
162 27 ▶ $lcl =~ m/ \[ % /smx
Branch: 1 branch
BranchTrueFalseTotal
$lcl =~ / \[ % /msx or return join(" ", $lcl, @arg) 0 27 27
163 or return join ' ', $lcl, @arg;
164  
165 0 my $tt = Template->new();
166  
167 0 my $output;
168 0 $tt->process( \$lcl, {
169 arg => \@arg,
170 }, \$output );
171  
172 0 return $output;
173 }
174  
175 1 ▶sub _message_template_exception {
Subroutine _message_template_exception: called CC=3 SCAR=11.1
176 1 my ( $ex, @arg ) = @_;
177 1 local $_ = $ex->info();
178 # NOTE we're assuming that if there is a return we caught a
179 # re-thrown exception that had already been munged.
180 1 ▶ m/ \n \z /smx
Branch: 1 branch
BranchTrueFalseTotal
/ \n \z /msx and return $ex 0 1 1
181 and return $ex;
182 1 $_ = join ' ', $_, @arg;
183 1 ▶ m/ [.?|] \z /smx
Branch: 1 branch
BranchTrueFalseTotal
/ [.?|] \z /msx or $_ .= '.' 0 1 1
184 or $_ .= '.';
185 1 $_ .= "\n";
186 1 my $class = ref $ex;
187 1 return $class->new( $ex->type(), $_ );
188 }
189  
190 2 ▶sub __preferred {
Subroutine __preferred: called CC=3 SCAR=11.7
191 2 ▶ $locale ||= _load();
Condition33%1/3
$locale ||= _load()
l!l&&r!l&&!r
200
MC/DC0%0/2
$locale ||= _load()
$locale _load()
Truth table33%1/3
A ||= B
A= $localeB= _load()
ABresult
1X1
011
000
192 2 ▶ return wantarray ? @lang : $lang[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
193 }
194  
195 8 ▶sub _load {
Subroutine _load: called CC=12 SCAR=25.1
196  
197 # Pick up the languages from the environment
198 8 @lang = I18N::LangTags::implicate_supers(
199 I18N::LangTags::Detect::detect() );
200  
201 # Normalize the language names.
202 8 foreach ( @lang ) {
203 2 ▶ s/ ( [^_-]+ ) [_-] (.* ) /\L$1_\U$2/smx
Branch: 1 branch
BranchTrueFalseTotal
s/ ( [^_-]+ ) [_-] (.* ) /\L$1_\E\U$2\E/msx or $_ = lc $_ 1 1 2
204 or $_ = lc $_;
205 2 ▶ 'c' eq $_
Branch: 1 branch
BranchTrueFalseTotal
"c" eq $_ and $_ = uc $_ 0 2 2
206 and $_ = uc $_;
207 }
208  
209 # Append the default locale name.
210 8 ▶ grep { 'C' eq $_ } @lang
Branch: 1 branch
BranchTrueFalseTotal
grep {"C" eq $_;} @lang or push @lang, "C" 0 8 8
211 or push @lang, 'C';
212  
213 # Accumulator for locale data.
214 8 my @locales;
215  
216 # Put all the user's data in a hash.
217 8 push @locales, {};
218 8 foreach my $lc ( @lang ) {
219 10 eval { ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
220 10 ▶ defined( my $path = expand_tilde( "~~/locale/$lc.pm" ) )
Branch: 1 branch
BranchTrueFalseTotal
defined(my $path = expand_tilde("~~/locale/$lc.pm")) or return 3 0 3
221 or return;
222 3 my $data;
223 $data = do $path
224 and HASH_REF eq ref $data
225 3 ▶ and $locales[-1]{$lc} = $data;
Branch: 1 branch
BranchTrueFalseTotal
$data = do $path and "HASH" eq ref $data and $locales[-1]{$lc} = $data 1 2 3
Condition66%2/3
$data = do $path and "HASH" eq ref $data
!ll&&!rl&&r
201
MC/DC50%1/2
$data = do $path and "HASH" eq ref $data
$data = do $path "HASH" eq ref $data
Truth table66%2/3
A and B
A= $data = do $pathB= "HASH" eq ref $data
ABresult
0X0
100
111
226 };
227 }
228  
229 # Put the system-wide data in a hash.
230 8 push @locales, {};
231 8 foreach my $lc ( @lang ) {
232 10 my $mod_name = __PACKAGE__ . "::$lc";
233 10 my $data;
234 $data = eval "require $mod_name"
235 and HASH_REF eq ref $data
236 10 ▶ and $locales[-1]{$lc} = $data;
Branch: 1 branch
BranchTrueFalseTotal
$data = eval "require $mod_name" and "HASH" eq ref $data and $locales[-1]{$lc} = $data 8 2 10
Condition66%2/3
$data = eval "require $mod_name" and "HASH" eq ref $data
!ll&&!rl&&r
208
MC/DC50%1/2
$data = eval "require $mod_name" and "HASH" eq ref $data
$data = eval "require $mod_name" "HASH" eq ref $data
Truth table66%2/3
A and B
A= $data = eval "require $mod_name"B= "HASH" eq ref $data
ABresult
0X0
100
111
237 }
238  
239 # Return a reference to the names of locales.
240 8 return \@locales;
241 }
242  
243 22 1;
244  
245 __END__
« blib/lib/Astro/App/Satpass2/Utils.pm ↑ Summary blib/lib/Astro/App/Satpass2/FormatValue.pm »