lib/Text/PO/MO.pm

filter: Statementstmt 87.5% 246 / 281 Branchbran 55.8% 67 / 120 Conditioncond 39.5% 36 / 91 MC/DCmcdc 3.9% 2 / 51 Subroutinesub 60.0% 21 / 35 Podpod 65.2% 15 / 23 totaltotal 64.3% 387 / 601 CC 106 SCAR 42.1
CC
106
Cov
71%
write
139.8
read
60.3
encode
19.1
CRAP 67.5
« lib/Text/PO.pm ↑ Summary lib/Text/PO/Element.pm »
1 ##----------------------------------------------------------------------------
2 ## PO Files Manipulation - ~/lib/Text/PO/MO.pm
3 ## Version v0.4.0
4 ## Copyright(c) 2023 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/06/25
7 ## Modified 2025/12/02
8 ## All rights reserved
9 ##
10 ## This program is free software; you can redistribute it and/or modify it
11 ## under the same terms as Perl itself.
12 ##----------------------------------------------------------------------------
13 package Text::PO::MO;
14 BEGIN
15 2 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 warnings::register_categories( 'Text::PO' );
19 2 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶ use vars qw( $VERSION @META $DEF_META );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶ use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶ use Text::PO;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 our $VERSION = 'v0.4.0';
24 };
25  
26 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 2 our @META = @Text::PO::META;
30 2 our $DEF_META = $Text::PO::DEF_META;
31  
32 sub init
33 3 ▶{
Subroutine init: called CC=1 SCAR=0.0
Pod documented
34 3 my $self = shift( @_ );
35 3 $self->{auto_decode} = 1;
36 3 $self->{default_encoding} = 'utf-8';
37 3 $self->{domain} = undef;
38 3 $self->{encoding} = undef;
39 3 $self->{file} = undef;
40 3 $self->{use_cache} = 1;
41 3 $self->{_init_strict_use_sub} = 1;
42 3 $self->SUPER::init( @_ );
43 3 $self->{revision} = 0;
44 3 $self->{magic} = '0x950412de';
45 3 $self->{_last_modified} = '';
46 3 return( $self );
47 }
48  
49 sub as_object
50 1 ▶{
Subroutine as_object: called CC=13 SCAR=25.8
Pod documented
51 1 my $self = shift( @_ );
52 # Pass through any argument we received over to read()
53 1 ▶ my( $ref, $order ) = $self->read( ( @_ ? @_ : () ) );
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 1 1
54 1 ▶ return( $self->pass_error ) if( !defined( $ref ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ref 0 1 1
55 # Get the raw meta element
56 1 ▶ my $raw = $ref->{ '' } // '';
Condition50%1/2
$ref->{""} // ""
l!l
10
MC/DC0%0/1
$ref->{""} // ""
$ref->{""}
Truth table50%1/2
A
A= $ref->{""}
Aresult
11
00
57 # Split on actual \n, filter non-empty
58 1 my $arr = [grep{ length( $_ ) } split( /\n/, $raw )];
59 1 my $po = Text::PO->new( debug => $self->debug, encoding => $self->encoding, domain => $self->domain );
60  
61 1 my $meta = {};
62 1 my $meta_keys = [];
63 1 foreach my $s ( @$arr )
64 {
65 11 chomp( $s );
66 11 $s =~ s/^[[:blank:]]*"//; # Strip leading spaces and optional "
67 11 $s =~ s/"[[:blank:]]*$//; # Strip trailing " and spaces
68 11 ▶ if( $s =~ /^([^\x00-\x1f\x80-\xff :=]+):[[:blank:]]*(.*?)$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /^([^\x00-\x1f\x80-\xff :=]+):[[:blank:]]*(.*?)$/) 1 10 11
69 {
70 1 my( $k, $v ) = ( lc( $1 ), $2 );
71 1 $meta->{ $k } = $v;
72 1 push( @$meta_keys, $k );
73 }
74 }
75  
76 1 my $rv = $po->meta( $meta );
77 1 $po->meta_keys( $meta_keys );
78 1 my $e = $po->new_element({
79 is_meta => 1,
80 msgid => '',
81 msgstr => $arr,
82 });
83 1 push( @{$po->{elements}}, $e );
84  
85 # Process order to handle contexts and plurals (from previous fix-keep if you applied)
86 1 foreach my $k ( @$order )
87 {
88 9 ▶ next if( !length( $k ) ); # Skip meta
Branch: 1 branch
BranchTrueFalseTotal
unless length $k 1 8 9
89 8 my $orig = $k;
90 8 my $ctx = '';
91 8 my $msgid_plural = '';
92 8 ▶ if( $orig =~ /^(.*?)\x04(.*)$/s )
Branch: 1 branch
BranchTrueFalseTotal
if ($orig =~ /^(.*?)\x04(.*)$/s) 0 8 8
93 {
94 0 $ctx = $1;
95 0 $orig = $2;
96 }
97 8 my @msgid_parts = split( /\x00/, $orig );
98 8 my $msgid = shift( @msgid_parts );
99 8 ▶ $msgid_plural = shift( @msgid_parts ) if( @msgid_parts );
Branch: 1 branch
BranchTrueFalseTotal
if @msgid_parts 1 7 8
100  
101 8 my $v = $ref->{ $k };
102 8 my @msgstr_parts = split( /\x00/, $v );
103  
104 8 my $e = $po->new_element({
105 msgid => $msgid,
106 });
107 8 ▶ $e->context( $ctx ) if( length( $ctx ) );
Branch: 1 branch
BranchTrueFalseTotal
if length $ctx 0 8 8
108 8 ▶ if( length( $msgid_plural ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $msgid_plural) { } 1 7 8
109 {
110 1 $e->msgid_plural( $msgid_plural );
111 1 $e->plural(1);
112 1 ▶ for( my $i = 0; $i < @msgstr_parts; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @msgstr_parts) 2 1 3
113 {
114 2 $e->msgstr( $i, $msgstr_parts[$i] );
115 }
116 }
117 else
118 {
119 7 $e->msgstr( $msgstr_parts[0] );
120 }
121 8 push( @{$po->{elements}}, $e );
122 }
123 1 return( $po );
124 }
125  
126 1 ▶sub auto_decode { return( shift->_set_get_boolean( 'auto_decode', @_ ) ); }
Subroutine auto_decode: called CC=1 SCAR=0.0
Pod documented
127  
128 sub decode
129 2 ▶{
Subroutine decode: called CC=7 SCAR=19.7
Pod documented
130 2 my $self = shift( @_ );
131 2 my $hash = shift( @_ );
132 2 ▶ my $enc = shift( @_ ) || $self->encoding;
Condition66%2/3
shift @_ || $self->encoding
l!l&&r!l&&!r
110
MC/DC0%0/2
shift @_ || $self->encoding
shift @_ $self->encoding
Truth table66%2/3
A || B
A= shift @_B= $self->encoding
ABresult
1X1
011
000
133 2 ▶ return( $self->error( "Data provided is not an hash reference." ) ) if( ref( $hash ) ne 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $hash ne "HASH" 0 2 2
134 2 ▶ return( $self->error( "No character encoding was provided to decode the mo file data." ) ) if( !CORE::length( $enc ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $enc 0 2 2
135 # try-catch
136 2 local $@;
137 eval
138 2 {
139 2 foreach my $k ( sort( keys( %$hash ) ) )
140 {
141 11 my $v = $hash->{ $k };
142 11 my $k2 = Encode::decode( $enc, $k, Encode::FB_CROAK );
143 11 my $v2 = Encode::decode( $enc, $v, Encode::FB_CROAK );
144 10 ▶ CORE::delete( $hash->{ $k } ) if( CORE::length( $k ) );
Branch: 1 branch
BranchTrueFalseTotal
if length $k 0 10 10
145 10 $hash->{ $k2 } = $v2;
146 }
147 };
148 2 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 1 2
149 {
150 1 return( $self->error( "An error occurred while trying to decode mo data using character encoding \"$enc\": $@" ) );
151 }
152 1 return( $hash );
153 }
154  
155 0 ▶sub default_encoding { return( shift->_set_get_scalar( 'default_encoding', @_ ) ); }
Subroutine default_encoding: not called CC=1 SCAR=6.9
Pod documented
156  
157 3 ▶sub domain { return( shift->_set_get_scalar( 'domain', @_ ) ); }
Subroutine domain: called CC=1 SCAR=0.0
Pod documented
158  
159 sub encode
160 9 ▶{
Subroutine encode: called CC=11 SCAR=29.5
Pod documented
161 9 my $self = shift( @_ );
162 9 my $hash = shift( @_ );
163 9 ▶ my $enc = shift( @_ ) || $self->encoding;
Condition33%1/3
shift @_ || $self->encoding
l!l&&r!l&&!r
900
MC/DC0%0/2
shift @_ || $self->encoding
shift @_ $self->encoding
Truth table33%1/3
A || B
A= shift @_B= $self->encoding
ABresult
1X1
011
000
164 9 ▶ return( $self->error( "Data provided is not an hash reference." ) ) if( ref( $hash ) ne 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $hash ne "HASH" 0 9 9
165 9 ▶ return( $self->error( "No character encoding was provided to encode data." ) ) if( !CORE::length( $enc ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $enc 0 9 9
166 # try-catch
167 9 local $@;
168 eval
169 9 {
170 9 foreach my $k ( keys( %$hash ) )
171 {
172 18 my $v = $hash->{ $k };
173 18 ▶ if( $self->_is_array( $hash->{ $k } ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_array($hash->{$k})) { } 0 18 18
elsif (not ref $hash->{$k}) { } 18 0 18
174 {
175 0 ▶ for( my $i = 0; $i < scalar( @{$hash->{ $k }} ); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$hash->{$k};}) 0 0 0
176 {
177 0 ▶ $hash->{ $k }->[$i] = Encode::encode( $enc, $hash->{ $k }->[$i], Encode::FB_CROAK ) if( Encode::is_utf8( $hash->{ $k }->[$i] ) );
Branch: 1 branch
BranchTrueFalseTotal
if &Encode::is_utf8($hash->{$k}[$i]) 0 0 0
178 }
179 }
180 elsif( !ref( $hash->{ $k } ) )
181 {
182 18 ▶ my $v2 = Encode::is_utf8( $v ) ? Encode::encode( $enc, $v, Encode::FB_CROAK ) : $v;
Branch: 1 branch
BranchTrueFalseTotal
&Encode::is_utf8($v) ? : 9 9 18
183 18 $hash->{ $k } = $v2;
184 }
185 }
186 };
187 9 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 9 9
188 {
189 0 return( $self->error( "An error occurred while trying to encode data using character encoding \"$enc\": $@" ) );
190 }
191 9 return( $hash );
192 }
193  
194 6 ▶sub encoding { return( shift->_set_get_scalar( 'encoding', @_ ) ); }
Subroutine encoding: called CC=1 SCAR=0.0
Pod documented
195  
196 4 ▶sub file { return( shift->_set_get_file( 'file', @_ ) ); }
Subroutine file: called CC=1 SCAR=0.0
Pod documented
197  
198 sub read
199 1 ▶{
Subroutine read: called CC=26 SCAR=41.0
Pod documented
200 1 my $self = shift( @_ );
201 1 my $opts = $self->_get_args_as_hash( @_ );
202 1 ▶ my $file = $opts->{file} || $self->file;
Condition33%1/3
$opts->{'file'} || $self->file
l!l&&r!l&&!r
010
MC/DC0%0/2
$opts->{'file'} || $self->file
$opts->{'file'} $self->file
Truth table33%1/3
A || B
A= $opts->{'file'}B= $self->file
ABresult
1X1
011
000
203 # Caching mechanism
204 1 ▶ if( !$self->{use_cache} &&
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"use_cache"} || $opts->{"no_cache"} and -e $file and ref $self->{"_cache"} eq "ARRAY" and $self->{"_last_modified"} and [stat $file]->[9] <= $self->{"_last_modified"}) 0 1 1
Condition33%5/15
not $self->{"use_cache"} || $opts->{"no_cache"} and -e $file and ref $self->{"_cache"} eq "ARRAY" and $self->{"_last_modified"} and [stat $file]->[9] <= $self->{"_last_modified"}
!ll&&!rl&&r
100
not $self->{"use_cache"} || $opts->{"no_cache"} and -e $file and ref $self->{"_cache"} eq "ARRAY" and $self->{"_last_modified"}
!ll&&!rl&&r
100
not $self->{"use_cache"} || $opts->{"no_cache"} and -e $file and ref $self->{"_cache"} eq "ARRAY"
!ll&&!rl&&r
100
not $self->{"use_cache"} || $opts->{"no_cache"} and -e $file
!ll&&!rl&&r
100
$self->{'use_cache'} || $opts->{'no_cache'}
l!l&&r!l&&!r
100
MC/DC0%0/6
not $self->{"use_cache"} || $opts->{"no_cache"} and -e $file and ref $self->{"_cache"} eq "ARRAY" and $self->{"_last_modified"} and [stat $file]->[9] <= $self->{"_last_modified"}
$self->{'use_cache'} $opts->{'no_cache'} -e $file ref $self->{"_cache"} eq "ARRAY" $self->{"_last_modified"} [stat $file]->[9] <= $self->{"_last_modified"}
Truth table14%1/7
not(A || B) and C and D and E and F
A= $self->{'use_cache'}B= $opts->{'no_cache'}C= -e $fileD= ref $self->{"_cache"} eq "ARRAY"E= $self->{"_last_modified"}F= [stat $file]->[9] <= $self->{"_last_modified"}
ABCDEFresult
1XXXXX0
01XXXX0
000XXX0
0010XX0
00110X0
0011100
0011111
205 !$opts->{no_cache} &&
206 -e( $file ) &&
207 ref( $self->{_cache} ) eq 'ARRAY' &&
208 $self->{_last_modified} &&
209 [CORE::stat( $file )]->[9] <= $self->{_last_modified} )
210 {
211 0 ▶ return( wantarray() ? @{$self->{_cache}} : $self->{_cache}->[0] );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
212 }
213 1 ▶ return( $self->error( "mo file \"$file\" does not exist." ) ) if( !-e( $file ) );
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 1 1
214 1 ▶ return( $self->error( "mo file \"$file\" is not readable." ) ) if( !-r( $file ) );
Branch: 1 branch
BranchTrueFalseTotal
unless -r $file 0 1 1
215 1 ▶ my $f = $self->new_file( $file ) ||
Condition50%1/2
$self->new_file($file) || (return $self->error(qq[Unable to open mo file "$file": ], $self->error))
l!l
10
MC/DC0%0/1
$self->new_file($file) || (return $self->error(qq[Unable to open mo file "$file": ], $self->error))
$self->new_file($file)
Truth table50%1/2
A
A= $self->new_file($file)
Aresult
11
00
216 return( $self->error( "Unable to open mo file \"$file\": ", $self->error ) );
217 1 ▶ my $io = $f->open( '<' ) || return( $self->pass_error( $f->error ) );
Condition50%1/2
$f->open('<') || (return $self->pass_error($f->error))
l!l
10
MC/DC0%0/1
$f->open('<') || (return $self->pass_error($f->error))
$f->open('<')
Truth table50%1/2
A
A= $f->open('<')
Aresult
11
00
218 1 $io->binmode;
219 1 my $data;
220 1 $io->read( $data, -s( $file ) );
221 1 $io->close;
222 1 my $byte_order = substr( $data, 0, 4 );
223 1 my $tmpl;
224 # Little endian
225 1 ▶ if( $byte_order eq "\xde\x12\x04\x95" )
Branch: 2 branches
BranchTrueFalseTotal
if ($byte_order eq "\336\cR\cD\225") { } 0 1 1
elsif ($byte_order eq "\225\cD\cR\336") { } 1 0 1
226 {
227 0 $tmpl = "V";
228 }
229 # Big endian
230 elsif( $byte_order eq "\x95\x04\x12\xde" )
231 {
232 1 $tmpl = "N";
233 }
234 else
235 {
236 0 return( $self->error( "Provided file \"$file\" is not a valid mo file." ) );
237 }
238 # Check the MO format revision number
239 1 my $rev_num = unpack( $tmpl, substr( $data, 4, 4 ) );
240 # There is only one revision now: revision 0.
241 1 ▶ return if( $rev_num > 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $rev_num > 0 0 1 1
242 1 $self->{revision} = $rev_num;
243  
244 # Total messages
245 1 my $total = unpack( $tmpl, substr( $data, 8, 4 ) );
246 # Offset to the beginning of the original messages
247 1 my $off_msgid = unpack( $tmpl, substr( $data, 12, 4 ) );
248 # Offset to the beginning of the translated messages
249 1 my $off_msgstr = unpack( $tmpl, substr( $data, 16, 4 ) );
250 1 my $hash = {};
251 1 my $order = [];
252 1 ▶ for( my $i = 0; $i < $total; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $total) 9 1 10
253 {
254 9 my( $len, $off, $msgid, $msgstr );
255 # The first word is the length of the message
256 9 $len = unpack( $tmpl, substr( $data, $off_msgid + $i * 8, 4 ) );
257 # The second word is the offset of the message
258 9 $off = unpack( $tmpl, substr( $data, $off_msgid + $i * 8 + 4, 4 ) );
259 # Original message
260 9 $msgid = substr( $data, $off, $len );
261
262 # The first word is the length of the message
263 9 $len = unpack( $tmpl, substr( $data, $off_msgstr + $i * 8, 4 ) );
264 # The second word is the offset of the message
265 9 $off = unpack( $tmpl, substr( $data, $off_msgstr + $i * 8 + 4, 4 ) );
266 # Translated message
267 9 $msgstr = substr( $data, $off, $len );
268
269 9 $hash->{ $msgid } = $msgstr;
270 9 push( @$order, $msgid );
271 }
272
273 1 ▶ if( $self->auto_decode || $opts->{auto_decode} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->auto_decode or $opts->{"auto_decode"}) 1 0 1
Condition33%1/3
$self->auto_decode or $opts->{"auto_decode"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->auto_decode or $opts->{"auto_decode"}
$self->auto_decode $opts->{"auto_decode"}
Truth table33%1/3
A or B
A= $self->auto_decodeB= $opts->{"auto_decode"}
ABresult
1X1
011
000
274 {
275 1 ▶ unless( my $enc = $self->encoding )
Branch: 1 branch
BranchTrueFalseTotal
unless (my $enc = $self->encoding) 1 0 1
276 {
277 # Find the encoding of that MO file
278 1 ▶ if( defined( $hash->{ '' } ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hash->{''} and $hash->{''} =~ m[Content-Type:[[:blank:]\h]*text/plain;[[:blank:]\h]*charset[[:blank:]\h]*=[[:blank:]\h]*(?<quote>["'])?(?<encoding>[\w\-]+)\g{quote}?]is) { } 1 0 1
Condition33%1/3
defined $hash->{""} and $hash->{""} =~ m[Content-Type:[[:blank:]\h]*text/plain;[[:blank:]\h]*charset[[:blank:]\h]*=[[:blank:]\h]*(?<quote>["'])?(?<encoding>[\w\-]+)\g{quote}?]is
!ll&&!rl&&r
001
MC/DC0%0/2
defined $hash->{""} and $hash->{""} =~ m[Content-Type:[[:blank:]\h]*text/plain;[[:blank:]\h]*charset[[:blank:]\h]*=[[:blank:]\h]*(?<quote>["'])?(?<encoding>[\w\-]+)\g{quote}?]is
defined $hash->{""} $hash->{""} =~ m[Content-Type:[[:blank:]\h]*text/plain;[[:blank:]\h]*charset[[:blank:]\h]*=[[:blank:]\h]*(?<quote>["'])?(?<encoding>[\w\-]+)\g{quote}?]is
Truth table33%1/3
A and B
A= defined $hash->{""}B= $hash->{""} =~ m[Content-Type:[[:blank:]\h]*text/plain;[[:blank:]\h]*charset[[:blank:]\h]*=[[:blank:]\h]*(?<quote>["'])?(?<encoding>[\w\-]+)\g{quote}?]is
ABresult
0X0
100
111
279 $hash->{ '' } =~ /Content-Type:[[:blank:]\h]*text\/plain;[[:blank:]\h]*charset[[:blank:]\h]*=[[:blank:]\h]*(?<quote>["'])?(?<encoding>[\w\-]+)\g{quote}?/is )
280 {
281 1 $enc = $+{encoding};
282 1 ▶ $self->encoding( $enc ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->encoding($enc) or return $self->pass_error 1 0 1
283 }
284 # Default to US-ASCII
285 else
286 {
287 0 ▶ $enc = $self->default_encoding || $opts->{default_encoding};
Condition0%0/3
$self->default_encoding || $opts->{'default_encoding'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->default_encoding || $opts->{'default_encoding'}
$self->default_encoding $opts->{'default_encoding'}
Truth table0%0/3
A || B
A= $self->default_encodingB= $opts->{'default_encoding'}
ABresult
1X1
011
000
288 }
289 1 ▶ $self->decode( $hash, $enc ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->decode($hash, $enc) or return $self->pass_error 1 0 1
290 }
291 1 $self->decode( $hash );
292 }
293 1 $self->{_last_modified} = [CORE::stat( $file )]->[9];
294 1 $self->{_cache} = [ $hash, $order ];
295 1 ▶ return( wantarray() ? ( $hash, $order ) : $hash );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
296 }
297  
298 sub reset
299 0 ▶{
Subroutine reset: not called CC=1 SCAR=6.9
Pod documented
300 0 my $self = shift( @_ );
301 0 $self->{_cache} = [];
302 0 $self->{_last_modified} = '';
303 0 return( $self );
304 }
305  
306 0 ▶sub revision { return( shift->_set_get_scalar( 'revision', @_ ) ); }
Subroutine revision: not called CC=1 SCAR=6.9
Pod documented
307  
308 0 ▶sub use_cache { return( shift->_set_get_boolean( 'use_cache', @_ ) ); }
Subroutine use_cache: not called CC=1 SCAR=6.9
Pod documented
309  
310 sub write
311 1 ▶{
Subroutine write: called CC=53 SCAR=49.4
Pod documented
312 1 my $self = shift( @_ );
313 1 my $po = shift( @_ );
314 1 my $opts = $self->_get_args_as_hash( @_ );
315  
316 1 ▶ if( !defined( $po ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $po) 0 1 1
317 {
318 0 return( $self->error( "I was expecting a Text::PO object, and got nothing." ) );
319 }
320  
321 1 ▶ if( !$self->_is_object( $po ) || !$po->isa( 'Text::PO' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_object($po) and $po->isa("Text::PO")) 0 1 1
Condition33%1/3
$self->_is_object($po) and $po->isa("Text::PO")
!ll&&!rl&&r
001
MC/DC0%0/2
$self->_is_object($po) and $po->isa("Text::PO")
$self->_is_object($po) $po->isa("Text::PO")
Truth table33%1/3
A and B
A= $self->_is_object($po)B= $po->isa("Text::PO")
ABresult
0X0
100
111
322 {
323 0 return( $self->error( "I was expecting a Text::PO object, and got an object of class \"" . ref( $po ) . "\" instead." ) );
324 }
325  
326 1 ▶ $opts->{encoding} //= '';
Condition50%1/2
$opts->{"encoding"} //= ""
l!l
01
MC/DC0%0/1
$opts->{"encoding"} //= ""
$opts->{"encoding"}
Truth table50%1/2
A
A= $opts->{"encoding"}
Aresult
11
00
327 1 ▶ my $enc = $opts->{encoding} || $self->encoding || $self->default_encoding || 'utf-8';
Condition37%3/8
$opts->{'encoding'} || $self->encoding || $self->default_encoding || 'utf-8'
l!l
10
$opts->{"encoding"} or $self->encoding or $self->default_encoding
l!l&&r!l&&!r
100
$opts->{"encoding"} or $self->encoding
l!l&&r!l&&!r
010
MC/DC0%0/3
$opts->{'encoding'} || $self->encoding || $self->default_encoding || 'utf-8'
$opts->{"encoding"} $self->encoding $self->default_encoding
Truth table25%1/4
A or B or C
A= $opts->{"encoding"}B= $self->encodingC= $self->default_encoding
ABCresult
1XX1
01X1
0011
0000
328  
329 # Build the hash of entries to write to the mo file.
330 # Keys are msgid (possibly including context and plural markers),
331 # values are msgstr (for plurals, concatenated with NUL separators).
332 1 my %entries;
333 my @keys;
334  
335 # Header / meta entry (msgid == "")
336 1 my $meta_keys = $po->meta_keys;
337 1 ▶ if( $meta_keys && !$meta_keys->is_empty )
Branch: 1 branch
BranchTrueFalseTotal
if ($meta_keys and not $meta_keys->is_empty) 1 0 1
Condition33%1/3
$meta_keys and not $meta_keys->is_empty
!ll&&!rl&&r
001
MC/DC0%0/2
$meta_keys and not $meta_keys->is_empty
$meta_keys not $meta_keys->is_empty
Truth table33%1/3
A and not(B)
A= $meta_keysB= not $meta_keys->is_empty
ABresult
0X0
100
111
338 {
339 1 my $header = '';
340  
341 1 foreach my $k ( @$meta_keys )
342 {
343 1 my $v = $po->meta( $k );
344 1 ▶ next if( !defined( $v ) || !length( $v ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and length $v 0 1 1
Condition33%1/3
defined $v and length $v
!ll&&!rl&&r
001
MC/DC0%0/2
defined $v and length $v
defined $v length $v
Truth table33%1/3
A and B
A= defined $vB= length $v
ABresult
0X0
100
111
345 # "Key: Value\n" – this is what gettext stores in the header string
346 1 $header .= sprintf( "%s: %s\n", $k, $v );
347 }
348  
349 1 ▶ if( length( $header ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $header) 1 0 1
350 {
351 1 ▶ my $h = $self->encode( { msgid => '', msgstr => $header } => $enc ) || return( $self->pass_error );
Condition50%1/2
$self->encode({'msgid', '', 'msgstr', $header}, $enc) || (return $self->pass_error)
l!l
10
MC/DC0%0/1
$self->encode({'msgid', '', 'msgstr', $header}, $enc) || (return $self->pass_error)
$self->encode({'msgid', '', 'msgstr', $header}, $enc)
Truth table50%1/2
A
A= $self->encode({'msgid', '', 'msgstr', $header}, $enc)
Aresult
11
00
352  
353 1 $entries{ $h->{msgid} } = $h->{msgstr};
354 # usually the empty string
355 1 push( @keys, $h->{msgid} );
356 }
357 }
358  
359 # Regular entries
360 1 my $elems = $po->elements;
361  
362 1 ▶ if( $elems && ref( $elems ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($elems and ref $elems) 1 0 1
Condition33%1/3
$elems and ref $elems
!ll&&!rl&&r
001
MC/DC0%0/2
$elems and ref $elems
$elems ref $elems
Truth table33%1/3
A and B
A= $elemsB= ref $elems
ABresult
0X0
100
111
363 {
364 1 foreach my $e ( @$elems )
365 {
366 9 ▶ next if( !$e );
Branch: 1 branch
BranchTrueFalseTotal
unless $e 0 9 9
367 # Header already handled via meta above
368 9 ▶ next if( $e->can( 'is_meta' ) && $e->is_meta );
Branch: 1 branch
BranchTrueFalseTotal
if $e->can("is_meta") and $e->is_meta 1 8 9
Condition66%2/3
$e->can("is_meta") and $e->is_meta
!ll&&!rl&&r
081
MC/DC50%1/2
$e->can("is_meta") and $e->is_meta
$e->can("is_meta") $e->is_meta
Truth table66%2/3
A and B
A= $e->can("is_meta")B= $e->is_meta
ABresult
0X0
100
111
369 # $include markers themselves should not become entries
370 8 ▶ next if( $e->can( 'is_include' ) && $e->is_include );
Branch: 1 branch
BranchTrueFalseTotal
if $e->can("is_include") and $e->is_include 0 8 8
Condition33%1/3
$e->can("is_include") and $e->is_include
!ll&&!rl&&r
080
MC/DC0%0/2
$e->can("is_include") and $e->is_include
$e->can("is_include") $e->is_include
Truth table33%1/3
A and B
A= $e->can("is_include")B= $e->is_include
ABresult
0X0
100
111
371  
372 8 my $msgid = $e->msgid_as_text;
373 8 ▶ next if( !defined( $msgid ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msgid 0 8 8
374  
375 8 ▶ my $ctx = $e->can( 'context' ) ? ( $e->context || '' ) : '';
Branch: 1 branch
BranchTrueFalseTotal
$e->can('context') ? : 8 0 8
Condition50%1/2
$e->context or ""
l!l
08
MC/DC0%0/1
$e->context or ""
$e->context
Truth table50%1/2
A
A= $e->context
Aresult
11
00
376 8 my $msgid_plural = $e->msgid_plural_as_string;
377  
378 # Build the msgid key as used inside the mo file:
379 # [ctx + EOT] + msgid [+ NUL + msgid_plural]
380 8 my $key = $msgid;
381 8 ▶ if( defined( $msgid_plural ) && length( $msgid_plural ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $msgid_plural and length $msgid_plural) 1 7 8
Condition66%2/3
defined $msgid_plural and length $msgid_plural
!ll&&!rl&&r
701
MC/DC50%1/2
defined $msgid_plural and length $msgid_plural
defined $msgid_plural length $msgid_plural
Truth table66%2/3
A and B
A= defined $msgid_pluralB= length $msgid_plural
ABresult
0X0
100
111
382 {
383 # singular and plural msgid separated by NUL
384 1 $key = join( null(), $msgid, $msgid_plural );
385 }
386  
387 8 ▶ if( defined( $ctx ) && length( $ctx ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ctx and length $ctx) 0 8 8
Condition33%1/3
defined $ctx and length $ctx
!ll&&!rl&&r
080
MC/DC0%0/2
defined $ctx and length $ctx
defined $ctx length $ctx
Truth table33%1/3
A and B
A= defined $ctxB= length $ctx
ABresult
0X0
100
111
388 {
389 # context is prefixed and separated by EOT (0x04)
390 0 $key = join( eot(), $ctx, $key );
391 }
392  
393 # Build msgstr (or msgstr[0]..[n] for plural forms)
394 8 my $val;
395 8 ▶ if( $e->plural )
Branch: 1 branch
BranchTrueFalseTotal
if ($e->plural) { } 1 7 8
396 {
397 1 ▶ my $multi = $e->msgstr // '';
Condition50%1/2
$e->msgstr // ""
l!l
10
MC/DC0%0/1
$e->msgstr // ""
$e->msgstr
Truth table50%1/2
A
A= $e->msgstr
Aresult
11
00
398 1 my @parts;
399  
400 # $multi is an arrayref; each element is either a string
401 # or an arrayref of continuation lines
402 1 ▶ if( ref( $multi ) eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $multi eq "ARRAY") 1 0 1
403 {
404 1 foreach my $variant ( @$multi )
405 {
406 2 my $s = '';
407 2 ▶ if( ref( $variant ) eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $variant eq 'ARRAY') { } 2 0 2
408 {
409 # Multi-line plural - concatenate lines, no extra NUL
410 2 $s = join( '', @$variant );
411 }
412 else
413 {
414 0 ▶ $s = defined( $variant ) ? $variant : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $variant ? : 0 0 0
415 }
416 2 push( @parts, $s );
417 }
418 }
419 # Plural forms are separated by a single NUL
420 1 $val = join( null(), @parts );
421 }
422 else
423 {
424 7 my $m = $e->msgstr;
425 7 ▶ if( ref( $m ) eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $m eq 'ARRAY') { } 0 7 7
426 {
427 # Multi-line singular - concatenate lines, no extra NUL
428 0 $val = join( '', @$m );
429 }
430 else
431 {
432 7 ▶ $val = defined( $m ) ? $m : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $m ? : 7 0 7
433 }
434 }
435  
436 8 ▶ my $h = $self->encode( { msgid => $key, msgstr => $val } => $enc ) || return( $self->pass_error );
Condition50%1/2
$self->encode({'msgid', $key, 'msgstr', $val}, $enc) || (return $self->pass_error)
l!l
80
MC/DC0%0/1
$self->encode({'msgid', $key, 'msgstr', $val}, $enc) || (return $self->pass_error)
$self->encode({'msgid', $key, 'msgstr', $val}, $enc)
Truth table50%1/2
A
A= $self->encode({'msgid', $key, 'msgstr', $val}, $enc)
Aresult
11
00
437 # Later entries override earlier ones for the same msgid, like msgfmt.
438 8 $entries{ $h->{msgid} } = $h->{msgstr};
439 }
440  
441 # Deterministic order:
442 # - header first (if present, already in @keys),
443 # - then all other keys sorted lexicographically (like msgfmt)
444 @keys = do
445 1 {
446 1 my %seen;
447 1 grep{ !$seen{ $_ }++ } @keys, sort( keys( %entries ) );
448 };
449 }
450  
451 # Serialise to the mo format.
452 1 my $cnt = scalar( @keys );
453 1 my $mem = 28 + ( $cnt * 16 );
454 1 my $l10n = [map( $entries{ $_ }, @keys )];
455  
456 1 ▶ my $file = $opts->{file} || $self->file;
Condition33%1/3
$opts->{'file'} || $self->file
l!l&&r!l&&!r
010
MC/DC0%0/2
$opts->{'file'} || $self->file
$opts->{'file'} $self->file
Truth table33%1/3
A || B
A= $opts->{'file'}B= $self->file
ABresult
1X1
011
000
457 1 ▶ if( !defined( $file ) || !length( "$file" ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file and length "$file") 0 1 1
Condition33%1/3
defined $file and length "$file"
!ll&&!rl&&r
001
MC/DC0%0/2
defined $file and length "$file"
defined $file length "$file"
Truth table33%1/3
A and B
A= defined $fileB= length "$file"
ABresult
0X0
100
111
458 {
459 0 return( $self->error( "No file has been set to write mo data to." ) );
460 }
461  
462 1 ▶ my $f = $self->new_file( $file ) || return( $self->pass_error );
Condition50%1/2
$self->new_file($file) || (return $self->pass_error)
l!l
10
MC/DC0%0/1
$self->new_file($file) || (return $self->pass_error)
$self->new_file($file)
Truth table50%1/2
A
A= $self->new_file($file)
Aresult
11
00
463 1 ▶ my $fh = $f->open( '>', { binmode => 'raw', autoflush => 1 } ) ||
Condition50%1/2
$f->open('>', {'binmode', 'raw', 'autoflush', 1}) || (return $self->pass_error($f->error))
l!l
10
MC/DC0%0/1
$f->open('>', {'binmode', 'raw', 'autoflush', 1}) || (return $self->pass_error($f->error))
$f->open('>', {'binmode', 'raw', 'autoflush', 1})
Truth table50%1/2
A
A= $f->open('>', {'binmode', 'raw', 'autoflush', 1})
Aresult
11
00
464 return( $self->pass_error( $f->error ) );
465  
466 # Magic (big-endian), revision, number of strings,
467 # offset of original table, offset of translation table,
468 # hash size and hash offset (unused).
469 1 ▶ $fh->print(
Branch: 1 branch
BranchTrueFalseTotal
$fh->print("\225\4\22\336", "\0\0\0\0", pack("N", $cnt), "\0\0\0\34", pack("N", 28 + $cnt * 8), "\0\0\0\0", "\0\0\0\0") or return $self->error(qq[Unable to write mo header to "$f": $!]) 1 0 1
470 pack( "N", 0x950412de ), # magic
471 pack( "N", 0 ), # revision
472 pack( "N", $cnt ), # number of strings
473 pack( "N", 28 ), # offset of original strings index
474 pack( "N", 28 + $cnt * 8 ), # offset of translated strings index
475 pack( "N", 0 ), # hash table size (unused)
476 pack( "N", 0 ), # hash table offset (unused)
477 ) || return( $self->error( "Unable to write mo header to \"$f\": $!" ) );
478  
479 # Original strings index
480 1 my $cursor = $mem;
481  
482 1 foreach my $k ( @keys )
483 {
484 9 my $len = length( $k );
485 9 ▶ $fh->print( pack( "N", $len ), pack( "N", $cursor ) ) ||
Branch: 1 branch
BranchTrueFalseTotal
$fh->print(pack("N", $len), pack("N", $cursor)) or return $self->error(qq[Unable to write original index for msgid "$k" to "$f": $!]) 9 0 9
486 return( $self->error( "Unable to write original index for msgid \"$k\" to \"$f\": $!" ) );
487 9 $cursor += $len + 1; # account for terminating NUL
488 }
489  
490 # Translated strings index
491 1 foreach my $v ( @{$l10n} )
492 {
493 9 my $len = length( $v );
494 9 ▶ $fh->print( pack( "N", $len ), pack( "N", $cursor ) ) ||
Branch: 1 branch
BranchTrueFalseTotal
$fh->print(pack("N", $len), pack("N", $cursor)) or return $self->error(qq[Unable to write translated index to "$f": $!]) 9 0 9
495 return( $self->error( "Unable to write translated index to \"$f\": $!" ) );
496 9 $cursor += $len + 1; # account for terminating NUL
497 }
498  
499 # Original strings
500 1 foreach my $k ( @keys )
501 {
502 9 ▶ $fh->print( $k, "\0" ) ||
Branch: 1 branch
BranchTrueFalseTotal
$fh->print($k, "\0") or return $self->error(qq[Unable to write original string for msgid "$k" to "$f": $!]) 9 0 9
503 return( $self->error( "Unable to write original string for msgid \"$k\" to \"$f\": $!" ) );
504 }
505  
506 # Translated strings
507 1 foreach my $v ( @{$l10n} )
508 {
509 9 ▶ $fh->print( $v, "\0" ) ||
Branch: 1 branch
BranchTrueFalseTotal
$fh->print($v, "\0") or return $self->error(qq[Unable to write translated string to "$f": $!]) 9 0 9
510 return( $self->error( "Unable to write translated string to \"$f\": $!" ) );
511 }
512  
513 1 $fh->close;
514 # We could do this, but it would return a Module::Generic::DateTime object, and we just need a simple unix timestamp.
515 # $self->{_last_modified} = $f->last_modified;
516 1 $self->{_last_modified} = [CORE::stat( "$f" )]->[9];
517 1 $self->{_cache} = [];
518 1 return( $self );
519 }
520  
521 # NOTE: helper functions
522 # Credits to Ryan Niebur
523 sub character
524 0 ▶{
Subroutine character: not called CC=1 SCAR=6.9
Pod documented
525 0 return( map{ pack( "N*", $_ ) } @_ );
526 }
527  
528 sub eot
529 0 ▶{
Subroutine eot: not called CC=1 SCAR=6.9
Pod undocumented
530 0 return( chr(4) );
531 }
532  
533 sub from_character
534 0 ▶{
Subroutine from_character: not called CC=1 SCAR=6.9
Pod undocumented
535 0 return( character( _from_character( @_ ) ) );
536 }
537  
538 sub from_hex
539 0 ▶{
Subroutine from_hex: not called CC=1 SCAR=6.9
Pod undocumented
540 0 return( character( _from_hex( @_ ) ) );
541 }
542  
543 sub from_string
544 0 ▶{
Subroutine from_string: not called CC=1 SCAR=6.9
Pod undocumented
545 0 return( join_string( from_character( _from_string( @_ ) ) ) );
546 }
547  
548 sub join_string
549 0 ▶{
Subroutine join_string: not called CC=1 SCAR=6.9
Pod undocumented
550 0 return( join( '', @_ ) );
551 }
552  
553 sub null
554 2 ▶{
Subroutine null: called CC=1 SCAR=0.0
Pod undocumented
555 2 return( null_terminate( '' ) );
556 }
557  
558 sub null_terminate
559 2 ▶{
Subroutine null_terminate: called CC=1 SCAR=0.0
Pod undocumented
560 2 return( pack( "Z*", shift( @_ ) ) );
561 }
562  
563 sub number_to_s
564 0 ▶{
Subroutine number_to_s: not called CC=1 SCAR=6.9
Pod undocumented
565 0 return( sprintf( "%d", shift( @_ ) ) );
566 }
567  
568 sub _from_character
569 0 ▶{
Subroutine _from_character: not called CC=1 SCAR=6.9
570 0 return( map( ord( $_ ), @_ ) );
571 }
572  
573 sub _from_hex
574 0 ▶{
Subroutine _from_hex: not called CC=1 SCAR=6.9
575 0 return( map( hex( $_ ), @_ ) );
576 }
577  
578 sub _from_string
579 0 ▶{
Subroutine _from_string: not called CC=1 SCAR=6.9
580 0 return( split( //, join( '', @_ ) ) );
581 }
582  
583 2 1;
584 # NOTE: POD
585 __END__
« lib/Text/PO.pm ↑ Summary lib/Text/PO/Element.pm »