lib/Text/PO.pm

filter: Statementstmt 65.5% 572 / 873 Branchbran 42.8% 204 / 476 Conditioncond 33.6% 98 / 291 MC/DCmcdc 8.2% 12 / 146 Subroutinesub 78.1% 68 / 87 Podpod 100% 51 / 51 totaltotal 52.2% 1005 / 1924 CC 375 SCAR 50.2
CC
375
Cov
53%
sync
650.0
add_element
600.0
add_include
552.0
CRAP 151.5
« blib/script/po.pl ↑ Summary lib/Text/PO/MO.pm »
1 ##----------------------------------------------------------------------------
2 ## PO Files Manipulation - ~/lib/Text/PO.pm
3 ## Version v1.0.3
4 ## Copyright(c) 2026 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2018/06/21
7 ## Modified 2026/11/17
8 ## All rights reserved
9 ## This program is free software; you can redistribute it and/or modify it
10 ## under the same terms as Perl itself.
11 ##----------------------------------------------------------------------------
12 package Text::PO;
13 BEGIN
14 6 ▶{
Subroutine BEGIN: called CC=3 SCAR=11.1
15 6 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶ use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 6 ▶ use vars qw( $VERSION @META $DEF_META $JSON_CLASS );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 ▶ use open ':std' => ':utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 6 ▶ use Class::Struct;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 6 ▶ use DateTime::Format::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 6 ▶ use DateTime::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 6 ▶ use DateTime::Lite::TimeZone;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 6 ▶ use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 6 ▶ use Fcntl qw( :DEFAULT );
Subroutine BEGIN: called CC=1 SCAR=0.0
27 6 ▶ use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 6 ▶ use Text::PO::Element;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 6 ▶ use constant HAS_LOCAL_TZ => ( DateTime::Lite::TimeZone->new( name => 'local' ) ? 1 : 0 );
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
'DateTime::Lite::TimeZone'->new('name', 'local') ? : 0 6 6
30 # JSON backend detection: prefer Cpanel::JSON::XS (fastest, most rigorous), fall back
31 # to JSON::XS, then JSON::PP (core since Perl 5.14).
32 6 our $JSON_CLASS;
33 6 ▶ if( eval{ require Cpanel::JSON::XS; 1 } )
Branch: 2 branches
BranchTrueFalseTotal
if (eval { do { require Cpanel::JSON::XS; 1 } }) { } 6 0 6
elsif (eval { do { require JSON::XS; 1 } }) { } 0 0 0
34 {
35 6 $JSON_CLASS = 'Cpanel::JSON::XS';
36 }
37 0 elsif( eval{ require JSON::XS; 1 } )
38 {
39 0 $JSON_CLASS = 'JSON::XS';
40 }
41 else
42 {
43 0 require JSON::PP;
44 0 $JSON_CLASS = 'JSON::PP';
45 }
46 6 our $VERSION = 'v1.0.3';
47 };
48  
49 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 6 struct 'Text::PO::Comment' =>
53 {
54 'text' => '@',
55 };
56 6 our @META = qw(
57 Project-Id-Version
58 Report-Msgid-Bugs-To
59 POT-Creation-Date
60 PO-Revision-Date
61 Last-Translator
62 Language-Team
63 Language
64 Plural-Forms
65 MIME-Version
66 Content-Type
67 Content-Transfer-Encoding
68 );
69 6 our $DEF_META =
70 {
71 'Project-Id-Version' => 'Project 0.1',
72 'Report-Msgid-Bugs-To' => 'bugs@example.com',
73 # 2011-07-02 20:53+0900
74 'POT-Creation-Date' => DateTime::Lite->from_epoch( 'epoch' => time(), 'time_zone' => ( HAS_LOCAL_TZ ? 'local' : 'UTC' ) )->strftime( '%Y-%m-%d %H:%M%z' ),
75 'PO-Revision-Date' => DateTime::Lite->from_epoch( 'epoch' => time(), 'time_zone' => ( HAS_LOCAL_TZ ? 'local' : 'UTC' ) )->strftime( '%Y-%m-%d %H:%M%z' ),
76 'Last-Translator' => 'Unknown <hello@example.com>',
77 'Language-Team' => 'Unknown <hello@example.com>',
78 'Language' => '',
79 'Plural-Forms' => 'nplurals=1; plural=0;',
80 'MIME-Version' => '1.0',
81 'Content-Type' => 'text/plain; charset=utf-8',
82 'Content-Transfer-Encoding' => '8bit',
83 };
84  
85 sub init
86 23 ▶{
Subroutine init: called CC=1 SCAR=0.0
Pod documented
87 23 my $self = shift( @_ );
88 23 $self->{domain} = undef;
89 23 $self->{header} = [];
90 # utf8
91 23 $self->{encoding} = undef;
92 # Should we allow inclusion ?
93 23 $self->{include} = 1;
94 # Maximum recursion allowed for the include option
95 23 $self->{max_recurse} = 32;
96 23 $self->{meta} = {};
97 23 $self->{meta_keys} = [];
98 # Default to using po json file if it exists
99 23 $self->{use_json} = 1;
100 23 $self->{remove_duplicates} = 1;
101 23 $self->{_init_strict_use_sub} = 1;
102 23 $self->SUPER::init( @_ );
103 23 $self->{elements} = [];
104 23 $self->{added} = [];
105 23 $self->{removed} = [];
106 23 $self->{source} = {};
107 23 $self->{_parsed} = 0;
108 23 return( $self );
109 }
110  
111 sub add_element
112 0 ▶{
Subroutine add_element: not called CC=24 SCAR=64.0
Pod documented
113 0 my $self = shift( @_ );
114 0 my( $msgid, $e, $opts );
115 0 ▶ if( $self->_is_a( $_[0] => 'Text::PO::Element' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_a($_[0], 'Text::PO::Element')) { } 0 0 0
116 {
117 0 $e = shift( @_ );
118 0 ▶ $msgid = $e->msgid_as_text || return( $self->error( "No msgid was provided" ) );
Condition0%0/2
$e->msgid_as_text || (return $self->error('No msgid was provided'))
l!l
00
MC/DC0%0/1
$e->msgid_as_text || (return $self->error('No msgid was provided'))
$e->msgid_as_text
Truth table0%0/2
A
A= $e->msgid_as_text
Aresult
11
00
119 0 $opts = $self->_get_args_as_hash( @_ );
120 }
121 else
122 {
123 0 $opts = $self->_get_args_as_hash( @_ );
124 0 ▶ $msgid = $opts->{msgid} || return( $self->error( "No msgid was provided" ) );
Condition0%0/2
$opts->{'msgid'} || (return $self->error('No msgid was provided'))
l!l
00
MC/DC0%0/1
$opts->{'msgid'} || (return $self->error('No msgid was provided'))
$opts->{'msgid'}
Truth table0%0/2
A
A= $opts->{'msgid'}
Aresult
11
00
125 0 ▶ $e = $self->new_element( %$opts ) || return( $self->pass_error );
Condition0%0/2
$self->new_element(%$opts) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->new_element(%$opts) || (return $self->pass_error)
$self->new_element(%$opts)
Truth table0%0/2
A
A= $self->new_element(%$opts)
Aresult
11
00
126 }
127 0 ▶ return( $self->error( "No msgid was provided." ) ) if( !length( $msgid ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $msgid 0 0 0
128 0 my $elems = $self->elements;
129 0 foreach my $e2 ( @$elems )
130 {
131 0 ▶ next if( $e2->is_meta || $e2->is_include );
Branch: 1 branch
BranchTrueFalseTotal
if $e2->is_meta or $e2->is_include 0 0 0
Condition0%0/3
$e2->is_meta or $e2->is_include
l!l&&r!l&&!r
000
MC/DC0%0/2
$e2->is_meta or $e2->is_include
$e2->is_meta $e2->is_include
Truth table0%0/3
A or B
A= $e2->is_metaB= $e2->is_include
ABresult
1X1
011
000
132 0 ▶ if( $e2->msgid_as_text eq $msgid )
Branch: 1 branch
BranchTrueFalseTotal
if ($e2->msgid_as_text eq $msgid) 0 0 0
133 {
134 # return( $self->error( "There already is an id '$msgid' in the po file" ) );
135 0 return( $e2 );
136 }
137 }
138 0 $e->po( $self );
139 0 ▶ my $id = ( $opts->{before} || $opts->{after} );
Condition0%0/3
$opts->{'before'} || $opts->{'after'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{'before'} || $opts->{'after'}
$opts->{'before'} $opts->{'after'}
Truth table0%0/3
A || B
A= $opts->{'before'}B= $opts->{'after'}
ABresult
1X1
011
000
140 0 ▶ if( $id )
Branch: 1 branch
BranchTrueFalseTotal
if ($id) { } 0 0 0
141 {
142 0 my $found = 0;
143 0 ▶ for( my $i = 0; $i < scalar( @$elems ); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$elems) 0 0 0
144 {
145 0 my $elem = $elems->[$i];
146 0 ▶ next if( $elem->is_meta );
Branch: 1 branch
BranchTrueFalseTotal
if $elem->is_meta 0 0 0
147 0 ▶ if( ( $elem->is_include && ( $elem->file // '' ) eq $id ) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->is_include and ($elem->file // "") eq $id or not $elem->is_include and ($elem->id // "") eq $id) 0 0 0
Condition0%0/13
$elem->is_include and ($elem->file // "") eq $id or not $elem->is_include and ($elem->id // "") eq $id
l!l&&r!l&&!r
000
$elem->is_include and ($elem->file // "") eq $id
!ll&&!rl&&r
000
$elem->file // ""
l!l
00
not $elem->is_include and ($elem->id // "") eq $id
!ll&&!rl&&r
000
$elem->id // ""
l!l
00
MC/DC0%0/4
$elem->is_include and ($elem->file // "") eq $id or not $elem->is_include and ($elem->id // "") eq $id
$elem->is_include $elem->file not $elem->is_include $elem->id
Truth table0%0/7
A and B or not(C) and D
A= $elem->is_includeB= $elem->fileC= not $elem->is_includeD= $elem->id
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
148 ( !$elem->is_include && ( $elem->id // '' ) eq $id ) )
149 {
150 0 ▶ if( $opts->{after} )
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'after'}) { } 0 0 0
elsif ($opts->{'before'}) { } 0 0 0
151 {
152 0 splice( @$elems, $i + 1, 0, $e );
153 }
154 elsif( $opts->{before} )
155 {
156 0 splice( @$elems, $i, 0, $e );
157 }
158 0 $found++;
159 0 last;
160 }
161 }
162 0 ▶ if( !$found )
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 0 0 0
163 {
164 0 ▶ return( $self->error( "No msgid/include '$id', to add ", ( $opts->{before} ? 'before' : 'after' ), ", was found, and thus the msgid '${msgid}' could not be added." ) );
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'before'} ? : 0 0 0
165 }
166 }
167 else
168 {
169 0 push( @{$self->{elements}}, $e );
170 }
171 0 return( $e );
172 }
173  
174 sub add_include
175 0 ▶{
Subroutine add_include: not called CC=23 SCAR=63.1
Pod documented
176 0 my $self = shift( @_ );
177 0 my $e;
178 my $opts;
179 0 ▶ if( $self->_is_a( $_[0] => 'Text::PO::Element' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_a($_[0], 'Text::PO::Element')) { } 0 0 0
180 {
181 0 $e = shift( @_ );
182 0 ▶ if( !$e->file )
Branch: 1 branch
BranchTrueFalseTotal
unless ($e->file) 0 0 0
183 {
184 0 return( $self->error( "The Text::PO::Element object provided does not have any 'file' value set." ) );
185 }
186 0 $opts = $self->_get_args_as_hash( @_ );
187 }
188 else
189 {
190 0 $opts = $self->_get_args_as_hash( @_ );
191 0 ▶ if( !$opts->{file} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts->{"file"}) 0 0 0
192 {
193 0 return( $self->error( "No 'file' property found in the hash of options provided." ) );
194 }
195 0 ▶ $e = $self->new_element( %$opts ) || return( $self->pass_error );
Condition0%0/2
$self->new_element(%$opts) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->new_element(%$opts) || (return $self->pass_error)
$self->new_element(%$opts)
Truth table0%0/2
A
A= $self->new_element(%$opts)
Aresult
11
00
196 }
197 0 $e->is_include(1);
198 0 my $file = $e->file;
199 0 my $elems = $self->elements;
200 0 foreach my $elem ( @$elems )
201 {
202 0 ▶ next unless( $elem->is_include );
Branch: 1 branch
BranchTrueFalseTotal
unless $elem->is_include 0 0 0
203 0 ▶ if( ( $elem->file // '' ) eq $file )
Branch: 1 branch
BranchTrueFalseTotal
if (($elem->file // "") eq $file) 0 0 0
Condition0%0/2
$elem->file // ""
l!l
00
MC/DC0%0/1
$elem->file // ""
$elem->file
Truth table0%0/2
A
A= $elem->file
Aresult
11
00
204 {
205 0 return( $elem );
206 }
207 }
208  
209 0 $e->po( $self );
210 0 ▶ my $id = ( $opts->{before} || $opts->{after} );
Condition0%0/3
$opts->{'before'} || $opts->{'after'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{'before'} || $opts->{'after'}
$opts->{'before'} $opts->{'after'}
Truth table0%0/3
A || B
A= $opts->{'before'}B= $opts->{'after'}
ABresult
1X1
011
000
211 0 ▶ if( $id )
Branch: 1 branch
BranchTrueFalseTotal
if ($id) { } 0 0 0
212 {
213 0 my $found = 0;
214 0 ▶ for( my $i = 0; $i < scalar( @$elems ); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$elems) 0 0 0
215 {
216 0 my $elem = $elems->[$i];
217 0 ▶ next if( $elem->is_meta );
Branch: 1 branch
BranchTrueFalseTotal
if $elem->is_meta 0 0 0
218 0 ▶ if( ( $elem->is_include && ( $elem->file // '' ) eq $id ) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->is_include and ($elem->file // "") eq $id or not $elem->is_include and ($elem->id // "") eq $id) 0 0 0
Condition0%0/13
$elem->is_include and ($elem->file // "") eq $id or not $elem->is_include and ($elem->id // "") eq $id
l!l&&r!l&&!r
000
$elem->is_include and ($elem->file // "") eq $id
!ll&&!rl&&r
000
$elem->file // ""
l!l
00
not $elem->is_include and ($elem->id // "") eq $id
!ll&&!rl&&r
000
$elem->id // ""
l!l
00
MC/DC0%0/4
$elem->is_include and ($elem->file // "") eq $id or not $elem->is_include and ($elem->id // "") eq $id
$elem->is_include $elem->file not $elem->is_include $elem->id
Truth table0%0/7
A and B or not(C) and D
A= $elem->is_includeB= $elem->fileC= not $elem->is_includeD= $elem->id
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
219 ( !$elem->is_include && ( $elem->id // '' ) eq $id ) )
220 {
221 0 ▶ if( $opts->{after} )
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'after'}) { } 0 0 0
elsif ($opts->{'before'}) { } 0 0 0
222 {
223 0 splice( @$elems, $i + 1, 0, $e );
224 }
225 elsif( $opts->{before} )
226 {
227 0 splice( @$elems, $i, 0, $e );
228 }
229 0 $found++;
230 0 last;
231 }
232 }
233 0 ▶ if( !$found )
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 0 0 0
234 {
235 0 ▶ return( $self->error( "No msgid/include '$id', to add ", ( $opts->{before} ? 'before' : 'after' ), ", was found, and thus the include '${file}' could not be added." ) );
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'before'} ? : 0 0 0
236 }
237 }
238 else
239 {
240 0 push( @{$self->{elements}}, $e );
241 }
242 0 return( $e );
243 }
244  
245 0 ▶sub added { return( shift->_set_get_array_as_object( 'added', @_ ) ); }
Subroutine added: not called CC=1 SCAR=6.9
Pod documented
246  
247 1 ▶sub as_hash { return( shift->hash( @_ ) ); }
Subroutine as_hash: called CC=1 SCAR=0.0
Pod documented
248  
249 sub as_json
250 1 ▶{
Subroutine as_json: called CC=20 SCAR=30.6
Pod documented
251 1 my $self = shift( @_ );
252 1 my $opts = $self->_get_args_as_hash( @_ );
253 1 my $metaKeys = $self->{meta_keys};
254 1 my $hash = {};
255 1 $hash->{domain} = $self->domain;
256 1 $hash->{meta} = {};
257 1 $hash->{meta_keys} = [];
258 1 $hash->{elements} = [];
259 1 foreach my $k ( @$metaKeys )
260 {
261 11 my $key = $self->normalise_meta( $k );
262 11 my $val = $self->meta( $k );
263 11 $hash->{meta}->{ $key } = $val;
264 11 push( @{$hash->{meta_keys}}, $key );
265 }
266 1 my $elem = $self->elements;
267 1 foreach my $e ( @$elem )
268 {
269 8 my $msgid = $e->msgid_as_text;
270 8 my $msgstr = $e->msgstr;
271 8 ▶ next if( $e->is_meta || !CORE::length( $msgid // '' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $e->is_meta or not length($msgid // "") 0 8 8
Condition40%2/5
$e->is_meta or not length($msgid // "")
l!l&&r!l&&!r
008
$msgid // ""
l!l
80
MC/DC0%0/2
$e->is_meta or not length($msgid // "")
$e->is_meta $msgid
Truth table33%1/3
A or not(B)
A= $e->is_metaB= $msgid
ABresult
1X1
001
010
272 8 my $k = $msgid;
273 # my $v = ref( $msgstr ) ? join( '', @$msgstr ) : $msgstr;
274 8 my $v;
275 8 ▶ if( $e->plural )
Branch: 1 branch
BranchTrueFalseTotal
if ($e->plural) { } 1 7 8
276 {
277 1 my $res = [];
278 1 ▶ for( my $i = 0; $i < scalar( @$msgstr ); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$msgstr) 2 1 3
279 {
280 2 ▶ push( @$res, ref( $msgstr->[$i] ) ? join( '', @{$msgstr->[$i]} ) : $msgstr->[$i] );
Branch: 1 branch
BranchTrueFalseTotal
ref $msgstr->[$i] ? : 2 0 2
281 }
282 1 $v = $res;
283 }
284 else
285 {
286 7 ▶ $v = ref( $msgstr ) ? join( '', @$msgstr ) : $msgstr;
Branch: 1 branch
BranchTrueFalseTotal
ref $msgstr ? : 1 6 7
287 }
288  
289 8 my $ref =
290 {
291 msgid => $k,
292 msgstr => $v,
293 };
294 8 ▶ $ref->{msgid_plural} = $e->msgid_plural if( $e->plural && $e->msgid_plural );
Branch: 1 branch
BranchTrueFalseTotal
if $e->plural and $e->msgid_plural 1 7 8
Condition66%2/3
$e->plural and $e->msgid_plural
!ll&&!rl&&r
701
MC/DC50%1/2
$e->plural and $e->msgid_plural
$e->plural $e->msgid_plural
Truth table66%2/3
A and B
A= $e->pluralB= $e->msgid_plural
ABresult
0X0
100
111
295 8 ▶ if( !scalar( @{$ref->{comment} = $e->comment} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @{$ref->{"comment"} = $e->comment;}) 8 0 8
296 {
297 8 delete( $ref->{comment} );
298 }
299 8 ▶ if( !length( $ref->{context} = $e->context ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (length($ref->{"context"} = $e->context)) 8 0 8
300 {
301 8 delete( $ref->{context} );
302 }
303 8 ▶ if( !scalar( @{$ref->{flags} = $e->flags} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @{$ref->{"flags"} = $e->flags;}) 8 0 8
304 {
305 8 delete( $ref->{flags} );
306 }
307 8 ▶ if( !length( $ref->{reference} = $e->reference ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (length($ref->{"reference"} = $e->reference)) 5 3 8
308 {
309 5 delete( $ref->{reference} );
310 }
311 8 push( @{$hash->{elements}}, $ref );
312 }
313 1 my $j = $JSON_CLASS->new->relaxed->allow_blessed->convert_blessed;
314 # canonical = sorting hash keys
315 1 foreach my $t ( qw( pretty utf8 indent canonical ) )
316 {
317 4 ▶ $j->$t( $opts->{ $t } ) if( exists( $opts->{ $t } ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{$t} 0 4 4
318 }
319 1 ▶ $j->canonical( $opts->{sort} ) if( exists( $opts->{sort} ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"sort"} 0 1 1
320 # try-catch
321 1 local $@;
322 my $json = eval
323 1 {
324 1 $j->encode( $hash );
325 };
326 1 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
327 {
328 0 return( $self->error( "Unable to json encode the hash data created: $@" ) );
329 }
330 1 return( $json );
331 }
332  
333 sub as_string
334 0 ▶{
Subroutine as_string: not called CC=2 SCAR=17.9
Pod documented
335 0 my $self = shift( @_ );
336 0 my $s = $self->new_scalar( '' );
337 0 ▶ my $io = $s->open || return( $self->pass_error( $s->error ) );
Condition0%0/2
$s->open || (return $self->pass_error($s->error))
l!l
00
MC/DC0%0/1
$s->open || (return $self->pass_error($s->error))
$s->open
Truth table0%0/2
A
A= $s->open
Aresult
11
00
338 0 $self->dump( $io );
339 0 return( "$s" );
340 }
341  
342 sub charset
343 1 ▶{
Subroutine charset: called CC=2 SCAR=8.1
Pod documented
344 1 my $self = shift( @_ );
345 1 my $type = $self->content_type();
346 1 my $def = $self->parse_header_value( $type );
347 1 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 1 1
348 {
349 0 my $v = shift( @_ );
350 0 $def->params->{charset} = $v;
351 0 $self->meta( content_type => $def->as_string );
352 }
353 1 return( $def->params->{charset} );
354 }
355  
356 1 ▶sub content_encoding { return( shift->_set_get_meta_value( 'Content-Transfer-Encoding', @_ ) ); }
Subroutine content_encoding: called CC=1 SCAR=0.0
Pod documented
357  
358 2 ▶sub content_type { return( shift->_set_get_meta_value( 'Content-Type', @_ ) ); }
Subroutine content_type: called CC=1 SCAR=0.0
Pod documented
359  
360 # <https://superuser.com/questions/392439/lang-and-language-environment-variable-in-debian-based-systems>
361 sub current_lang
362 1 ▶{
Subroutine current_lang: called CC=6 SCAR=30.5
Pod documented
363 1 my $self = shift( @_ );
364 1 ▶ return( '' ) if( !CORE::exists( $ENV{LANGUAGE} ) && !CORE::exists( $ENV{LANG} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ENV{"LANGUAGE"} or exists $ENV{"LANG"} 1 0 1
Condition33%1/3
exists $ENV{"LANGUAGE"} or exists $ENV{"LANG"}
l!l&&r!l&&!r
001
MC/DC0%0/2
exists $ENV{"LANGUAGE"} or exists $ENV{"LANG"}
exists $ENV{"LANGUAGE"} exists $ENV{"LANG"}
Truth table33%1/3
A or B
A= exists $ENV{"LANGUAGE"}B= exists $ENV{"LANG"}
ABresult
1X1
011
000
365 0 ▶ return( ( $ENV{LANGUAGE} || $ENV{LANG} ) ? [split( /:/, ( $ENV{LANGUAGE} || $ENV{LANG} ) )]->[0] : '' );
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'LANGUAGE'} || $ENV{'LANG'} ? : 0 0 0
Condition0%0/6
$ENV{"LANGUAGE"} or $ENV{"LANG"}
l!l&&r!l&&!r
000
$ENV{'LANGUAGE'} || $ENV{'LANG'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{"LANGUAGE"} or $ENV{"LANG"}
$ENV{"LANGUAGE"} $ENV{"LANG"}
MC/DC0%0/2
$ENV{'LANGUAGE'} || $ENV{'LANG'}
$ENV{'LANGUAGE'} $ENV{'LANG'}
Truth table0%0/3
A or B
A= $ENV{"LANGUAGE"}B= $ENV{"LANG"}
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $ENV{'LANGUAGE'}B= $ENV{'LANG'}
ABresult
1X1
011
000
366 }
367  
368 sub decode
369 0 ▶{
Subroutine decode: not called CC=5 SCAR=34.0
Pod documented
370 0 my $self = shift( @_ );
371 0 my $str = shift( @_ );
372 0 ▶ return( '' ) if( !length( $str ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $str 0 0 0
373 0 my $enc = $self->encoding;
374 0 ▶ return( $str ) if( !$enc );
Branch: 1 branch
BranchTrueFalseTotal
unless $enc 0 0 0
375 # try-catch
376 0 local $@;
377 my $rv = eval
378 0 {
379 0 ▶ return( Encode::decode_utf8( $str, Encode::FB_CROAK ) ) if( $enc eq 'utf8' );
Branch: 1 branch
BranchTrueFalseTotal
if $enc eq "utf8" 0 0 0
380 0 return( Encode::decode( $enc, $str, Encode::FB_CROAK ) );
381 };
382 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
383 {
384 0 return( $self->error( "An error occurred while trying to decode a string using encoding '$enc': $@" ) );
385 }
386 0 return( $rv );
387 }
388  
389 12 ▶sub domain { return( shift->_set_get_scalar( 'domain', @_ ) ); }
Subroutine domain: called CC=1 SCAR=0.0
Pod documented
390  
391 sub dump
392 1 ▶{
Subroutine dump: called CC=33 SCAR=45.2
Pod documented
393 1 my $self = shift( @_ );
394 1 require IO::File;
395 1 my $fh = IO::File->new;
396 1 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1 0 1
397 {
398 1 $fh = shift( @_ );
399 1 ▶ return( $self->error( "Filehandle provided '$fh' (", ref( $fh ), ") does not look like a filehandle" ) ) if( !Scalar::Util::openhandle( $fh ) );
Branch: 1 branch
BranchTrueFalseTotal
unless &Scalar::Util::openhandle($fh) 0 1 1
400 # $fh->fdopen( fileno( $fh ), 'w' );
401 }
402 else
403 {
404 0 $fh->fdopen( fileno( STDOUT ), 'w' );
405 }
406 1 ▶ my $enc = $self->encoding || 'utf8';
Condition50%1/2
$self->encoding || 'utf8'
l!l
10
MC/DC0%0/1
$self->encoding || 'utf8'
$self->encoding
Truth table50%1/2
A
A= $self->encoding
Aresult
11
00
407 1 ▶ $enc = 'utf8' if( lc( $enc ) eq 'utf-8' );
Branch: 1 branch
BranchTrueFalseTotal
if lc $enc eq "utf-8" 0 1 1
408 1 ▶ $fh->binmode( ":${enc}" ) || return( $self->error( "Unable to set binmode on character encoding '$enc': $!" ) );
Branch: 1 branch
BranchTrueFalseTotal
$fh->binmode(":$enc") or return $self->error("Unable to set binmode on character encoding '${enc}': $!") 1 0 1
409 1 $fh->autoflush(1);
410  
411 # If this is a brain new instance whose data do not originate from parsing a file, and we do not have yet meta data, we set some default now
412 0 ▶ if( !$self->{_parsed} && !scalar( keys( %{$self->{meta}} ) ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_parsed"} or scalar keys %{$$self{"meta"};}) 0 1 1
Condition33%1/3
$self->{"_parsed"} or scalar keys %{$$self{"meta"};}
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->{"_parsed"} or scalar keys %{$$self{"meta"};}
$self->{"_parsed"} scalar keys %{$$self{"meta"};}
Truth table33%1/3
A or B
A= $self->{"_parsed"}B= scalar keys %{$$self{"meta"};}
ABresult
1X1
011
000
413 {
414 0 $self->set_default_meta;
415 }
416  
417 1 my $elem = $self->{elements};
418 1 ▶ if( my $header = $self->header )
Branch: 1 branch
BranchTrueFalseTotal
if (my $header = $self->header) 1 0 1
419 {
420 1 ▶ $fh->print( join( "\n", @$header ) ) || return( $self->error( "Unable to print po data to file handle: $!" ) );
Branch: 1 branch
BranchTrueFalseTotal
$fh->print(join("\n", @$header)) or return $self->error("Unable to print po data to file handle: $!") 1 0 1
421 }
422 1 my $domain = '';
423 1 ▶ $domain = $self->domain if( $self->domain );
Branch: 1 branch
BranchTrueFalseTotal
if $self->domain 1 0 1
424 1 ▶ if( length( $domain ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $domain) 1 0 1
425 {
426 1 ▶ $fh->print( "\n#\n# domain \"${domain}\"" ) || return( $self->error( "Unable to print po data to file handle: $!" ) );
Branch: 1 branch
BranchTrueFalseTotal
$fh->print(qq[\n#\n# domain "$domain"]) or return $self->error("Unable to print po data to file handle: $!") 1 0 1
427 }
428 1 ▶ $fh->print( "\n\n" ) || return( $self->error( "Unable to print po data to file handle: $!" ) );
Branch: 1 branch
BranchTrueFalseTotal
$fh->print("\n\n") or return $self->error("Unable to print po data to file handle: $!") 1 0 1
429 # my $metaKeys = $self->meta_keys;
430 1 my $metaKeys = [@META];
431 1 ▶ if( scalar( @$metaKeys ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$metaKeys) 1 0 1
432 {
433 1 ▶ $fh->printf( "msgid \"\"\n" ) || return( $self->error( "Unable to print po data to file handle: $!" ) );
Branch: 1 branch
BranchTrueFalseTotal
$fh->printf("msgid \"\"\n") or return $self->error("Unable to print po data to file handle: $!") 1 0 1
434 1 ▶ $fh->printf( "msgstr \"\"\n" ) || return( $self->error( "Unable to print po data to file handle: $!" ) );
Branch: 1 branch
BranchTrueFalseTotal
$fh->printf("msgstr \"\"\n") or return $self->error("Unable to print po data to file handle: $!") 1 0 1
435 1 foreach my $k ( @$metaKeys )
436 {
437 11 my $k2 = lc( $k );
438 11 $k2 =~ tr/-/_/;
439 # No, we do not do this anymore. See set_default_meta()
440 # if( !exists( $self->{meta}->{ $k2 } ) &&
441 # length( $DEF_META->{ $k } ) )
442 # {
443 # $self->{meta}->{ $k2 } = $DEF_META->{ $k };
444 # }
445 11 ▶ next if( !exists( $self->{meta}->{ $k2 } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"meta"}{$k2} 0 11 11
446 11 ▶ $fh->printf( "\"%s: %s\\n\"\n", $self->normalise_meta( $k ), $self->meta( $k ) ) || return( $self->error( "Unable to print po data to file handle: $!" ) );
Branch: 1 branch
BranchTrueFalseTotal
$fh->printf("\"%s: %s\\n\"\n", $self->normalise_meta($k), $self->meta($k)) or return $self->error("Unable to print po data to file handle: $!") 11 0 11
447 }
448 1 ▶ $fh->print( "\n" ) || return( $self->error( "Unable to print po data to file handle: $!" ) );
Branch: 1 branch
BranchTrueFalseTotal
$fh->print("\n") or return $self->error("Unable to print po data to file handle: $!") 1 0 1
449 }
450 1 foreach my $e ( @$elem )
451 {
452 8 my $msgid = $e->msgid;
453 8 ▶ if( $e->is_include )
Branch: 1 branch
BranchTrueFalseTotal
if ($e->is_include) { } 0 8 8
454 {
455 0 ▶ $fh->print( $e->dump, "\n" ) || return( $self->error( "Unable to print po data to file handle: $!" ) );
Branch: 1 branch
BranchTrueFalseTotal
$fh->print($e->dump, "\n") or return $self->error("Unable to print po data to file handle: $!") 0 0 0
456 }
457 else
458 {
459 8 ▶ next if( $e->is_meta || !CORE::length( $msgid ) || ( ref( $msgid // '' ) eq 'ARRAY' && !scalar( @$msgid ) ) );
Branch: 1 branch
BranchTrueFalseTotal
if $e->is_meta or not length $msgid or ref($msgid // "") eq "ARRAY" and not scalar @$msgid 0 8 8
Condition45%5/11
$e->is_meta or not length $msgid or ref($msgid // "") eq "ARRAY" and not scalar @$msgid
l!l&&r!l&&!r
008
$e->is_meta or not length $msgid
l!l&&r!l&&!r
008
ref($msgid // "") eq "ARRAY" and not scalar @$msgid
!ll&&!rl&&r
710
$msgid // ""
l!l
80
MC/DC0%0/4
$e->is_meta or not length $msgid or ref($msgid // "") eq "ARRAY" and not scalar @$msgid
$e->is_meta not length $msgid $msgid not scalar @$msgid
Truth table20%1/5
A or not(B) or C and not(D)
A= $e->is_metaB= not length $msgidC= $msgidD= not scalar @$msgid
ABCDresult
1XXX1
01XX1
00111
000X0
00100
460 8 ▶ if( $e->po ne $self )
Branch: 1 branch
BranchTrueFalseTotal
if ($e->po ne $self) 0 8 8
461 {
462 0 ▶ warn( "This element '", $e->msgid_as_text, "' does not belong to us. Its po object is different than our current object.\n" ) if( $self->_is_warnings_enabled );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_warnings_enabled 0 0 0
463 }
464 8 ▶ $fh->print( $e->dump, "\n" ) || return( $self->error( "Unable to print po data to file handle: $!" ) );
Branch: 1 branch
BranchTrueFalseTotal
$fh->print($e->dump, "\n") or return $self->error("Unable to print po data to file handle: $!") 8 0 8
465 }
466 8 ▶ $fh->print( "\n" ) || return( $self->error( "Unable to print po data to file handle: $!" ) );
Branch: 1 branch
BranchTrueFalseTotal
$fh->print("\n") or return $self->error("Unable to print po data to file handle: $!") 8 0 8
467 }
468 1 return( $self );
469 }
470  
471 27 ▶sub elements { return( shift->_set_get_array_as_object( 'elements', @_ ) ); }
Subroutine elements: called CC=1 SCAR=0.0
Pod documented
472  
473 114 ▶sub encoding { return( shift->_set_get_scalar( 'encoding', @_ ) ); }
Subroutine encoding: called CC=1 SCAR=0.0
Pod documented
474  
475 sub exists
476 1 ▶{
Subroutine exists: called CC=23 SCAR=48.4
Pod documented
477 1 my $self = shift( @_ );
478 1 ▶ my $elem = shift( @_ ) || return( $self->error( "No element to check existence was provided." ) );
Condition50%1/2
shift @_ || (return $self->error('No element to check existence was provided.'))
l!l
10
MC/DC0%0/1
shift @_ || (return $self->error('No element to check existence was provided.'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
479 1 ▶ return( $self->error( "The element provided is not an Text::PO::Element object" ) ) if( !$self->_is_a( $elem => 'Text::PO::Element' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($elem, "Text::PO::Element") 0 1 1
480 1 my $opts = $self->_get_args_as_hash( @_ );
481 1 ▶ $opts->{msgid_only} //= 0;
Condition50%1/2
$opts->{"msgid_only"} //= 0
l!l
01
MC/DC0%0/1
$opts->{"msgid_only"} //= 0
$opts->{"msgid_only"}
Truth table50%1/2
A
A= $opts->{"msgid_only"}
Aresult
11
00
482 1 my $elems = $self->{elements};
483 # No need to go further if the object provided does not even have a msgid
484 1 ▶ return(0) if( !$elem->is_include && !length( $elem->msgid_as_text ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $elem->is_include or length $elem->msgid_as_text 0 1 1
Condition33%1/3
$elem->is_include or length $elem->msgid_as_text
l!l&&r!l&&!r
010
MC/DC0%0/2
$elem->is_include or length $elem->msgid_as_text
$elem->is_include length $elem->msgid_as_text
Truth table33%1/3
A or B
A= $elem->is_includeB= length $elem->msgid_as_text
ABresult
1X1
011
000
485 1 foreach my $e ( @$elems )
486 {
487 1 ▶ if( $e->is_include )
Branch: 2 branches
BranchTrueFalseTotal
if ($e->is_include) { } 0 1 1
elsif ($opts->{'msgid_only'} and ($e->msgid_as_text // '') eq ($elem->msgid_as_text // '') or ($e->msgid_as_text // '') eq ($elem->msgid_as_text // '') and ($e->msgstr_as_text // '') eq ($elem->msgstr_as_text // '')) { } 1 0 1
488 {
489 0 ▶ if( ( $e->file // '' ) eq ( $elem->file // '' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (($e->file // "") eq ($elem->file // "")) 0 0 0
Condition0%0/4
$e->file // ""
l!l
00
$elem->file // ""
l!l
00
MC/DC0%0/1
$e->file // ""
$e->file
MC/DC0%0/1
$elem->file // ""
$elem->file
Truth table0%0/2
A
A= $e->file
Aresult
11
00
Truth table0%0/2
A
A= $elem->file
Aresult
11
00
490 {
491 0 return(1);
492 }
493 }
494 ▶ elsif( ( $opts->{msgid_only} && ( $e->msgid_as_text // '' ) eq ( $elem->msgid_as_text // '' ) ) ||
Condition33%7/21
$opts->{"msgid_only"} and ($e->msgid_as_text // "") eq ($elem->msgid_as_text // "") or ($e->msgid_as_text // "") eq ($elem->msgid_as_text // "") and ($e->msgstr_as_text // "") eq ($elem->msgstr_as_text // "")
l!l&&r!l&&!r
010
$opts->{"msgid_only"} and ($e->msgid_as_text // "") eq ($elem->msgid_as_text // "")
!ll&&!rl&&r
100
$e->msgid_as_text // ""
l!l
00
$elem->msgid_as_text // ""
l!l
00
($e->msgid_as_text // "") eq ($elem->msgid_as_text // "") and ($e->msgstr_as_text // "") eq ($elem->msgstr_as_text // "")
!ll&&!rl&&r
001
$e->msgid_as_text // ""
l!l
10
$elem->msgid_as_text // ""
l!l
10
$e->msgstr_as_text // ""
l!l
10
$elem->msgstr_as_text // ""
l!l
10
MC/DC0%0/4
$opts->{"msgid_only"} and ($e->msgid_as_text // "") eq ($elem->msgid_as_text // "") or ($e->msgid_as_text // "") eq ($elem->msgid_as_text // "") and ($e->msgstr_as_text // "") eq ($elem->msgstr_as_text // "")
$opts->{"msgid_only"} $e->msgid_as_text $e->msgid_as_text $e->msgstr_as_text
MC/DC0%0/1
$elem->msgid_as_text // ""
$elem->msgid_as_text
MC/DC0%0/1
$elem->msgid_as_text // ""
$elem->msgid_as_text
MC/DC0%0/1
$elem->msgstr_as_text // ""
$elem->msgstr_as_text
Truth table14%1/7
A and B or C and D
A= $opts->{"msgid_only"}B= $e->msgid_as_textC= $e->msgid_as_textD= $e->msgstr_as_text
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
Truth table0%0/2
A
A= $elem->msgid_as_text
Aresult
11
00
Truth table50%1/2
A
A= $elem->msgid_as_text
Aresult
11
00
Truth table50%1/2
A
A= $elem->msgstr_as_text
Aresult
11
00
495 ( ( $e->msgid_as_text // '' ) eq ( $elem->msgid_as_text // '' ) && ( $e->msgstr_as_text // '' ) eq ( $elem->msgstr_as_text // '' ) ) )
496 {
497 1 ▶ if( length( $elem->context ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $elem->context) { } 0 1 1
498 {
499 0 ▶ if( $elem->context eq $e->context )
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->context eq $e->context) 0 0 0
500 {
501 0 return(1);
502 }
503 }
504 else
505 {
506 1 return(1);
507 }
508 }
509 }
510 0 return(0);
511 }
512  
513 sub hash
514 1 ▶{
Subroutine hash: called CC=2 SCAR=6.9
Pod documented
515 1 my $self = shift( @_ );
516 1 my $elem = $self->elements;
517 1 my $hash = {};
518 1 foreach my $e ( @$elem )
519 {
520 8 my $msgid = $e->msgid_as_text;
521 8 my $msgstr = $e->msgstr_as_text;
522 8 $hash->{ $msgid } = $msgstr;
523 }
524 1 return( $self->new_hash( $hash ) );
525 }
526  
527 12 ▶sub header { return( shift->_set_get_array_as_object( 'header', @_ ) ); }
Subroutine header: called CC=1 SCAR=0.0
Pod documented
528  
529 14 ▶sub include { return( shift->_set_get_boolean( 'include', @_ ) ); }
Subroutine include: called CC=1 SCAR=0.0
Pod documented
530  
531 9 ▶sub language { return( shift->_set_get_meta_value( 'Language', @_ ) ); }
Subroutine language: called CC=1 SCAR=0.0
Pod documented
532  
533 1 ▶sub language_team { return( shift->_set_get_meta_value( 'Language-Team', @_ ) ); }
Subroutine language_team: called CC=1 SCAR=0.0
Pod documented
534  
535 1 ▶sub last_translator { return( shift->_set_get_meta_value( 'Last-Translator', @_ ) ); }
Subroutine last_translator: called CC=1 SCAR=0.0
Pod documented
536  
537 12 ▶sub max_recurse { return( shift->_set_get_number({
Subroutine max_recurse: called CC=1 SCAR=0.0
Pod documented
538 12 field => 'max_recurse',
539 constraint => 'unsigned_int',
540 }, @_ ) ); }
541  
542 sub merge
543 0 ▶{
Subroutine merge: not called CC=1 SCAR=6.9
Pod documented
544 0 my $self = shift( @_ );
545 0 my $opts = $self->_get_args_as_hash( @_ );
546 0 $opts->{merge} = 1;
547 0 return( $self->sync( $opts ) );
548 }
549  
550 sub meta
551 70 ▶{
Subroutine meta: called CC=8 SCAR=22.8
Pod documented
552 70 my $self = shift( @_ );
553 70 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 62 8 70
554 {
555 62 ▶ if( $self->_is_hash( $_[0] ) )
Branch: 3 branches
BranchTrueFalseTotal
if ($self->_is_hash($_[0])) { } 15 47 62
elsif (scalar @_ == 1) { } 47 0 47
elsif (not @_ % 2) { } 0 0 0
556 {
557 15 $self->{meta} = shift( @_ );
558 }
559 elsif( scalar( @_ ) == 1 )
560 {
561 47 my $k = shift( @_ );
562 47 $k =~ tr/-/_/;
563 47 return( $self->{meta}->{ lc( $k ) } );
564 }
565 elsif( !( @_ % 2 ) )
566 {
567 0 my $this = { @_ };
568 0 foreach my $k ( keys( %$this ) )
569 {
570 0 my $k2 = $k;
571 0 $k2 =~ tr/-/_/;
572 0 $self->{meta}->{ lc( $k2 ) } = $this->{ $k };
573 }
574 }
575 else
576 {
577 0 return( $self->error( "Unknown data provided: '", join( "', '", @_ ), "'." ) );
578 }
579  
580 15 foreach my $k ( keys( %{$self->{meta}} ) )
581 {
582 54 ▶ if( CORE::index( $k, '-' ) != -1 )
Branch: 1 branch
BranchTrueFalseTotal
if (index($k, "-") != -1) 42 12 54
583 {
584 42 my $k2 = $k;
585 42 $k2 =~ tr/-/_/;
586 42 $self->{meta}->{ lc( $k2 ) } = CORE::delete( $self->{meta}->{ $k } );
587 }
588 }
589 }
590 23 return( $self->_set_get_hash_as_mix_object( 'meta' ) );
591 }
592  
593 sub meta_keys
594 3 ▶{
Subroutine meta_keys: called CC=6 SCAR=18.1
Pod documented
595 3 my $self = shift( @_ );
596 3 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 2 3
597 {
598 1 my $ref = shift( @_ );
599 1 ▶ return( $self->error( "Value provided is not an array reference." ) ) if( !$self->_is_array( $ref ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_array($ref) 0 1 1
600 1 my $copy = [@$ref];
601 1 for( @$copy )
602 {
603 1 tr/-/_/;
604 1 $_ = lc( $_ );
605 }
606 1 $self->{meta_keys} = $copy;
607 }
608 3 my $data = $self->{meta_keys};
609 0 ▶ $data = [sort( keys( %{$self->{meta}} ) )] if( !scalar( @$data ) );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$data 0 3 3
610 3 my $new = [];
611 3 for( @$data )
612 {
613 13 push( @$new, $self->normalise_meta( $_ ) );
614 }
615 3 return( $self->new_array( $new ) );
616 }
617  
618 1 ▶sub mime_version { return( shift->_set_get_meta_value( 'MIME-Version', @_ ) ); }
Subroutine mime_version: called CC=1 SCAR=0.0
Pod documented
619  
620 sub new_element
621 9 ▶{
Subroutine new_element: called CC=3 SCAR=11.4
Pod documented
622 9 my $self = shift( @_ );
623 9 my $opts = $self->_get_args_as_hash( @_ );
624 9 $opts->{po} = $self;
625 9 my $e = Text::PO::Element->new( $opts );
626 9 ▶ $e->encoding( $self->encoding ) if( !$opts->{encoding} && $self->encoding );
Branch: 1 branch
BranchTrueFalseTotal
if not $opts->{"encoding"} and $self->encoding 9 0 9
Condition33%1/3
not $opts->{"encoding"} and $self->encoding
!ll&&!rl&&r
009
MC/DC0%0/2
not $opts->{"encoding"} and $self->encoding
not $opts->{"encoding"} $self->encoding
Truth table33%1/3
not(A) and B
A= not $opts->{"encoding"}B= $self->encoding
ABresult
0X0
100
111
627 9 $e->debug( $self->debug );
628 9 return( $e );
629 }
630  
631 sub normalise_meta
632 36 ▶{
Subroutine normalise_meta: called CC=3 SCAR=11.6
Pod documented
633 36 my $self = shift( @_ );
634 36 ▶ my $str = shift( @_ ) || return( '' );
Condition50%1/2
shift @_ || (return '')
l!l
360
MC/DC0%0/1
shift @_ || (return '')
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
635 36 $str =~ tr/_/-/;
636 36 my @res = grep( /^$str$/i, @META );
637 36 ▶ if( scalar( @res ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @res) 36 0 36
638 {
639 36 return( $res[0] );
640 }
641 0 return( '' );
642 }
643  
644 sub parse
645 19 ▶{
Subroutine parse: called CC=85 SCAR=50.6
Pod documented
646 19 my $self = shift( @_ );
647 19 ▶ my $this = shift( @_ ) || return( $self->error( "No file or glob was provided to parse po file." ) );
Condition50%1/2
shift @_ || (return $self->error('No file or glob was provided to parse po file.'))
l!l
190
MC/DC0%0/1
shift @_ || (return $self->error('No file or glob was provided to parse po file.'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
648 19 my $opts = $self->_get_args_as_hash( @_ );
649 19 my $io;
650 19 my $fh_was_open = 0;
651 19 ▶ if( Scalar::Util::reftype( $this ) eq 'GLOB' )
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::reftype($this) eq 'GLOB') { } 0 19 19
elsif (index($this, "\n") != -1) { } 0 19 19
652 {
653 0 $io = $this;
654 0 ▶ return( $self->error( "Filehandle provided '$io' is not opened" ) ) if( !Scalar::Util::openhandle( $io ) );
Branch: 1 branch
BranchTrueFalseTotal
unless &Scalar::Util::openhandle($io) 0 0 0
655 0 $fh_was_open++;
656 0 $self->source({ handle => $this });
657 }
658 elsif( index( $this, "\n" ) != -1 )
659 {
660 0 return( $self->error( "Use parse_data() if you want to parse lines of data." ) );
661 }
662 else
663 {
664 # Use the inherited method 'new_file' from Module::Generic to get a Module::Generic::File object
665 19 ▶ my $file = $self->new_file( $this ) ||
Condition50%1/2
$self->new_file($this) || (return $self->pass_error)
l!l
190
MC/DC0%0/1
$self->new_file($this) || (return $self->pass_error)
$self->new_file($this)
Truth table50%1/2
A
A= $self->new_file($this)
Aresult
11
00
666 return( $self->pass_error );
667 19 ▶ $io = $file->open( '<' ) || return( $self->error( "Unable to open po file \"$this\" in read mode: $!" ) );
Condition50%1/2
$file->open('<') || (return $self->error(qq[Unable to open po file "$this" in read mode: $!]))
l!l
190
MC/DC0%0/1
$file->open('<') || (return $self->error(qq[Unable to open po file "$this" in read mode: $!]))
$file->open('<')
Truth table50%1/2
A
A= $file->open('<')
Aresult
11
00
668 # By default
669 19 $self->source({ file => $file });
670 }
671 19 $io->binmode( ':utf8' );
672 19 my $elem = [];
673 19 $self->{elements} = $elem;
674 19 my $header = '';
675 19 my $ignoring_leading_blanks = 1;
676 19 my $n = 0;
677  
678 19 ▶ my $include = exists( $opts->{include} ) ? $opts->{include} : $self->include;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'include'} ? : 8 11 19
679 # For include recursion
680 19 ▶ my $seen_inc = $opts->{seen} // {};
Condition100%2/2
$opts->{"seen"} // {}
l!l
811
MC/DC100%1/1
$opts->{"seen"} // {}
$opts->{"seen"}
Truth table100%2/2
A
A= $opts->{"seen"}
Aresult
11
00
681 19 ▶ my $depth = $opts->{depth} // 0;
Condition100%2/2
$opts->{"depth"} // 0
l!l
811
MC/DC100%1/1
$opts->{"depth"} // 0
$opts->{"depth"}
Truth table100%2/2
A
A= $opts->{"depth"}
Aresult
11
00
682 19 ▶ my $max_recurse = exists( $opts->{max_recurse} ) ? $opts->{max_recurse} : $self->max_recurse;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'max_recurse'} ? : 8 11 19
683 19 my $lang;
684  
685 19 my $e = Text::PO::Element->new( po => $self );
686 19 $e->debug( $self->debug );
687 # What was the last seen element?
688 # This is used for multi line buffer, so we know where to add it
689 19 my $lastSeen = '';
690 19 my $foundFirstLine = 0;
691 # To keep track of the msgid found so we can skip duplicates
692 19 my $seen = {};
693  
694 my $include_file = sub
695 11 ▶ {
Subroutine __ANON__: called CC=26 SCAR=37.1
696 11 ▶ my $inc_name = shift( @_ ) || return( $self->error( "No file to include was provided." ) );
Condition50%1/2
shift @_ || (return $self->error('No file to include was provided.'))
l!l
110
MC/DC0%0/1
shift @_ || (return $self->error('No file to include was provided.'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
697 11 my $c = shift( @_ ); # The original line
698 11 my $inc_file;
699 # Resolve path relative to current source file, if any
700 11 my $source = $self->source;
701 11 ▶ if( $source && $source->file )
Branch: 1 branch
BranchTrueFalseTotal
if ($source and $source->file) { } 11 0 11
Condition33%1/3
$source and $source->file
!ll&&!rl&&r
0011
MC/DC0%0/2
$source and $source->file
$source $source->file
Truth table33%1/3
A and B
A= $sourceB= $source->file
ABresult
0X0
100
111
702 {
703 11 ▶ my $base_file = $self->new_file( $source->file ) ||
Condition50%1/2
$self->new_file($source->file) || (return $self->pass_error)
l!l
110
MC/DC0%0/1
$self->new_file($source->file) || (return $self->pass_error)
$self->new_file($source->file)
Truth table50%1/2
A
A= $self->new_file($source->file)
Aresult
11
00
704 return( $self->pass_error );
705 11 ▶ $inc_file = $self->new_file( $inc_name, base_file => $base_file ) ||
Condition50%1/2
$self->new_file($inc_name, 'base_file', $base_file) || (return $self->pass_error)
l!l
110
MC/DC0%0/1
$self->new_file($inc_name, 'base_file', $base_file) || (return $self->pass_error)
$self->new_file($inc_name, 'base_file', $base_file)
Truth table50%1/2
A
A= $self->new_file($inc_name, 'base_file', $base_file)
Aresult
11
00
706 return( $self->pass_error );
707 }
708 else
709 {
710 0 $inc_file = $self->new_file( $inc_name );
711 }
712  
713 11 ▶ if( !$inc_file->exists )
Branch: 1 branch
BranchTrueFalseTotal
unless ($inc_file->exists) 1 10 11
714 {
715 # Maybe the user has specified the include file as a domain, and so we try to find the file by adding '.po'
716 1 ▶ if( substr( "$inc_file", -3 ) ne '.po' &&
Branch: 1 branch
BranchTrueFalseTotal
if (substr("$inc_file", -3) ne '.po' and (my $test_file = $self->new_file("$inc_file.po"))->exists) { } 0 1 1
Condition33%1/3
substr("$inc_file", -3) ne ".po" and (my $test_file = $self->new_file("$inc_file.po"))->exists
!ll&&!rl&&r
100
MC/DC0%0/2
substr("$inc_file", -3) ne ".po" and (my $test_file = $self->new_file("$inc_file.po"))->exists
substr("$inc_file", -3) ne ".po" (my $test_file = $self->new_file("$inc_file.po"))->exists
Truth table33%1/3
A and B
A= substr("$inc_file", -3) ne ".po"B= (my $test_file = $self->new_file("$inc_file.po"))->exists
ABresult
0X0
100
111
717 ( my $test_file = $self->new_file( "${inc_file}.po" ) )->exists )
718 {
719 0 $inc_file = $test_file;
720 }
721 else
722 {
723 # Add it as a comment so the user sees it
724 1 $e->add_comment( $c );
725 1 my $msg = "Include file $inc_name ($inc_file) does not exist at line $n";
726 1 ▶ warn( $msg ) if( $self->_is_warnings_enabled );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_warnings_enabled 1 0 1
727 # Add a comment so translators see the problem:
728 1 $e->add_comment( "ERROR: $msg" );
729 1 return(1);
730 }
731 }
732  
733 # Cycle detection: avoid infinite mutual includes
734 10 ▶ if( exists( $seen_inc->{ "$inc_file" } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$seen_inc{"$inc_file"}) 1 9 10
735 {
736 1 my $from = $seen_inc->{ "$inc_file" };
737 1 my $msg = "Include file \"$inc_file\" has already been included in \"$from\".";
738 1 ▶ warn( $msg ) if( $self->_is_warnings_enabled );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_warnings_enabled 1 0 1
739 # Optionally annotate:
740 1 $e->add_comment( "INFO: $msg" );
741 1 return(1);
742 }
743  
744 # Mark as seen from this file
745 # $this might be a glob, but the point here is to mark this include as being already processed.
746 9 ▶ $seen_inc->{ "$inc_file" } = ( $source && $source->file ) ? $source->file : "$this";
Branch: 1 branch
BranchTrueFalseTotal
$source && $source->file ? : 9 0 9
Condition33%1/3
$source and $source->file
!ll&&!rl&&r
009
MC/DC0%0/2
$source and $source->file
$source $source->file
Truth table33%1/3
A and B
A= $sourceB= $source->file
ABresult
0X0
100
111
747  
748 9 ▶ if( ( $depth + 1 ) > $max_recurse )
Branch: 1 branch
BranchTrueFalseTotal
if ($depth + 1 > $max_recurse) 1 8 9
749 {
750 1 ▶ warn( "Maximum include recursion depth ($max_recurse) exceeded (", ( $depth + 1 ), "). Not parsing $inc_file" ) if( $self->_is_warnings_enabled );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_warnings_enabled 1 0 1
751 1 return(1);
752 }
753  
754 # Parse include in a fresh Text::PO object
755 8 my $sub = $self->new;
756 8 $sub->debug( $self->debug );
757  
758 8 my $me = $sub->parse(
759 $inc_file,
760 include => $include,
761 seen => $seen_inc,
762 depth => ( $depth + 1 ),
763 max_recurse => $max_recurse,
764 );
765 8 ▶ if( !$me )
Branch: 1 branch
BranchTrueFalseTotal
unless ($me) 0 8 8
766 {
767 0 return( $self->pass_error( $sub->error ) );
768 }
769  
770 # If the include file has some meta information that include language and we do too, we check, and warn if they do not match
771 8 my $sub_lang = $sub->language;
772 8 ▶ if( defined( $lang ) && defined( $sub_lang ) && lc( $lang ) ne lc( $sub_lang ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lang and defined $sub_lang and lc $lang ne lc $sub_lang) 0 8 8
Condition83%5/6
defined $lang and defined $sub_lang and lc $lang ne lc $sub_lang
!ll&&!rl&&r
710
defined $lang and defined $sub_lang
!ll&&!rl&&r
611
MC/DC0%0/3
defined $lang and defined $sub_lang and lc $lang ne lc $sub_lang
defined $lang defined $sub_lang lc $lang ne lc $sub_lang
Truth table75%3/4
A and B and C
A= defined $langB= defined $sub_langC= lc $lang ne lc $sub_lang
ABCresult
0XX0
10X0
1100
1111
773 {
774 0 ▶ warn( "Warning only: the language ($sub_lang) of the include file ($inc_file) is different than ours ($lang)" ) if( $self->_is_warnings_enabled );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_warnings_enabled 0 0 0
775 }
776  
777 8 my $sub_elems = $sub->elements;
778 8 ▶ if( $sub_elems && @$sub_elems )
Branch: 1 branch
BranchTrueFalseTotal
if ($sub_elems and @$sub_elems) 7 1 8
Condition66%2/3
$sub_elems and @$sub_elems
!ll&&!rl&&r
017
MC/DC50%1/2
$sub_elems and @$sub_elems
$sub_elems @$sub_elems
Truth table66%2/3
A and B
A= $sub_elemsB= @$sub_elems
ABresult
0X0
100
111
779 {
780 # Reuse the same %$seen as for top-level duplicates
781 7 foreach my $se ( @$sub_elems )
782 {
783 12 ▶ my $id = $se->id // '';
Condition50%1/2
$se->id // ""
l!l
120
MC/DC0%0/1
$se->id // ""
$se->id
Truth table50%1/2
A
A= $se->id
Aresult
11
00
784 # Skip duplicate msgid/context combos
785 12 ▶ next if( ++$seen->{ $id } > 1 );
Branch: 1 branch
BranchTrueFalseTotal
if ++$seen->{$id} > 1 0 12 12
786 # Before we add it to our elements, we change ownership
787 12 $se->po( $self );
788 12 push( @$elem, $se );
789 }
790 }
791 8 return( $sub );
792 19 };
793  
794 # Ignore / remove possible leading blank lines
795 19 ▶ while( defined( $_ = $io->getline ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = $io->getline)) 48 0 48
796 {
797 48 $n++;
798 48 ▶ if( /^\S+/ )
Branch: 2 branches
BranchTrueFalseTotal
if (/^\S+/) { } 33 15 48
elsif ($ignoring_leading_blanks and /^[[:blank:]\h]*$/) { } 0 30 30
799 {
800 33 $ignoring_leading_blanks = 0;
801 }
802 ▶ elsif( $ignoring_leading_blanks && /^[[:blank:]\h]*$/ )
Condition33%1/3
$ignoring_leading_blanks and /^[[:blank:]\h]*$/
!ll&&!rl&&r
1500
MC/DC0%0/2
$ignoring_leading_blanks and /^[[:blank:]\h]*$/
$ignoring_leading_blanks /^[[:blank:]\h]*$/
Truth table33%1/3
A and B
A= $ignoring_leading_blanksB= /^[[:blank:]\h]*$/
ABresult
0X0
100
111
803 {
804 0 next;
805 }
806 #( 1 .. /^[^\#]+$/ ) or last;
807 48 ▶ /^\#+/ || last;
Branch: 1 branch
BranchTrueFalseTotal
/^\#+/ or last 29 19 48
808 29 ▶ if( /^\#+[[:blank:]]*(?:\.[[:blank:]]*)?\$include[[:blank:]]+(["'])(.+?)\1$/i )
Branch: 2 branches
BranchTrueFalseTotal
if (/^\#+[[:blank:]]*(?:\.[[:blank:]]*)?\$include[[:blank:]]+(["'])(.+?)\1$/i) { } 8 21 29
elsif (/^\#+[[:blank:]\h]*domain[[:blank:]]+\"([^\"]+)\"/) { } 2 19 21
809 {
810 8 my $inc_name = $2;
811 8 ▶ if( $include )
Branch: 1 branch
BranchTrueFalseTotal
if ($include) 8 0 8
812 {
813 8 ▶ $include_file->( $inc_name, $_, $n ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
&$include_file($inc_name, $_, $n) or return $self->pass_error 8 0 8
814 }
815 # otherwise, we don't do anything, and discard the line
816 }
817 elsif( /^\#+[[:blank:]\h]*domain[[:blank:]]+\"([^\"]+)\"/ )
818 {
819 2 $self->domain( $1 );
820 }
821 else
822 {
823 19 $header .= $_;
824 }
825 }
826 # Remove trailing blank lines from header
827 19 $header =~ s/(^[[:blank:]\h]*\#[[:blank:]\h]*\n$)+\Z//gms;
828 # Make sure to position ourself after the initial blank line if any, since blank lines are used as separators
829 # Actually, no we don't care. Blocks are: maybe some comments, msgid then msgstr. That's how we delimit them
830 # $_ = $io->getline while( /^[[:blank:]]*$/ && defined( $_ ) );
831 19 ▶ $self->header( [split( /\n/, $header )] ) if( length( $header ) );
Branch: 1 branch
BranchTrueFalseTotal
if length $header 11 8 19
832  
833 19 ▶ while( defined( $_ = $io->getline ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = $io->getline)) 262 19 281
834 {
835 262 $n++;
836 262 chomp( $_ );
837 262 ▶ if( !$foundFirstLine && /^\S/ )
Branch: 1 branch
BranchTrueFalseTotal
if (not $foundFirstLine and /^\S/) 19 243 262
Condition66%2/3
not $foundFirstLine and /^\S/
!ll&&!rl&&r
243019
MC/DC50%1/2
not $foundFirstLine and /^\S/
not $foundFirstLine /^\S/
Truth table66%2/3
not(A) and B
A= not $foundFirstLineB= /^\S/
ABresult
0X0
100
111
838 {
839 19 $foundFirstLine++;
840 }
841 262 ▶ if( /^[[:blank:]]*$/ )
Branch: 12 branches
BranchTrueFalseTotal
if (/^[[:blank:]]*$/) { } 53 209 262
elsif (/^\#\.[[:blank:]]*(?<text>(?!.*\$include[[:blank:]]+["'][^"']*["']).*?)$/i) { } 0 209 209
elsif (/^\#\:[[:blank:]]+(.*?)$/) { } 9 200 209
elsif (/^\#\,[[:blank:]]+(.*?)$/) { } 0 200 200
elsif (/^\#+(.*?)$/) { } 6 194 200
elsif (/^msgid[[:blank:]]+"(.*?)"$/) { } 52 142 194
elsif (/^msgid_plural[[:blank:]]+"(.*?)"[[:blank:]]*$/) { } 3 139 142
elsif (/^msgctxt[[:blank:]]+"(.*?)"[[:blank:]]*$/) { } 2 137 139
elsif (/^msgstr[[:blank:]]+"(.*?)"[[:blank:]]*$/) { } 52 85 137
elsif (/^msgstr\[(\d+)\][[:blank:]]+"(.*?)"[[:blank:]]*$/) { } 8 77 85
elsif (/^[[:blank:]]*"(.*?)"[[:blank:]]*$/) { } 75 2 77
elsif (/^\#[[:blank:]\h]*$/) { } 0 2 2
842 {
843 53 ▶ if( $foundFirstLine )
Branch: 1 branch
BranchTrueFalseTotal
if ($foundFirstLine) 53 0 53
844 {
845 # Case where msgid and msgstr are separated by a blank line
846 53 ▶ if( scalar( @$elem ) > 1 &&
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @$elem > 1 and not length $e->msgid_as_text and length $e->msgstr_as_text and length $elem->[-1]->msgid_as_text and not length $elem->[-1]->msgstr_as_text) { } 0 53 53
elsif ($e->is_include) { } 1 52 53
Condition83%10/12
scalar @$elem > 1 and not length $e->msgid_as_text and length $e->msgstr_as_text and length $elem->[-1]->msgid_as_text and not length $elem->[-1]->msgstr_as_text
!ll&&!rl&&r
5120
scalar @$elem > 1 and not length $e->msgid_as_text and length $e->msgstr_as_text and length $elem->[-1]->msgid_as_text
!ll&&!rl&&r
5102
scalar @$elem > 1 and not length $e->msgid_as_text and length $e->msgstr_as_text
!ll&&!rl&&r
5012
scalar @$elem > 1 and not length $e->msgid_as_text
!ll&&!rl&&r
27233
MC/DC0%0/5
scalar @$elem > 1 and not length $e->msgid_as_text and length $e->msgstr_as_text and length $elem->[-1]->msgid_as_text and not length $elem->[-1]->msgstr_as_text
scalar @$elem > 1 not length $e->msgid_as_text length $e->msgstr_as_text length $elem->[-1]->msgid_as_text not length $elem->[-1]->msgstr_as_text
Truth table66%4/6
A and not(B) and C and D and not(E)
A= scalar @$elem > 1B= not length $e->msgid_as_textC= length $e->msgstr_as_textD= length $elem->[-1]->msgid_as_textE= not length $elem->[-1]->msgstr_as_text
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
847 !length( $e->msgid_as_text ) &&
848 length( $e->msgstr_as_text ) &&
849 length( $elem->[-1]->msgid_as_text ) &&
850 !length( $elem->[-1]->msgstr_as_text ) )
851 {
852 0 $elem->[-1]->merge( $e );
853 }
854 elsif( $e->is_include )
855 {
856 1 push( @$elem, $e );
857 }
858 else
859 {
860 52 ▶ if( ++$seen->{ $e->id // '' } > 1 )
Branch: 2 branches
BranchTrueFalseTotal
if (++$$seen{$e->id // ''} > 1) { } 9 43 52
elsif (not $e->id || length($e->msgstr // '')) { } 1 42 43
Condition100%2/2
$e->id // ""
l!l
457
MC/DC100%1/1
$e->id // ""
$e->id
Truth table100%2/2
A
A= $e->id
Aresult
11
00
861 {
862 # next;
863 # NOTE: Do NOT use `next` here. The $e reset block at the end
864 # of this enclosing if-block MUST execute, otherwise the
865 # skipped element's state (msgid, msgstr, flags, reference)
866 # leaks into the next block. Worse, if the next block contains
867 # continuation lines (e.g. `msgid ""` followed by multi-line
868 # strings), `_add()` will push onto the existing arrayref
869 # instead of starting fresh, producing fused msgids.
870 }
871 ▶ elsif( !$e->id && !length( $e->msgstr // '' ) )
Condition100%5/5
$e->id || length($e->msgstr // '')
l!l&&r!l&&!r
28141
$e->msgstr // ""
l!l
141
MC/DC100%2/2
$e->id || length($e->msgstr // '')
$e->id $e->msgstr
Truth table100%3/3
A || B
A= $e->idB= $e->msgstr
ABresult
1X1
011
000
872 {
873 # Skipping empty first element. Probably from a bad file...
874 }
875 else
876 {
877 42 push( @$elem, $e );
878 }
879 }
880 53 $e = Text::PO::Element->new( po => $self );
881 53 $e->{_po_line} = $n;
882 53 ▶ $e->encoding( $self->encoding ) if( $self->encoding );
Branch: 1 branch
BranchTrueFalseTotal
if $self->encoding 19 34 53
883 53 $e->debug( $self->debug );
884 53 $lastSeen = '';
885 }
886  
887 # special treatment for first item that contains the meta information
888 # NOTE: We only attempt to parse meta headers from the very first element
889 # when it actually looks like the meta block, that is: an empty msgid whose
890 # msgstr is the multi-line array of header lines.
891 # A single element is not necessarily the meta: when an include brings exactly
892 # one entry and the main file's first own entry is a duplicate that gets
893 # skipped, $elem->[0] is a real entry whose msgstr is a plain scalar, not an
894 # array reference. Dereferencing it as an array would die under strict refs.
895 53 ▶ if( scalar( @$elem ) == 1 &&
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$elem == 1 and not length($elem->[0]->msgid_as_text // "") and ref $elem->[0]->msgstr eq "ARRAY") 14 39 53
Condition87%7/8
scalar @$elem == 1 and not length($elem->[0]->msgid_as_text // "") and ref $elem->[0]->msgstr eq "ARRAY"
!ll&&!rl&&r
39014
scalar @$elem == 1 and not length($elem->[0]->msgid_as_text // "")
!ll&&!rl&&r
38114
$elem->[0]->msgid_as_text // ""
l!l
123
MC/DC66%2/3
scalar @$elem == 1 and not length($elem->[0]->msgid_as_text // "") and ref $elem->[0]->msgstr eq "ARRAY"
scalar @$elem == 1 $elem->[0]->msgid_as_text ref $elem->[0]->msgstr eq "ARRAY"
Truth table75%3/4
A and not(B) and C
A= scalar @$elem == 1B= $elem->[0]->msgid_as_textC= ref $elem->[0]->msgstr eq "ARRAY"
ABCresult
0XX0
11X0
1000
1011
896 !length( $elem->[0]->msgid_as_text // '' ) &&
897 ref( $elem->[0]->msgstr ) eq 'ARRAY' )
898 {
899 14 my $this = $elem->[0];
900 14 ▶ my $def = $this->msgstr || [];
Condition50%1/2
$this->msgstr || []
l!l
140
MC/DC0%0/1
$this->msgstr || []
$this->msgstr
Truth table50%1/2
A
A= $this->msgstr
Aresult
11
00
901 14 $def = [split( /\n/, join( '', @$def ) )];
902  
903 14 my $meta = {};
904 14 foreach my $s ( @$def )
905 {
906 53 chomp( $s );
907 53 ▶ if( $s =~ /^([^\x00-\x1f\x80-\xff :=]+):[[:blank:]]*(.*?)$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /^([^\x00-\x1f\x80-\xff :=]+):[[:blank:]]*(.*?)$/) 53 0 53
908 {
909 53 my( $k, $v ) = ( lc( $1 ), $2 );
910 53 $meta->{ $k } = $v;
911 53 push( @{$self->{meta_keys}}, $k );
912 53 ▶ if( $k eq 'content-type' )
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "content-type") 5 48 53
913 {
914 5 ▶ if( $v =~ /\bcharset=\s*([-\w]+)/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($v =~ /\bcharset=\s*([-\w]+)/i) 5 0 5
915 {
916 # my $enc = lc( $1 );
917 5 my $enc = $1;
918 ## See PerlIO::encoding man page
919 5 ▶ $enc = 'utf8' if( lc( $enc ) eq 'utf-8' );
Branch: 1 branch
BranchTrueFalseTotal
if lc $enc eq "utf-8" 5 0 5
920 5 $self->encoding( $enc );
921 # try-catch
922 5 local $@;
923 eval
924 5 {
925 5 ▶ $io->binmode( $enc eq 'utf8' ? ":$enc" : ":encoding($enc)" );
Branch: 1 branch
BranchTrueFalseTotal
$enc eq 'utf8' ? : 5 0 5
926 };
927 5 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 5 5
928 {
929 0 return( $self->error( "Unable to set binmode to charset \"$enc\": $@" ) );
930 }
931 }
932 }
933 }
934 }
935 14 ▶ if( scalar( keys( %$meta ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$meta) 14 0 14
936 {
937 14 ▶ $lang = $meta->{language} if( exists( $meta->{language} ) && defined( $meta->{language} ) && length( $meta->{language} // '' ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $meta->{"language"} and defined $meta->{"language"} and length($meta->{"language"} // "") 12 2 14
Condition62%5/8
exists $meta->{"language"} and defined $meta->{"language"} and length($meta->{"language"} // "")
!ll&&!rl&&r
2012
exists $meta->{"language"} and defined $meta->{"language"}
!ll&&!rl&&r
2012
$meta->{"language"} // ""
l!l
120
MC/DC33%1/3
exists $meta->{"language"} and defined $meta->{"language"} and length($meta->{"language"} // "")
exists $meta->{"language"} defined $meta->{"language"} $meta->{"language"}
Truth table50%2/4
A and B and C
A= exists $meta->{"language"}B= defined $meta->{"language"}C= $meta->{"language"}
ABCresult
0XX0
10X0
1100
1111
938 14 $self->meta( $meta );
939 14 $this->is_meta(1);
940 }
941 }
942 }
943 # #. TRANSLATORS: A test phrase with all letters of the English alphabet.
944 # #. Replace it with a sample text in your language, such that it is
945 # #. representative of language's writing system.
946 # We make sure this is not confused with a non-standard include directive
947 elsif( /^\#\.[[:blank:]]*(?<text>(?!.*\$include[[:blank:]]+["'][^"']*["']).*?)$/i )
948 {
949 0 my $c = $1;
950 0 $e->add_auto_comment( $c );
951 }
952 # #: finddialog.cpp:38
953 # #: colorscheme.cpp:79 skycomponents/equator.cpp:31
954 elsif( /^\#\:[[:blank:]]+(.*?)$/ )
955 {
956 9 my $c = $1;
957 9 $e->reference( $c );
958 }
959 # #, c-format
960 elsif( /^\#\,[[:blank:]]+(.*?)$/ )
961 {
962 0 my $c = $1;
963 0 ▶ $e->flags( [ split( /[[:blank:]]*,[[:blank:]]*/, $c ) ] ) if( $c );
Branch: 1 branch
BranchTrueFalseTotal
if $c 0 0 0
964 }
965 # Some other comments:
966 # - domain declaration
967 # - auto comment (extracted with xgettext from the code)
968 # - $include directives
969 elsif( /^\#+(.*?)$/ )
970 {
971 6 my $c = $1;
972  
973 # NOTE: Include directive:
974 # # $include "file.po"
975 # #. $include "file.po"
976 # #.$include "file.po"
977 # case insensitive, and single or double quote is ok.
978 6 ▶ if( $c =~ /^(?:(?:\.[[:blank:]]*)|[[:blank:]]+)\$include[[:blank:]]+(["'])(.+?)\1/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /^(?:(?:\.[[:blank:]]*)|[[:blank:]]+)\$include[[:blank:]]+(["'])(.+?)\1/i) 4 2 6
979 {
980 4 my $inc_name = $2;
981 4 ▶ if( $include )
Branch: 1 branch
BranchTrueFalseTotal
if ($include) { } 3 1 4
982 {
983 3 ▶ $include_file->( $inc_name, $c ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
&$include_file($inc_name, $c) or return $self->pass_error 3 0 3
984  
985 # Since this line is an include directive, we do not treat it as a normal comment.
986 3 next;
987 }
988 # We just record it
989 else
990 {
991 1 $e->is_include(1);
992 1 $e->file( $inc_name );
993 }
994 }
995  
996 # Normal comment / domain handling as before
997 3 ▶ if( !$self->meta->length && $c =~ /^domain[[:blank:]\h]+\"(.*?)\"/ )
Branch: 2 branches
BranchTrueFalseTotal
if (not $self->meta->length and $c =~ /^domain[[:blank:]\h]+\"(.*?)\"/) { } 0 3 3
elsif ($c =~ /^\.[[:blank:]]*(.*?)$/) { } 1 2 3
Condition33%1/3
not $self->meta->length and $c =~ /^domain[[:blank:]\h]+\"(.*?)\"/
!ll&&!rl&&r
300
MC/DC0%0/2
not $self->meta->length and $c =~ /^domain[[:blank:]\h]+\"(.*?)\"/
not $self->meta->length $c =~ /^domain[[:blank:]\h]+\"(.*?)\"/
Truth table33%1/3
not(A) and B
A= not $self->meta->lengthB= $c =~ /^domain[[:blank:]\h]+\"(.*?)\"/
ABresult
0X0
100
111
998 {
999 0 $self->domain( $1 );
1000 }
1001 # It could be a blank auto comment, but we keep it to represent faithfully what we found.
1002 elsif( $c =~ /^\.[[:blank:]]*(.*?)$/ )
1003 {
1004 1 my $auto_comment = $1;
1005 # Trim leading and trailing spaces
1006 1 $auto_comment =~ s/^[[:blank:]]+|[[:blank:]]+$//g;
1007 1 $e->add_auto_comment( $auto_comment );
1008 }
1009 else
1010 {
1011 # Trim leading and trailing spaces
1012 2 $c =~ s/^[[:blank:]]+|[[:blank:]]+$//g;
1013 2 $e->add_comment( $c );
1014 }
1015 }
1016 elsif( /^msgid[[:blank:]]+"(.*?)"$/ )
1017 {
1018 # NOTE: Always reset, even on empty string. An empty `msgid ""` either
1019 # declares an empty msgid (rare but legal: the header element), or
1020 # opens a multi-line msgid filled by subsequent continuation lines.
1021 # In both cases the previous value of msgid must be cleared first,
1022 # otherwise continuations will append to leaked state from a prior
1023 # block (e.g. a duplicate that was skipped without reset).
1024 52 $e->msgid( $self->unquote( $1 ) );
1025 52 $lastSeen = 'msgid';
1026 }
1027 # #: mainwindow.cpp:127
1028 # #, kde-format
1029 # msgid "Time: %1 second"
1030 # msgid_plural "Time: %1 seconds"
1031 # msgstr[0] "Tiempo: %1 segundo"
1032 # msgstr[1] "Tiempo: %1 segundos"
1033 elsif( /^msgid_plural[[:blank:]]+"(.*?)"[[:blank:]]*$/ )
1034 {
1035 3 $e->msgid_plural( $self->unquote( $1 ) );
1036 3 $e->plural(1);
1037 3 $lastSeen = 'msgid_plural';
1038 }
1039 # disambiguating context:
1040 # #: tools/observinglist.cpp:700
1041 # msgctxt "First letter in 'Scope'"
1042 # msgid "S"
1043 # msgstr ""
1044 #
1045 # #: skycomponents/horizoncomponent.cpp:429
1046 # msgctxt "South"
1047 # msgid "S"
1048 # msgstr ""
1049 elsif( /^msgctxt[[:blank:]]+"(.*?)"[[:blank:]]*$/ )
1050 {
1051 2 $e->context( $self->unquote( $1 ) );
1052 2 $lastSeen = 'msgctxt';
1053 }
1054 elsif( /^msgstr[[:blank:]]+"(.*?)"[[:blank:]]*$/ )
1055 {
1056 52 $e->msgstr( $self->unquote( $1 ) );
1057 52 $lastSeen = 'msgstr';
1058 }
1059 elsif( /^msgstr\[(\d+)\][[:blank:]]+"(.*?)"[[:blank:]]*$/ )
1060 {
1061 8 ▶ if( length( $2 ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $2) 8 0 8
1062 {
1063 8 $e->msgstr( $1, $self->unquote( $2 ) );
1064 8 $e->plural(1);
1065 }
1066 8 $lastSeen = 'msgstr';
1067 }
1068 elsif( /^[[:blank:]]*"(.*?)"[[:blank:]]*$/ )
1069 {
1070 75 my $sub = "add_${lastSeen}";
1071 75 ▶ if( $e->can( $sub ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($e->can($sub)) { } 75 0 75
1072 {
1073 75 ▶ $e->$sub( $self->unquote( $1 ) ) if( length( $1 ) );
Branch: 1 branch
BranchTrueFalseTotal
if length $1 75 0 75
1074 }
1075 else
1076 {
1077 0 warn( "Unable to find method \"${sub}\" in class \"", ref( $e ), "\" for line parsed \"$_\"\n" );
1078 }
1079 }
1080 elsif( /^\#[[:blank:]\h]*$/ )
1081 {
1082 # Found some standalone comments; we just ignore
1083 }
1084 else
1085 {
1086 2 ▶ warn( "I do not understand the line \"$_\" at line $n\n" ) if( $self->_is_warnings_enabled );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_warnings_enabled 2 0 2
1087 }
1088 }
1089 19 ▶ $io->close unless( $fh_was_open );
Branch: 1 branch
BranchTrueFalseTotal
unless $fh_was_open 19 0 19
1090 19 ▶ if( scalar( @$elem ) )
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @$elem) { } 18 1 19
elsif ($e->msgid // '') { } 0 1 1
1091 {
1092 18 ▶ if( $elem->[-1] ne $e &&
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->[-1] ne $e and length $e->msgid_as_text and ++$$seen{$e->msgid_as_text} < 2) 7 11 18
Condition66%4/6
$elem->[-1] ne $e and length $e->msgid_as_text and ++$$seen{$e->msgid_as_text} < 2
!ll&&!rl&&r
1107
$elem->[-1] ne $e and length $e->msgid_as_text
!ll&&!rl&&r
0117
MC/DC33%1/3
$elem->[-1] ne $e and length $e->msgid_as_text and ++$$seen{$e->msgid_as_text} < 2
$elem->[-1] ne $e length $e->msgid_as_text ++$$seen{$e->msgid_as_text} < 2
Truth table50%2/4
A and B and C
A= $elem->[-1] ne $eB= length $e->msgid_as_textC= ++$$seen{$e->msgid_as_text} < 2
ABCresult
0XX0
10X0
1100
1111
1093 CORE::length( $e->msgid_as_text ) &&
1094 ++$seen->{ $e->msgid_as_text } < 2 )
1095 {
1096 7 push( @$elem, $e );
1097 }
1098 18 ▶ shift( @$elem ) if( $elem->[0]->is_meta );
Branch: 1 branch
BranchTrueFalseTotal
if $elem->[0]->is_meta 12 6 18
1099 }
1100 ▶ elsif( $e->msgid // '' )
Condition50%1/2
$e->msgid // ""
l!l
01
MC/DC0%0/1
$e->msgid // ""
$e->msgid
Truth table50%1/2
A
A= $e->msgid
Aresult
11
00
1101 {
1102 0 push( @$elem, $e );
1103 }
1104 # Mark this instance as having parsed data (vs an instance where we build data programmatically)
1105 19 $self->{_parsed} = 1;
1106 19 return( $self );
1107 }
1108  
1109 sub parse_date_to_object
1110 2 ▶{
Subroutine parse_date_to_object: called CC=3 SCAR=12.0
Pod documented
1111 2 my $self = shift( @_ );
1112 2 my $str = shift( @_ );
1113 2 ▶ my $d = $self->_parse_timestamp( $str ) ||
Condition50%1/2
$self->_parse_timestamp($str) || (return $self->error(qq[Date time string provided is unsupported: "$str".]))
l!l
20
MC/DC0%0/1
$self->_parse_timestamp($str) || (return $self->error(qq[Date time string provided is unsupported: "$str".]))
$self->_parse_timestamp($str)
Truth table50%1/2
A
A= $self->_parse_timestamp($str)
Aresult
11
00
1114 return( $self->error( "Date time string provided is unsupported: \"${str}\"." ) );
1115 2 my $strp = $d->formatter;
1116 2 ▶ unless( $strp )
Branch: 1 branch
BranchTrueFalseTotal
unless ($strp) 0 2 2
1117 {
1118 0 $strp = DateTime::Format::Lite->new(
1119 pattern => '%Y-%m-%d %H:%M%z',
1120 locale => 'en_GB',
1121 time_zone => $d->time_zone,
1122 );
1123 0 $d->set_formatter( $strp );
1124 }
1125 2 return( $d );
1126 }
1127  
1128 sub parse_header_value
1129 1 ▶{
Subroutine parse_header_value: called CC=11 SCAR=25.6
Pod documented
1130 1 my $self = shift( @_ );
1131 1 my $s = shift( @_ );
1132 1 ▶ return( $self->error( 'Argument string is required' ) ) if( !defined( $s ) || !length( $s ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and length $s 0 1 1
Condition33%1/3
defined $s and length $s
!ll&&!rl&&r
001
MC/DC0%0/2
defined $s and length $s
defined $s length $s
Truth table33%1/3
A and B
A= defined $sB= length $s
ABresult
0X0
100
111
1133 1 ▶ my $sep = @_ ? shift( @_ ) : ';';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 1 1
1134 1 my @parts = ();
1135 1 my $i = 0;
1136 1 foreach( split( /(\\.)|$sep/, $s ) )
1137 {
1138 3 ▶ defined( $_ ) ? do{ $parts[$i] .= $_ } : do{ $i++ };
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 2 1 3
1139 }
1140 1 my $header_val = shift( @parts );
1141 1 my $obj = Text::PO::HeaderValue->new( $header_val );
1142  
1143 1 my $param = {};
1144 1 foreach my $frag ( @parts )
1145 {
1146 1 $frag =~ s/^[[:blank:]]+|[[:blank:]]+$//g;
1147 1 my( $attribute, $value ) = split( /[[:blank:]]*\=[[:blank:]]*/, $frag, 2 );
1148 1 $value =~ s/^\"|\"$//g;
1149 # Check character string and length. Should not be more than 255 characters
1150 # http://tools.ietf.org/html/rfc1341
1151 # http://www.iana.org/assignments/media-types/media-types.xhtml
1152 # Won't complain if this does not meet our requirement, but will discard it silently
1153 1 ▶ if( $attribute =~ /^[a-zA-Z][a-zA-Z0-9\_\-]+$/ && CORE::length( $attribute ) <= 255 )
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute =~ /^[a-zA-Z][a-zA-Z0-9\_\-]+$/ and length $attribute <= 255) 1 0 1
Condition33%1/3
$attribute =~ /^[a-zA-Z][a-zA-Z0-9\_\-]+$/ and length $attribute <= 255
!ll&&!rl&&r
001
MC/DC0%0/2
$attribute =~ /^[a-zA-Z][a-zA-Z0-9\_\-]+$/ and length $attribute <= 255
$attribute =~ /^[a-zA-Z][a-zA-Z0-9\_\-]+$/ length $attribute <= 255
Truth table33%1/3
A and B
A= $attribute =~ /^[a-zA-Z][a-zA-Z0-9\_\-]+$/B= length $attribute <= 255
ABresult
0X0
100
111
1154 {
1155 1 ▶ if( $value =~ /^[a-zA-Z][a-zA-Z0-9\_\-]+$/ && CORE::length( $value ) <= 255 )
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^[a-zA-Z][a-zA-Z0-9\_\-]+$/ and length $value <= 255) 1 0 1
Condition33%1/3
$value =~ /^[a-zA-Z][a-zA-Z0-9\_\-]+$/ and length $value <= 255
!ll&&!rl&&r
001
MC/DC0%0/2
$value =~ /^[a-zA-Z][a-zA-Z0-9\_\-]+$/ and length $value <= 255
$value =~ /^[a-zA-Z][a-zA-Z0-9\_\-]+$/ length $value <= 255
Truth table33%1/3
A and B
A= $value =~ /^[a-zA-Z][a-zA-Z0-9\_\-]+$/B= length $value <= 255
ABresult
0X0
100
111
1156 {
1157 1 $obj->param( lc( $attribute ) => $value );
1158 }
1159 }
1160 }
1161 1 return( $obj );
1162 }
1163  
1164 sub parse2hash
1165 0 ▶{
Subroutine parse2hash: not called CC=10 SCAR=47.0
Pod documented
1166 0 my $self = shift( @_ );
1167 0 ▶ my $this = shift( @_ ) || return( $self->error( "No file or glob was provided to parse po file." ) );
Condition0%0/2
shift @_ || (return $self->error('No file or glob was provided to parse po file.'))
l!l
00
MC/DC0%0/1
shift @_ || (return $self->error('No file or glob was provided to parse po file.'))
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
1168 0 my $buff = '';
1169 0 ▶ if( $self->{use_json} && ( -e( "${this}.json" ) || $this =~ /\.json$/ ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'use_json'} and -e "$this.json" || $this =~ /\.json$/) { } 0 0 0
Condition0%0/6
$self->{"use_json"} and -e "$this.json" || $this =~ /\.json$/
!ll&&!rl&&r
000
-e "$this.json" or $this =~ /\.json$/
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->{"use_json"} and -e "$this.json" || $this =~ /\.json$/
$self->{"use_json"} -e "$this.json" $this =~ /\.json$/
Truth table0%0/4
A and B or C
A= $self->{"use_json"}B= -e "$this.json"C= $this =~ /\.json$/
ABCresult
0XX0
1000
11X1
1011
1170 {
1171 0 ▶ my $file = -e( "${this}.json" ) ? "${this}.json" : $this;
Branch: 1 branch
BranchTrueFalseTotal
-e "$this.json" ? : 0 0 0
1172 0 ▶ my $io = IO::File->new( "$file" ) || return( $self->error( "Unable to open json po file \"${file}\" in read mode: $!" ) );
Condition0%0/2
'IO::File'->new("$file") || (return $self->error(qq[Unable to open json po file "$file" in read mode: $!]))
l!l
00
MC/DC0%0/1
'IO::File'->new("$file") || (return $self->error(qq[Unable to open json po file "$file" in read mode: $!]))
'IO::File'->new("$file")
Truth table0%0/2
A
A= 'IO::File'->new("$file")
Aresult
11
00
1173 0 $io->binmode( ':utf8' );
1174 0 $io->read( $buff, -s( $file ) );
1175 0 $io->close;
1176 0 my $j = $JSON_CLASS->new->relaxed;
1177 0 my $ref = {};
1178 # try-catch
1179 0 local $@;
1180 eval
1181 0 {
1182 0 $ref = $j->decode( $buff );
1183 };
1184 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1185 {
1186 0 return( $self->error( "An error occurred while json decoding data from \"${file}\": $@" ) );
1187 }
1188 0 my $hash = {};
1189 0 foreach my $elem ( @{$ref->{elements}} )
1190 {
1191 0 $hash->{ $elem->{msgid} } = $elem->{msgstr};
1192 }
1193 0 return( $self->new_hash( $hash ) );
1194 }
1195 else
1196 {
1197 0 ▶ $self->parse( $this ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->parse($this) or return $self->pass_error 0 0 0
1198 0 return( $self->hash );
1199 }
1200 }
1201  
1202 sub parse2object
1203 2 ▶{
Subroutine parse2object: called CC=24 SCAR=34.7
Pod documented
1204 2 my $self = shift( @_ );
1205 2 ▶ my $this = shift( @_ ) || return( $self->error( "No file or glob was provided to parse po file." ) );
Condition50%1/2
shift @_ || (return $self->error('No file or glob was provided to parse po file.'))
l!l
20
MC/DC0%0/1
shift @_ || (return $self->error('No file or glob was provided to parse po file.'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
1206 2 my $buff = '';
1207 2 ▶ if( $self->{use_json} && ( -e( "${this}.json" ) || $this =~ /\.json$/ ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'use_json'} and -e "$this.json" || $this =~ /\.json$/) { } 2 0 2
Condition33%2/6
$self->{"use_json"} and -e "$this.json" || $this =~ /\.json$/
!ll&&!rl&&r
002
-e "$this.json" or $this =~ /\.json$/
l!l&&r!l&&!r
020
MC/DC0%0/3
$self->{"use_json"} and -e "$this.json" || $this =~ /\.json$/
$self->{"use_json"} -e "$this.json" $this =~ /\.json$/
Truth table25%1/4
A and B or C
A= $self->{"use_json"}B= -e "$this.json"C= $this =~ /\.json$/
ABCresult
0XX0
1000
11X1
1011
1208 {
1209 2 ▶ my $file = -e( "${this}.json" ) ? "${this}.json" : $this;
Branch: 1 branch
BranchTrueFalseTotal
-e "$this.json" ? : 0 2 2
1210 2 ▶ my $io = IO::File->new( $file ) || return( $self->error( "Unable to open json po file \"${file}\" in read mode: $!" ) );
Condition50%1/2
'IO::File'->new($file) || (return $self->error(qq[Unable to open json po file "$file" in read mode: $!]))
l!l
20
MC/DC0%0/1
'IO::File'->new($file) || (return $self->error(qq[Unable to open json po file "$file" in read mode: $!]))
'IO::File'->new($file)
Truth table50%1/2
A
A= 'IO::File'->new($file)
Aresult
11
00
1211 2 $io->binmode( ':utf8' );
1212 2 $io->read( $buff, -s( $file ) );
1213 2 $io->close;
1214 2 my $j = $JSON_CLASS->new->relaxed;
1215 2 my $ref = {};
1216 # try-catch
1217 2 local $@;
1218 eval
1219 2 {
1220 2 $ref = $j->decode( $buff );
1221 };
1222 2 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
1223 {
1224 0 return( $self->error( "An error occurred while json decoding data from \"${file}\": $@" ) );
1225 }
1226  
1227 2 ▶ $self->domain( $ref->{domain} ) if( length( $ref->{domain} ) && !length( $self->domain ) );
Branch: 1 branch
BranchTrueFalseTotal
if length $ref->{"domain"} and not length $self->domain 0 2 2
Condition33%1/3
length $ref->{"domain"} and not length $self->domain
!ll&&!rl&&r
020
MC/DC0%0/2
length $ref->{"domain"} and not length $self->domain
length $ref->{"domain"} not length $self->domain
Truth table33%1/3
A and not(B)
A= length $ref->{"domain"}B= not length $self->domain
ABresult
0X0
100
111
1228 2 my $meta_keys = [];
1229 2 ▶ if( $ref->{meta_keys} )
Branch: 2 branches
BranchTrueFalseTotal
if ($ref->{'meta_keys'}) { } 2 0 2
elsif ($ref->{'meta'}) { } 0 0 0
1230 {
1231 2 $meta_keys = $ref->{meta_keys};
1232 }
1233 elsif( $ref->{meta} )
1234 {
1235 0 $meta_keys = [sort( keys( %{$ref->{meta}} ) )];
1236 }
1237  
1238 2 ▶ if( $ref->{meta} )
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->{"meta"}) 2 0 2
1239 {
1240 2 $self->{meta} = {};
1241 2 foreach my $k ( keys( %{$ref->{meta}} ) )
1242 {
1243 22 my $k2 = lc( $k );
1244 22 $k2 =~ tr/-/_/;
1245 22 $self->{meta}->{ $k2 } = $ref->{meta}->{ $k };
1246 }
1247 }
1248 2 $self->{meta_keys} = $meta_keys;
1249  
1250 2 ▶ if( scalar( @$meta_keys ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$meta_keys) 2 0 2
1251 {
1252 2 my $e = Text::PO::Element->new( 'po' => $self );
1253 2 $e->debug( $self->debug );
1254 2 $e->msgid( '' );
1255 $e->msgstr(
1256 2 [map( sprintf( '%s: %s', $_, $ref->{meta}->{ $_ } ), @$meta_keys )]
1257 );
1258 2 $e->is_meta(1);
1259 2 push( @{$self->{elements}}, $e );
1260 }
1261  
1262 2 foreach my $def ( @{$ref->{elements}} )
1263 {
1264 16 my $e = Text::PO::Element->new( 'po' => $self );
1265 16 $e->debug( $self->debug );
1266 16 $e->msgid( $def->{msgid} );
1267 16 ▶ if( $def->{msgid_plural} )
Branch: 1 branch
BranchTrueFalseTotal
if ($def->{"msgid_plural"}) 2 14 16
1268 {
1269 2 $e->msgid_plural( $def->{msgid_plural} );
1270 }
1271 16 ▶ if( ref( $def->{msgstr} ) eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $def->{'msgstr'} eq 'ARRAY') { } 2 14 16
1272 {
1273 2 ▶ for( my $i = 0; $i < scalar( @{$def->{msgstr}} ); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$def->{"msgstr"};}) 3 2 5
1274 {
1275 3 $e->msgstr( $i => $def->{msgstr}->[$i] );
1276 }
1277 }
1278 else
1279 {
1280 14 $e->msgstr( $def->{msgstr} );
1281 }
1282 16 ▶ $e->comment( $def->{comment} ) if( $def->{comment} );
Branch: 1 branch
BranchTrueFalseTotal
if $def->{"comment"} 0 16 16
1283 16 ▶ $e->context( $def->{context} ) if( $def->{context} );
Branch: 1 branch
BranchTrueFalseTotal
if $def->{"context"} 0 16 16
1284 16 ▶ $e->flags( $def->{flags} ) if( $def->{flags} );
Branch: 1 branch
BranchTrueFalseTotal
if $def->{"flags"} 0 16 16
1285 16 ▶ $e->reference( $def->{reference} ) if( $def->{reference} );
Branch: 1 branch
BranchTrueFalseTotal
if $def->{"reference"} 6 10 16
1286 16 ▶ $e->encoding( $self->encoding ) if( $self->encoding );
Branch: 1 branch
BranchTrueFalseTotal
if $self->encoding 0 16 16
1287 16 push( @{$self->{elements}}, $e );
1288 }
1289 2 return( $self );
1290 }
1291 else
1292 {
1293 0 return( $self->parse( $this ) );
1294 }
1295 }
1296  
1297 sub plural
1298 3 ▶{
Subroutine plural: called CC=6 SCAR=23.0
Pod documented
1299 3 my $self = shift( @_ );
1300 3 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 3 3
1301 {
1302 0 my( $nplurals, $expr ) = @_;
1303 0 $self->{plural} = [ $nplurals, $expr ];
1304 0 return( [ @{$self->{plural}} ] );
1305 }
1306 else
1307 {
1308 0 ▶ return( [@{$self->{plural}}] ) if( $self->{plural} && scalar( @{$self->{plural}} ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"plural"} and scalar @{$self->{"plural"};} 0 3 3
Condition50%1/2
$self->{"plural"} and scalar @{$self->{"plural"};}
!ll
30
MC/DC0%0/1
$self->{"plural"} and scalar @{$self->{"plural"};}
$self->{"plural"}
Truth table50%1/2
A
A= $self->{"plural"}
Aresult
00
11
1309 3 my $meta = $self->meta;
1310 3 my $pluralDef = $self->meta( 'Plural-Forms' );
1311 3 ▶ if( $pluralDef )
Branch: 1 branch
BranchTrueFalseTotal
if ($pluralDef) 3 0 3
1312 {
1313 3 ▶ if( $pluralDef =~ /^[[:blank:]\h]*nplurals[[:blank:]\h]*=[[:blank:]\h]*(\d+)[[:blank:]\h]*\;[[:blank:]\h]*plural[[:blank:]\h]*=[[:blank:]\h]*(.*?)\;?$/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($pluralDef =~ /^[[:blank:]\h]*nplurals[[:blank:]\h]*=[[:blank:]\h]*(\d+)[[:blank:]\h]*\;[[:blank:]\h]*plural[[:blank:]\h]*=[[:blank:]\h]*(.*?)\;?$/i) { } 3 0 3
1314 {
1315 3 $self->{plural} = [ $1, $2 ];
1316 3 return( $self->{plural} );
1317 }
1318 else
1319 {
1320 0 return( $self->error( "Malformed plural definition found in po data in meta field \"Plural-Forms\": " . $pluralDef ) );
1321 }
1322 }
1323 0 return( [] );
1324 }
1325 }
1326  
1327 2 ▶sub plural_forms { return( shift->_set_get_meta_value( 'Plural-Forms', @_ ) ); }
Subroutine plural_forms: called CC=1 SCAR=0.0
Pod documented
1328  
1329 2 ▶sub po_revision_date { return( shift->_set_get_meta_date( 'PO-Revision-Date', @_ ) ); }
Subroutine po_revision_date: called CC=1 SCAR=0.0
Pod documented
1330  
1331 0 ▶sub pot_creation_date { return( shift->_set_get_meta_date( 'POT-Creation-Date', @_ ) ); }
Subroutine pot_creation_date: not called CC=1 SCAR=6.9
Pod documented
1332  
1333 1 ▶sub project_id_version { return( shift->_set_get_meta_value( 'Project-Id-Version', @_ ) ); }
Subroutine project_id_version: called CC=1 SCAR=0.0
Pod documented
1334  
1335 1 ▶sub report_bugs_to { return( shift->_set_get_meta_value( 'Report-Msgid-Bugs-To', @_ ) ); }
Subroutine report_bugs_to: called CC=1 SCAR=0.0
Pod documented
1336  
1337 sub quote
1338 21 ▶{
Subroutine quote: called CC=2 SCAR=7.0
Pod documented
1339 21 my $self = shift( @_ );
1340 21 my $str = shift( @_ );
1341 21 ▶ return( '' ) if( !length( $str ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $str 0 21 21
1342 # \t is a tab
1343 21 $str =~ s/(?<!\\)\\(?!t)/\\\\/g;
1344 21 $str =~ s/(?<!\\)"/\\"/g;
1345 21 $str =~ s/(?<!\\)\n/\\n/g;
1346 21 return( sprintf( '%s', $str ) );
1347 }
1348  
1349 0 ▶sub remove_duplicates { return( shift->_set_get_boolean( 'remove_duplicates', @_ ) ); }
Subroutine remove_duplicates: not called CC=1 SCAR=6.9
Pod documented
1350  
1351 sub remove_element
1352 0 ▶{
Subroutine remove_element: not called CC=5 SCAR=34.0
Pod documented
1353 0 my $self = shift( @_ );
1354 0 my $elem = shift( @_ );
1355 0 my $rv = $self->exists( $elem );
1356 0 ▶ return if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 0 0
1357 0 ▶ return(0) if( !$rv );
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 0 0 0
1358 0 my $elems = $self->elements;
1359 0 my $found = 0;
1360 0 ▶ for( my $i = 0; $i < scalar( @$elems ); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$elems) 0 0 0
1361 {
1362 0 ▶ if( $elems->[$i] eq $elem )
Branch: 1 branch
BranchTrueFalseTotal
if ($elems->[$i] eq $elem) 0 0 0
1363 {
1364 0 splice( @$elems, $i, 1 );
1365 0 $i--;
1366 0 $found++;
1367 }
1368 }
1369 0 return( $found );
1370 }
1371  
1372 0 ▶sub removed { return( shift->_set_get_array_as_object( 'removed', @_ ) ); }
Subroutine removed: not called CC=1 SCAR=6.9
Pod documented
1373  
1374 sub set_default_meta
1375 0 ▶{
Subroutine set_default_meta: not called CC=4 SCAR=30.0
Pod documented
1376 0 my $self = shift( @_ );
1377 0 foreach my $k ( @META )
1378 {
1379 0 my $k2 = lc( $k );
1380 0 $k2 =~ tr/-/_/;
1381 0 ▶ if( !exists( $self->{meta}->{ $k2 } ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{"meta"}{$k2} and length $Text::PO::DEF_META->{$k}) 0 0 0
Condition0%0/3
not exists $self->{"meta"}{$k2} and length $Text::PO::DEF_META->{$k}
!ll&&!rl&&r
000
MC/DC0%0/2
not exists $self->{"meta"}{$k2} and length $Text::PO::DEF_META->{$k}
not exists $self->{"meta"}{$k2} length $Text::PO::DEF_META->{$k}
Truth table0%0/3
not(A) and B
A= not exists $self->{"meta"}{$k2}B= length $Text::PO::DEF_META->{$k}
ABresult
0X0
100
111
1382 length( $DEF_META->{ $k } ) )
1383 {
1384 0 $self->{meta}->{ $k2 } = $DEF_META->{ $k };
1385 }
1386 }
1387 0 return( $self );
1388 }
1389  
1390 30 ▶sub source { return( shift->_set_get_hash_as_object( 'source', @_ ) ); }
Subroutine source: called CC=1 SCAR=0.0
Pod documented
1391  
1392 sub sync
1393 0 ▶{
Subroutine sync: not called CC=25 SCAR=64.8
Pod documented
1394 0 my $self = shift( @_ );
1395 # a filehandle, or a filename?
1396 # my $this = shift( @_ ) || return( $self->error( "No file or filehandle provided." ) );
1397 0 my $this;
1398 0 ▶ $this = shift( @_ ) if( scalar( @_ ) && ( ( @_ % 2 ) || ( !( @_ % 2 ) && ref( $_[1] ) eq 'HASH' ) ) );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ and @_ % 2 || !(@_ % 2) && ref $_[1] eq "HASH" 0 0 0
Condition0%0/9
scalar @_ and @_ % 2 || !(@_ % 2) && ref $_[1] eq "HASH"
!ll&&!rl&&r
000
@_ % 2 or not @_ % 2 and ref $_[1] eq "HASH"
l!l&&r!l&&!r
000
not @_ % 2 and ref $_[1] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/4
scalar @_ and @_ % 2 || !(@_ % 2) && ref $_[1] eq "HASH"
scalar @_ @_ % 2 not @_ % 2 ref $_[1] eq "HASH"
Truth table0%0/5
A and B or not(C) and D
A= scalar @_B= @_ % 2C= not @_ % 2D= ref $_[1] eq "HASH"
ABCDresult
0XXX0
100X0
10100
11XX1
10111
1399 0 my $opts = $self->_get_args_as_hash( @_ );
1400 0 ▶ $this = ( $opts->{handle} || $opts->{file} ) if( !CORE::length( $this ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $this 0 0 0
Condition0%0/3
$opts->{'handle'} || $opts->{'file'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{'handle'} || $opts->{'file'}
$opts->{'handle'} $opts->{'file'}
Truth table0%0/3
A || B
A= $opts->{'handle'}B= $opts->{'file'}
ABresult
1X1
011
000
1401 0 ▶ if( !$this )
Branch: 1 branch
BranchTrueFalseTotal
unless ($this) 0 0 0
1402 {
1403 0 my $fh;
1404 0 ▶ if( $fh = $self->source->handle )
Branch: 2 branches
BranchTrueFalseTotal
if ($fh = $self->source->handle) { } 0 0 0
elsif (my $file = $self->source->file) { } 0 0 0
1405 {
1406 0 ▶ $this = $fh if( $self->_can_write_fh( $fh ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_can_write_fh($fh) 0 0 0
1407 }
1408 elsif( my $file = $self->source->file )
1409 {
1410 0 ▶ $this = $file if( -e( $file ) && -w( $file ) );
Branch: 1 branch
BranchTrueFalseTotal
if -e $file and -w $file 0 0 0
Condition0%0/3
-e $file and -w $file
!ll&&!rl&&r
000
MC/DC0%0/2
-e $file and -w $file
-e $file -w $file
Truth table0%0/3
A and B
A= -e $fileB= -w $file
ABresult
0X0
100
111
1411 0 ▶ $fh = IO::File->new( ">$file" ) || return( $self->error( "Unable to open file \"$file\" in write mode: $!" ) );
Condition0%0/2
'IO::File'->new(">$file") || (return $self->error(qq[Unable to open file "$file" in write mode: $!]))
l!l
00
MC/DC0%0/1
'IO::File'->new(">$file") || (return $self->error(qq[Unable to open file "$file" in write mode: $!]))
'IO::File'->new(">$file")
Truth table0%0/2
A
A= 'IO::File'->new(">$file")
Aresult
11
00
1412 }
1413 0 ▶ return( $self->error( "No writable file handle or file set to sync our data against." ) ) if( !$this );
Branch: 1 branch
BranchTrueFalseTotal
unless $this 0 0 0
1414 0 $fh->binmode( ':utf8' );
1415 0 ▶ $self->dump( $fh ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->dump($fh) or return $self->pass_error 0 0 0
1416 0 $fh->close;
1417 0 return( $self );
1418 }
1419  
1420 0 ▶ if( Scalar::Util::reftype( $this ) eq 'GLOB' )
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::reftype($this) eq 'GLOB') { } 0 0 0
elsif (-l $this) { } 0 0 0
elsif (-e $this) { } 0 0 0
1421 {
1422 0 ▶ return( $self->error( "Filehandle provided is not opened" ) ) if( !Scalar::Util::openhandle( $this ) );
Branch: 1 branch
BranchTrueFalseTotal
unless &Scalar::Util::openhandle($this) 0 0 0
1423 0 ▶ return( $self->error( "Filehandle provided is not writable" ) ) if( !$self->_can_write_fh( $this ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_can_write_fh($this) 0 0 0
1424 0 return( $self->sync_fh( $this, $opts ) );
1425 }
1426 elsif( -l( $this ) )
1427 {
1428 0 return( $self->error( "File provided is actually a symbolic link. Do not want to write to a symbolic link." ) );
1429 }
1430 elsif( -e( $this ) )
1431 {
1432 0 ▶ if( !-f( $this ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $this) 0 0 0
1433 {
1434 0 return( $self->error( "File '$this' is not a file." ) );
1435 }
1436 0 ▶ my $fh = IO::File->new( "+<$this" ) || return( $self->error( "Unable to open file '$this' in read/write mode: $!" ) );
Condition0%0/2
'IO::File'->new("+<$this") || (return $self->error("Unable to open file '${this}' in read/write mode: $!"))
l!l
00
MC/DC0%0/1
'IO::File'->new("+<$this") || (return $self->error("Unable to open file '${this}' in read/write mode: $!"))
'IO::File'->new("+<$this")
Truth table0%0/2
A
A= 'IO::File'->new("+<$this")
Aresult
11
00
1437 0 my $po = $self->sync_fh( $fh, $opts );
1438 0 $fh->close;
1439 0 return( $po );
1440 }
1441 # Does not exist yet
1442 else
1443 {
1444 0 ▶ my $fh = IO::File->new( ">$this" ) || return( $self->error( "Unable to write to file '$this': $!" ) );
Condition0%0/2
'IO::File'->new(">$this") || (return $self->error("Unable to write to file '${this}': $!"))
l!l
00
MC/DC0%0/1
'IO::File'->new(">$this") || (return $self->error("Unable to write to file '${this}': $!"))
'IO::File'->new(">$this")
Truth table0%0/2
A
A= 'IO::File'->new(">$this")
Aresult
11
00
1445 0 ▶ $self->dump( $fh ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->dump($fh) or return $self->pass_error 0 0 0
1446 0 $fh->close;
1447 }
1448 0 return( $self );
1449 }
1450  
1451 sub sync_fh
1452 0 ▶{
Subroutine sync_fh: not called CC=10 SCAR=47.0
Pod documented
1453 0 my $self = shift( @_ );
1454 0 my $fh = shift( @_ );
1455 0 ▶ return( $self->error( "Filehandle provided $fh is not a valid file handle" ) ) if( !Scalar::Util::openhandle( $fh ) );
Branch: 1 branch
BranchTrueFalseTotal
unless &Scalar::Util::openhandle($fh) 0 0 0
1456 0 my $opts = $self->_get_args_as_hash( @_ );
1457 # Parse file
1458 0 my $po = $self->new( include => 0 );
1459 0 $po->debug( $self->debug );
1460 # Load the target data file
1461 0 $po->parse( $fh );
1462 # Remove the ones that do not exist
1463 0 my $elems = $po->elements;
1464 0 my @removed = ();
1465 # Check the target elements against ours
1466 0 ▶ for( my $i = 0; $i < scalar( @$elems ); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$elems) 0 0 0
1467 {
1468 0 my $e = $elems->[$i];
1469 # Do we have the target element ? If not, it was removed
1470 0 ▶ if( !$self->exists( $e, { msgid_only => 1 } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->exists($e, {'msgid_only', 1})) { } 0 0 0
1471 {
1472 0 my $removedObj = splice( @$elems, $i, 1 );
1473 0 ▶ push( @removed, $removedObj ) if( $removedObj );
Branch: 1 branch
BranchTrueFalseTotal
if $removedObj 0 0 0
1474 0 $i--;
1475 }
1476 else
1477 {
1478 # Ok, already exists
1479 }
1480 }
1481 # Now check each one of ours against this parsed file and add our items if missing
1482 0 $elems = $self->elements;
1483 0 my @added = ();
1484 # Check our source elements against the target ones
1485 0 foreach my $e ( @$elems )
1486 {
1487 # Does the target file have our element ? If not, we add it.
1488 0 ▶ if( !$po->exists( $e, { msgid_only => 1 } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not $po->exists($e, {'msgid_only', 1})) { } 0 0 0
1489 {
1490 0 ▶ if( $e->is_include )
Branch: 1 branch
BranchTrueFalseTotal
if ($e->is_include) { } 0 0 0
1491 {
1492 0 $po->add_include( $e );
1493 }
1494 else
1495 {
1496 0 $po->add_element( $e );
1497 }
1498 0 push( @added, $e );
1499 }
1500 else
1501 {
1502 # Ok, already exists
1503 }
1504 }
1505 # Now, rewind and rewrite the file
1506 0 ▶ $fh->seek(0,0) || return( $self->error( "Unable to seek file handle!: $!" ) );
Branch: 1 branch
BranchTrueFalseTotal
$fh->seek(0, 0) or return $self->error("Unable to seek file handle!: $!") 0 0 0
1507 # $fh->print( $po->dump );
1508 0 ▶ $po->dump( $fh ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$po->dump($fh) or return $self->pass_error 0 0 0
1509 0 $fh->truncate( $fh->tell );
1510 0 $po->added( \@added );
1511 0 $po->removed( \@removed );
1512 0 return( $po );
1513 }
1514  
1515 sub unquote
1516 192 ▶{
Subroutine unquote: called CC=2 SCAR=6.9
Pod documented
1517 192 my $self = shift( @_ );
1518 192 my $str = shift( @_ );
1519 192 ▶ return( '' ) if( !length( $str ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $str 38 154 192
1520 154 $str =~ s/^"(.*)"/$1/;
1521 154 $str =~ s/\\"/"/g;
1522 ## newline
1523 154 $str =~ s/(?<!(\\))\\n/\n/g;
1524 ## inline newline
1525 154 $str =~ s/(?<!(\\))\\{2}n/\\n/g;
1526 ## \ followed by newline
1527 154 $str =~ s/(?<!(\\))\\{3}n/\\\n/g;
1528 ## \ followed by inline newline
1529 154 $str =~ s/\\{4}n/\\\\n/g;
1530 ## all slashes not related to a newline
1531 154 $str =~ s/\\\\(?!n)/\\/g;
1532 154 return( $str );
1533 }
1534  
1535 2 ▶sub use_json { return( shift->_set_get_boolean( 'use_json', @_ ) ); }
Subroutine use_json: called CC=1 SCAR=0.0
Pod documented
1536  
1537 # https://stackoverflow.com/questions/3807231/how-can-i-test-if-i-can-write-to-a-filehandle
1538 # -> https://stackoverflow.com/a/3807381/4814971
1539 sub _can_write_fh
1540 0 ▶{
Subroutine _can_write_fh: not called CC=2 SCAR=17.9
1541 0 my $self = shift( @_ );
1542 0 my $fh = shift( @_ );
1543 0 my $flags = fcntl( $fh, F_GETFL, 0 );
1544 0 ▶ if( ( $flags & O_ACCMODE ) & ( O_WRONLY|O_RDWR ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 3 & 3) 0 0 0
1545 {
1546 0 return(1);
1547 }
1548 0 return(0);
1549 }
1550  
1551 sub _set_get_meta_date
1552 2 ▶{
Subroutine _set_get_meta_date: called CC=7 SCAR=29.6
1553 2 my $self = shift( @_ );
1554 2 ▶ my $field = shift( @_ ) || return( $self->error( "No field was provided to get its DateTime::Lite object equivalent." ) );
Condition50%1/2
shift @_ || (return $self->error('No field was provided to get its DateTime::Lite object equivalent.'))
l!l
20
MC/DC0%0/1
shift @_ || (return $self->error('No field was provided to get its DateTime::Lite object equivalent.'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
1555 2 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 2 2
1556 {
1557 0 my $v = shift( @_ );
1558 0 ▶ if( ref( $v ) && $self->_is_a( $v => 'DateTime::Lite' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v and $self->_is_a($v, "DateTime::Lite")) 0 0 0
Condition0%0/3
ref $v and $self->_is_a($v, "DateTime::Lite")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $v and $self->_is_a($v, "DateTime::Lite")
ref $v $self->_is_a($v, "DateTime::Lite")
Truth table0%0/3
A and B
A= ref $vB= $self->_is_a($v, "DateTime::Lite")
ABresult
0X0
100
111
1559 {
1560 0 my $strp = DateTime::Format::Lite->new(
1561 pattern => '%F %H:%M%z',
1562 locale => 'en_GB',
1563 time_zone => ( HAS_LOCAL_TZ ? 'local' : 'UTC' ),
1564 );
1565 0 $v->set_formatter( $strp );
1566 }
1567 0 $self->meta( $field => $v );
1568 0 return( $v );
1569 }
1570 else
1571 {
1572 2 my $meta = $self->meta( $field );
1573 2 ▶ if( !defined( $meta ) || !length( $meta ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $meta and length $meta) 0 2 2
Condition33%1/3
defined $meta and length $meta
!ll&&!rl&&r
002
MC/DC0%0/2
defined $meta and length $meta
defined $meta length $meta
Truth table33%1/3
A and B
A= defined $metaB= length $meta
ABresult
0X0
100
111
1574 {
1575 0 return;
1576 }
1577 2 return( $self->parse_date_to_object( $meta ) );
1578 }
1579 }
1580  
1581 sub _set_get_meta_value
1582 19 ▶{
Subroutine _set_get_meta_value: called CC=3 SCAR=12.7
1583 19 my $self = shift( @_ );
1584 19 ▶ my $field = shift( @_ ) || return( $self->error( "No field was provided to get its DateTime::Lite object equivalent." ) );
Condition50%1/2
shift @_ || (return $self->error('No field was provided to get its DateTime::Lite object equivalent.'))
l!l
190
MC/DC0%0/1
shift @_ || (return $self->error('No field was provided to get its DateTime::Lite object equivalent.'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
1585 19 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 19 19
1586 {
1587 0 my $v = shift( @_ );
1588 0 $self->meta( $field => $v );
1589 }
1590 19 return( $self->meta( $field ) );
1591 }
1592  
1593 # NOTE: Text::PO::HeaderValue class
1594 {
1595 package
1596 Text::PO::HeaderValue;
1597 BEGIN
1598 6 ▶ {
Subroutine BEGIN: called CC=1 SCAR=0.0
1599 6 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1600 6 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1601 6 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
1602 6 ▶ use vars qw( $VERSION $QUOTE_REGEXP $TYPE_REGEXP $TOKEN_REGEXP $TEXT_REGEXP );
Subroutine BEGIN: called CC=1 SCAR=0.0
1603 6 our $VERSION = 'v0.1.0';
1604 6 ▶ use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
1605 6 '""' => 'as_string',
1606 fallback => 1,
1607 6 );
1608 6 our $QUOTE_REGEXP = qr/([\\"])/;
1609 #
1610 # RegExp to match type in RFC 7231 sec 3.1.1.1
1611 #
1612 # media-type = type "/" subtype
1613 # type = token
1614 # subtype = token
1615 #
1616 6 our $TYPE_REGEXP = qr/^[!#$%&'*+.^_`|~0-9A-Za-z-]+\/[!#$%&'*+.^_`|~0-9A-Za-z-]+$/;
1617 6 our $TOKEN_REGEXP = qr/^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/;
1618 6 our $TEXT_REGEXP = qr/^[\u000b\u0020-\u007e\u0080-\u00ff]+$/;
1619 };
1620  
1621 6 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1622 6 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1623  
1624 sub init
1625 1 ▶ {
Subroutine init: called CC=2 SCAR=7.0
1626 1 my $self = shift( @_ );
1627 1 my $value = shift( @_ );
1628 1 ▶ return( $self->error( "No value provided." ) ) if( !length( $value ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $value 0 1 1
1629 1 $self->{original} = '';
1630 1 $self->{value} = $value;
1631 1 $self->SUPER::init( @_ );
1632 1 $self->{params} = {};
1633 1 return( $self );
1634 }
1635  
1636 sub as_string
1637 0 ▶ {
Subroutine as_string: not called CC=11 SCAR=48.8
1638 0 my $self = shift( @_ );
1639 0 ▶ if( !defined( $self->{original} ) || !length( $self->{original} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"original"} and length $self->{"original"}) 0 0 0
Condition0%0/3
defined $self->{"original"} and length $self->{"original"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"original"} and length $self->{"original"}
defined $self->{"original"} length $self->{"original"}
Truth table0%0/3
A and B
A= defined $self->{"original"}B= length $self->{"original"}
ABresult
0X0
100
111
1640 {
1641 0 my $string = '';
1642 0 ▶ if( defined( $self->{value} ) && length( $self->{value} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"value"} and length $self->{"value"}) 0 0 0
Condition0%0/3
defined $self->{"value"} and length $self->{"value"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"value"} and length $self->{"value"}
defined $self->{"value"} length $self->{"value"}
Truth table0%0/3
A and B
A= defined $self->{"value"}B= length $self->{"value"}
ABresult
0X0
100
111
1643 {
1644 0 ▶ if( $self->{value} !~ /^$TYPE_REGEXP$/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"value"} =~ /^$Text::PO::HeaderValue::TYPE_REGEXP$/) 0 0 0
1645 {
1646 0 return( $self->error( "Invalid value \"$self->{value}\"" ) );
1647 }
1648 0 $string = $self->{value};
1649 }
1650  
1651 # Append parameters
1652 0 ▶ if( $self->{params} && ref( $self->{params} ) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"params"} and ref $self->{"params"} eq "HASH") 0 0 0
Condition0%0/3
$self->{"params"} and ref $self->{"params"} eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"params"} and ref $self->{"params"} eq "HASH"
$self->{"params"} ref $self->{"params"} eq "HASH"
Truth table0%0/3
A and B
A= $self->{"params"}B= ref $self->{"params"} eq "HASH"
ABresult
0X0
100
111
1653 {
1654 0 my $params = [ sort( keys( %{$self->{params}} ) ) ];
1655 0 ▶ for( my $i = 0; $i < scalar( @$params ); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$params) 0 0 0
1656 {
1657 0 ▶ if( $params->[$i] !~ /^$TOKEN_REGEXP$/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($params->[$i] =~ /^$Text::PO::HeaderValue::TOKEN_REGEXP$/) 0 0 0
1658 {
1659 0 return( $self->error( "Invalid parameter name: \"" . $params->[$i] . "\"" ) );
1660 }
1661 0 ▶ if( length( $string ) > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (length $string > 0) 0 0 0
1662 {
1663 0 $string .= '; ';
1664 }
1665 0 $string .= $params->[$i] . '=' . $self->qstring( $self->{params}->{ $params->[$i] } );
1666 }
1667 }
1668 0 $self->{original} = $string;
1669 }
1670 0 return( $self->{original} );
1671 }
1672  
1673 0 ▶ sub original { return( shift->_set_get_scalar_as_object( 'original', @_ ) ); }
Subroutine original: not called CC=1 SCAR=6.9
1674  
1675 sub param
1676 1 ▶ {
Subroutine param: called CC=3 SCAR=11.2
1677 1 my $self = shift( @_ );
1678 1 ▶ my $name = shift( @_ ) || return( $self->error( "No parameter name was provided." ) );
Condition50%1/2
shift @_ || (return $self->error('No parameter name was provided.'))
l!l
10
MC/DC0%0/1
shift @_ || (return $self->error('No parameter name was provided.'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
1679 1 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 0 1
1680 {
1681 1 my $v = shift( @_ );
1682 1 $self->{params}->{ $name } = $v;
1683 }
1684 1 return( $self->{params}->{ $name } );
1685 }
1686  
1687 sub qstring
1688 0 ▶ {
Subroutine qstring: not called CC=4 SCAR=30.0
1689 0 my $self = shift( @_ );
1690 0 my $str = shift( @_ );
1691  
1692 # no need to quote tokens
1693 0 ▶ if( $str =~ /^$TOKEN_REGEXP$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^$Text::PO::HeaderValue::TOKEN_REGEXP$/) 0 0 0
1694 {
1695 0 return( $str );
1696 }
1697  
1698 0 ▶ if( length( $str ) > 0 && $str !~ /^$TEXT_REGEXP$/ )
Branch: 1 branch
BranchTrueFalseTotal
if (length $str > 0 and not $str =~ /^$Text::PO::HeaderValue::TEXT_REGEXP$/) 0 0 0
Condition0%0/3
length $str > 0 and not $str =~ /^$Text::PO::HeaderValue::TEXT_REGEXP$/
!ll&&!rl&&r
000
MC/DC0%0/2
length $str > 0 and not $str =~ /^$Text::PO::HeaderValue::TEXT_REGEXP$/
length $str > 0 not $str =~ /^$Text::PO::HeaderValue::TEXT_REGEXP$/
Truth table0%0/3
A and not(B)
A= length $str > 0B= not $str =~ /^$Text::PO::HeaderValue::TEXT_REGEXP$/
ABresult
0X0
100
111
1699 {
1700 0 return( $self->error( 'Invalid parameter value' ) );
1701 }
1702  
1703 0 $str =~ s/$QUOTE_REGEXP/\\$1/g;
1704 0 return( '"' . $str . '"' );
1705 }
1706  
1707 0 ▶ sub value { return( shift->_set_get_scalar_as_object( 'value', @_ ) ); }
Subroutine value: not called CC=1 SCAR=6.9
1708 }
1709  
1710 6 1;
1711 # NOTE: POD
1712 __END__
« blib/script/po.pl ↑ Summary lib/Text/PO/MO.pm »