Log/Report/Message.pm

filter: Statementstmt 90.7% 108 / 119 Branchbran 68.0% 49 / 72 Conditioncond 52.6% 30 / 57 MC/DCmcdc 22.5% 7 / 31 Subroutinesub 85.7% 30 / 35 Podpod 100% 21 / 21 totaltotal 73.1% 245 / 335 CC 59 SCAR 26.6
CC
59
Cov
75%
toString
35.8
freeze
18.4
new
17.8
CRAP 14.3
« Log/Report/Dispatcher/File.pm ↑ Summary Log/Report/Die.pm »
1 # This code is part of Perl distribution Log-Report version 1.46.
2 # The POD got stripped from this file by OODoc version 3.06.
3 # For contributors see file ChangeLog.
4  
5 # This software is copyright (c) 2007-2026 by Mark Overmeer.
6  
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10  
11  
12 package Log::Report::Message;{
13 17 our $VERSION = '1.46';
14 }
15  
16  
17 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 17 ▶use Log::Report 'log-report', import => [ qw/textdomain/ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 17 ▶use POSIX qw/locale_h/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 17 ▶use List::Util qw/first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 17 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 17 ▶use Log::Report::Util qw/to_html/;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 # Work-around for missing LC_MESSAGES on old Perls and Windows
29 17 ▶{ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 17 eval "&LC_MESSAGES";
31 17 ▶ *LC_MESSAGES = sub(){5} if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 17 17
32 }
33  
34 #--------------------
35  
36 use overload
37 17 ▶ '""' => 'toString',
Subroutine BEGIN: called CC=1 SCAR=0.0
38 2 ▶ '&{}' => sub { my $obj = shift; sub{$obj->clone(@_)} },
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
39 17 '.' => 'concat',
40 17 fallback => 1;
41  
42 #--------------------
43  
44 sub new($@)
45 156 ▶{ my ($class, %s) = @_;
Subroutine new: called CC=15 SCAR=28.8
Pod documented
46  
47 156 ▶ if(ref $s{_count})
Branch: 1 branch
BranchTrueFalseTotal
if (ref $s{"_count"}) 0 156 156
48 0 { my $c = $s{_count};
49 0 ▶ $s{_count} = ref $c eq 'ARRAY' ? @$c : keys %$c;
Branch: 1 branch
BranchTrueFalseTotal
ref $c eq 'ARRAY' ? : 0 0 0
50 }
51  
52 156 ▶ $s{_join} //= $";
Condition66%2/3
$s{"_join"} //= $"
l!l&&r!l&&!r
311250
MC/DC0%0/2
$s{"_join"} //= $"
$s{"_join"} $"
Truth table66%2/3
A //= B
A= $s{"_join"}B= $"
ABresult
1X1
011
000
53  
54 156 ▶ if($s{_msgid})
Branch: 1 branch
BranchTrueFalseTotal
if ($s{"_msgid"}) 143 13 156
55 { $s{_append} = defined $s{_append} ? $1.$s{_append} : $1
56 143 ▶ if $s{_msgid} =~ s/(\s+)$//s;
Branch: 2 branches
BranchTrueFalseTotal
if $s{"_msgid"} =~ s/(\s+)$//s 1 142 143
defined $s{'_append'} ? : 0 1 1
57  
58 $s{_prepend}.= $1
59 143 ▶ if $s{_msgid} =~ s/^(\s+)//s;
Branch: 1 branch
BranchTrueFalseTotal
if $s{"_msgid"} =~ s/^(\s+)//s 1 142 143
60 }
61 156 ▶ if($s{_plural})
Branch: 1 branch
BranchTrueFalseTotal
if ($s{"_plural"}) 20 136 156
62 20 { s/\s+$//, s/^\s+// for $s{_plural};
63 }
64  
65 156 ▶ my $tags = delete $s{_tag} // delete $s{_tags} // delete $s{_class} // delete $s{_classes};
Condition66%6/9
((delete $s{"_tag"} // delete $s{"_tags"}) // delete $s{"_class"}) // delete $s{"_classes"}
l!l&&r!l&&!r
130143
(delete $s{"_tag"} // delete $s{"_tags"}) // delete $s{"_class"}
l!l&&r!l&&!r
130143
delete $s{"_tag"} // delete $s{"_tags"}
l!l&&r!l&&!r
013143
MC/DC25%1/4
((delete $s{"_tag"} // delete $s{"_tags"}) // delete $s{"_class"}) // delete $s{"_classes"}
delete $s{"_tag"} delete $s{"_tags"} delete $s{"_class"} delete $s{"_classes"}
Truth table40%2/5
A // B // C // D
A= delete $s{"_tag"}B= delete $s{"_tags"}C= delete $s{"_class"}D= delete $s{"_classes"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
66 156 ▶ $s{_tags} = ref $tags eq 'ARRAY' ? $tags : [ split /[,\s]+/, $tags ] if defined $tags;
Branch: 2 branches
BranchTrueFalseTotal
if defined $tags 13 143 156
ref $tags eq 'ARRAY' ? : 3 10 13
67  
68 156 bless \%s, $class;
69 }
70  
71 # internal use only: to simplify __*p* functions
72 0 ▶sub _msgctxt($) { $_[0]->{_msgctxt} = $_[1]; $_[0] }
Subroutine _msgctxt: not called CC=1 SCAR=6.9
73  
74  
75 sub clone(@)
76 2 ▶{ my $self = shift;
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
77 2 (ref $self)->new(%$self, @_);
78 }
79  
80 #--------------------
81  
82 2 ▶sub prepend() { $_[0]->{_prepend}}
Subroutine prepend: called CC=1 SCAR=0.0
Pod documented
83 115 ▶sub msgid() { $_[0]->{_msgid} }
Subroutine msgid: called CC=1 SCAR=0.0
Pod documented
84 5 ▶sub append() { $_[0]->{_append} }
Subroutine append: called CC=1 SCAR=0.0
Pod documented
85 32 ▶sub domain() { $_[0]->{_domain} }
Subroutine domain: called CC=1 SCAR=0.0
Pod documented
86 0 ▶sub count() { $_[0]->{_count} }
Subroutine count: not called CC=1 SCAR=6.9
Pod documented
87 0 ▶sub context() { $_[0]->{_context}}
Subroutine context: not called CC=1 SCAR=6.9
Pod documented
88 0 ▶sub msgctxt() { $_[0]->{_msgctxt}}
Subroutine msgctxt: not called CC=1 SCAR=6.9
Pod documented
89  
90  
91 12 ▶sub tags() { @{$_[0]->{_tags} || []} }
Subroutine tags: called CC=2 SCAR=7.2
Pod documented
Condition50%1/2
$_[0]{'_tags'} || []
l!l
120
MC/DC0%0/1
$_[0]{'_tags'} || []
$_[0]{'_tags'}
Truth table50%1/2
A
A= $_[0]{'_tags'}
Aresult
11
00
92 17 *classes = \&tags;
93  
94  
95 0 ▶sub addTags() { push @{shift->{_tags}}, @_ }
Subroutine addTags: not called CC=1 SCAR=6.9
Pod documented
96  
97  
98 sub to(;$)
99 20 ▶{ my $self = shift;
Subroutine to: called CC=2 SCAR=7.2
Pod documented
100 20 ▶ @_ ? $self->{_to} = shift : $self->{_to};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 20 20
101 }
102  
103  
104 sub errno(;$)
105 17 ▶{ my $self = shift;
Subroutine errno: called CC=2 SCAR=7.2
Pod documented
106 17 ▶ @_ ? $self->{_errno} = shift : $self->{_errno};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 17 17
107 }
108  
109  
110 3 ▶sub valueOf($) { $_[0]->{$_[1]} }
Subroutine valueOf: called CC=1 SCAR=0.0
Pod documented
111  
112 #--------------------
113  
114 sub taggedWith($)
115 10 ▶{ my ($self, $match) = @_;
Subroutine taggedWith: called CC=2 SCAR=6.9
Pod documented
116 10 ▶ ref $match eq 'Regexp' ? (first { $_ =~ $$match } $self->tags) : (first { $_ eq $match } $self->tags);
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
ref $match eq 'Regexp' ? : 2 8 10
117 }
118  
119  
120 17 *inClass = \&taggedWith;
121  
122  
123 sub toString(;$)
124 117 ▶{ my ($self, $locale) = @_;
Subroutine toString: called CC=21 SCAR=35.8
Pod documented
125  
126 117 ▶ my $count = $self->{_count} || 0;
Condition100%2/2
$self->{'_count'} || 0
l!l
15102
MC/DC100%1/1
$self->{'_count'} || 0
$self->{'_count'}
Truth table100%2/2
A
A= $self->{'_count'}
Aresult
11
00
127 117 ▶ $locale = $self->{_lang} if $self->{_lang};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_lang"} 0 117 117
128 117 ▶ my $prepend = $self->{_prepend} // '';
Condition100%2/2
$self->{"_prepend"} // ""
l!l
2493
MC/DC100%1/1
$self->{"_prepend"} // ""
$self->{"_prepend"}
Truth table100%2/2
A
A= $self->{"_prepend"}
Aresult
11
00
129 117 ▶ my $append = $self->{_append} // '';
Condition100%2/2
$self->{"_append"} // ""
l!l
14103
MC/DC100%1/1
$self->{"_append"} // ""
$self->{"_append"}
Truth table100%2/2
A
A= $self->{"_append"}
Aresult
11
00
130  
131 117 ▶ $prepend = $prepend->isa(__PACKAGE__) ? $prepend->toString($locale) : "$prepend"
Branch: 2 branches
BranchTrueFalseTotal
if builtin::blessed($prepend) 0 117 117
$prepend->isa(__PACKAGE__) ? : 0 0 0
132 if blessed $prepend;
133  
134 117 ▶ $append = $append->isa(__PACKAGE__) ? $append->toString($locale) : "$append"
Branch: 2 branches
BranchTrueFalseTotal
if builtin::blessed($append) 10 107 117
$append->isa(__PACKAGE__) ? : 10 0 10
135 if blessed $append;
136  
137 $self->{_msgid} # no translation, constant string
138 117 ▶ or return "$prepend$append";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_msgid"} or return "$prepend$append" 100 17 117
139  
140 # assumed is that switching locales is expensive
141 100 my $oldloc = setlocale(LC_MESSAGES);
142 100 ▶ setlocale(LC_MESSAGES, $locale)
Branch: 1 branch
BranchTrueFalseTotal
if defined $locale and !defined($oldloc) || $locale ne $oldloc 0 100 100
Condition16%1/6
defined $locale and !defined($oldloc) || $locale ne $oldloc
!ll&&!rl&&r
10000
not defined $oldloc or $locale ne $oldloc
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $locale and !defined($oldloc) || $locale ne $oldloc
defined $locale not defined $oldloc $locale ne $oldloc
Truth table25%1/4
A and not(B) or C
A= defined $localeB= not defined $oldlocC= $locale ne $oldloc
ABCresult
0XX0
1000
11X1
1011
143 if defined $locale && (!defined $oldloc || $locale ne $oldloc);
144  
145 # translate the msgid
146 100 my $domain = $self->{_domain};
147 blessed $domain && $domain->isa('Log::Report::Minimal::Domain')
148 100 ▶ or $domain = $self->{_domain} = textdomain $domain;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($domain) and $domain->isa("Log::Report::Minimal::Domain") or $domain = ($self->{"_domain"} = &textdomain($domain)) 16 84 100
Condition66%2/3
builtin::blessed($domain) and $domain->isa("Log::Report::Minimal::Domain")
!ll&&!rl&&r
84016
MC/DC50%1/2
builtin::blessed($domain) and $domain->isa("Log::Report::Minimal::Domain")
builtin::blessed($domain) $domain->isa("Log::Report::Minimal::Domain")
Truth table66%2/3
A and B
A= builtin::blessed($domain)B= $domain->isa("Log::Report::Minimal::Domain")
ABresult
0X0
100
111
149  
150 100 ▶ my $format = $domain->translate($self, $locale || $oldloc)
Condition40%2/5
$domain->translate($self, $locale || $oldloc) // (return ())
l!l
1000
$locale || $oldloc
l!l&&r!l&&!r
01000
MC/DC0%0/1
$domain->translate($self, $locale || $oldloc) // (return ())
$domain->translate($self, $locale || $oldloc)
MC/DC0%0/2
$locale || $oldloc
$locale $oldloc
Truth table50%1/2
A
A= $domain->translate($self, $locale || $oldloc)
Aresult
11
00
Truth table33%1/3
A || B
A= $localeB= $oldloc
ABresult
1X1
011
000
151 // return ();
152  
153 # fill-in the fields
154 100 ▶ my $text = $self->{_expand} ? $domain->interpolate($format, $self) : "$prepend$format$append";
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_expand'} ? : 71 29 100
155  
156 100 ▶ setlocale(LC_MESSAGES, $oldloc)
Branch: 1 branch
BranchTrueFalseTotal
if defined $oldloc and !defined($locale) || $oldloc ne $locale 100 0 100
Condition33%2/6
defined $oldloc and !defined($locale) || $oldloc ne $locale
!ll&&!rl&&r
00100
not defined $locale or $oldloc ne $locale
l!l&&r!l&&!r
10000
MC/DC0%0/3
defined $oldloc and !defined($locale) || $oldloc ne $locale
defined $oldloc not defined $locale $oldloc ne $locale
Truth table25%1/4
A and not(B) or C
A= defined $oldlocB= not defined $localeC= $oldloc ne $locale
ABCresult
0XX0
1000
11X1
1011
157 if defined $oldloc && (!defined $locale || $oldloc ne $locale);
158  
159 100 $text;
160 }
161  
162  
163 17 my %tohtml = qw/ > gt < lt " quot & amp /;
164  
165 1 ▶sub toHTML(;$) { to_html($_[0]->toString($_[1])) }
Subroutine toHTML: called CC=1 SCAR=0.0
Pod documented
166  
167  
168 sub untranslated()
169 2 ▶{ my $self = shift;
Subroutine untranslated: called CC=4 SCAR=14.5
Pod documented
170 2 ▶ ($self->{_prepend} // '') . ($self->{_msgid} // '') . ($self->{_append} // '');
Condition66%4/6
$self->{"_prepend"} // ""
l!l
11
$self->{"_msgid"} // ""
l!l
20
$self->{"_append"} // ""
l!l
20
MC/DC100%1/1
$self->{"_prepend"} // ""
$self->{"_prepend"}
MC/DC0%0/1
$self->{"_msgid"} // ""
$self->{"_msgid"}
MC/DC0%0/1
$self->{"_append"} // ""
$self->{"_append"}
Truth table100%2/2
A
A= $self->{"_prepend"}
Aresult
11
00
Truth table50%1/2
A
A= $self->{"_msgid"}
Aresult
11
00
Truth table50%1/2
A
A= $self->{"_append"}
Aresult
11
00
171 }
172  
173  
174 sub concat($;$)
175 24 ▶{ my ($self, $what, $reversed) = @_;
Subroutine concat: called CC=4 SCAR=13.9
Pod documented
176 24 ▶ if($reversed)
Branch: 1 branch
BranchTrueFalseTotal
if ($reversed) 6 18 24
177 6 ▶ { $what .= $self->{_prepend} if defined $self->{_prepend};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_prepend"} 1 5 6
178 6 return (ref $self)->new(%$self, _prepend => $what);
179 }
180  
181 18 ▶ $what = $self->{_append} . $what if defined $self->{_append};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_append"} 8 10 18
182 18 (ref $self)->new(%$self, _append => $what);
183 }
184  
185  
186 sub freeze()
187 4 ▶{ my ($self, %args) = @_;
Subroutine freeze: called CC=11 SCAR=29.1
Pod documented
188 4 my %data = %$self;
189 4 ▶ if(my $p = $data{_prepend})
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = $data{"_prepend"}) 0 4 4
190 0 ▶ { $data{_prepend} = blessed $p && $p->isa(__PACKAGE__) ? $p->freeze(%args) : "$p";
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($p) && $p->isa(__PACKAGE__) ? : 0 0 0
Condition0%0/3
builtin::blessed($p) and $p->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($p) and $p->isa(__PACKAGE__)
builtin::blessed($p) $p->isa(__PACKAGE__)
Truth table0%0/3
A and B
A= builtin::blessed($p)B= $p->isa(__PACKAGE__)
ABresult
0X0
100
111
191 }
192 4 ▶ if(my $a = $data{_append})
Branch: 1 branch
BranchTrueFalseTotal
if (my $a = $data{"_append"}) 2 2 4
193 2 ▶ { $data{_append} = blessed $a && $a->isa(__PACKAGE__) ? $a->freeze(%args) : "$a";
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($a) && $a->isa(__PACKAGE__) ? : 2 0 2
Condition33%1/3
builtin::blessed($a) and $a->isa(__PACKAGE__)
!ll&&!rl&&r
002
MC/DC0%0/2
builtin::blessed($a) and $a->isa(__PACKAGE__)
builtin::blessed($a) $a->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= builtin::blessed($a)B= $a->isa(__PACKAGE__)
ABresult
0X0
100
111
194 }
195 4 ▶ if(my $d = $data{_domain})
Branch: 1 branch
BranchTrueFalseTotal
if (my $d = $data{"_domain"}) 4 0 4
196 4 ▶ { $data{_domain} = $d->name if blessed $d && $d->isa('Log::Report::Minimal::Domain');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($d) and $d->isa("Log::Report::Minimal::Domain") 2 2 4
Condition66%2/3
builtin::blessed($d) and $d->isa("Log::Report::Minimal::Domain")
!ll&&!rl&&r
202
MC/DC50%1/2
builtin::blessed($d) and $d->isa("Log::Report::Minimal::Domain")
builtin::blessed($d) $d->isa("Log::Report::Minimal::Domain")
Truth table66%2/3
A and B
A= builtin::blessed($d)B= $d->isa("Log::Report::Minimal::Domain")
ABresult
0X0
100
111
197 }
198 4 ▶ $data{_lr_version} = $Log::Report::VERSION // '3.14';
Condition50%1/2
$Log::Report::VERSION // "3.14"
l!l
40
MC/DC0%0/1
$Log::Report::VERSION // "3.14"
$Log::Report::VERSION
Truth table50%1/2
A
A= $Log::Report::VERSION
Aresult
11
00
199 4 \%data;
200 }
201  
202  
203 sub thaw($%)
204 4 ▶{ my ($class, $data, %args) = @_;
Subroutine thaw: called CC=5 SCAR=17.5
Pod documented
205 4 my %data = %$data;
206 4 ▶ if(my $p = $data{_prepend})
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = $data{"_prepend"}) 0 4 4
207 0 ▶ { $data{_prepend} = ref $p eq 'HASH' ? $class->thaw($p, %args) : $p;
Branch: 1 branch
BranchTrueFalseTotal
ref $p eq 'HASH' ? : 0 0 0
208 }
209 4 ▶ if(my $a = $data{_append})
Branch: 1 branch
BranchTrueFalseTotal
if (my $a = $data{"_append"}) 2 2 4
210 2 ▶ { $data{_append} = ref $a eq 'HASH' ? $class->thaw($a, %args) : $a;
Branch: 1 branch
BranchTrueFalseTotal
ref $a eq 'HASH' ? : 2 0 2
211 }
212 4 delete $data{_lr_version};
213 4 $class->new(%data);
214 }
215  
216 #--------------------
217  
218 17 1;
« Log/Report/Dispatcher/File.pm ↑ Summary Log/Report/Die.pm »