blib/lib/SignalWire/POM/PromptObjectModel.pm

filter: Statementstmt 37.5% 68 / 181 Branchbran 28.5% 24 / 84 Conditioncond 18.5% 15 / 81 MC/DCmcdc 0.0% 0 / 48 Subroutinesub 56.5% 13 / 23 Podpod 0.0% 0 / 10 totaltotal 28.1% 120 / 427 CC 79 SCAR 50.0
CC
79
Cov
31%
_build_section_from_hash
348.0
render_markdown
72.0
render_xml
72.0
CRAP 148.8
« blib/lib/SignalWire/POM/Section.pm ↑ Summary blib/lib/SignalWire/Skills/Builtin/WebSearch.pm »
1 package SignalWire::POM::PromptObjectModel;
2 # Copyright (c) 2025 SignalWire
3 # Licensed under the MIT License.
4 #
5 # Typed Prompt Object Model — Perl port of
6 # signalwire.pom.pom.PromptObjectModel
7 # (signalwire-python/signalwire/signalwire/pom/pom.py).
8 #
9 # A POM is an ordered tree of L<SignalWire::POM::Section> objects that
10 # collectively describe a structured prompt for a large language model.
11 # This class exposes JSON / YAML serialisation, Markdown / XML rendering,
12 # and a search/append API that matches the Python reference exactly.
13 #
14 # Output format MUST match the Python reference byte-for-byte; see
15 # t/pom/prompt_object_model.t for the parity assertions.
16  
17 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use SignalWire::POM::Section;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # ---------- attributes ----------
27  
28 has sections => (
29 is => 'rw',
30 3 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
31 1 );
32  
33 has debug => (
34 is => 'rw',
35 3 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
36 1 );
37  
38 # ---------- Class methods (constructors from JSON / YAML) ----------
39  
40 # Build a PromptObjectModel from JSON. Accepts either a JSON string or
41 # an already-decoded arrayref (mirroring Python's overloaded contract).
42 #
43 # Python signature: ``@staticmethod from_json(json_data)``. The
44 # ``$class_or_self`` receiver name is the SDK-wide convention for
45 # Perl methods that mirror Python ``@staticmethod`` helpers — both
46 # ``Class->from_json($data)`` and ``$instance->from_json($data)`` work,
47 # and the cross-language signature diff strips the receiver entirely.
48 0 ▶sub from_json {
Subroutine from_json: not called CC=3 SCAR=24.8
Pod undocumented
49 0 my ($class_or_self, $json_data) = @_;
50 0 ▶ my $class = ref($class_or_self) || $class_or_self;
Condition0%0/3
ref $class_or_self || $class_or_self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class_or_self || $class_or_self
ref $class_or_self $class_or_self
Truth table0%0/3
A || B
A= ref $class_or_selfB= $class_or_self
ABresult
1X1
011
000
51 0 my $data;
52 0 ▶ if (!ref $json_data) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $json_data) { } 0 0 0
53 0 $data = JSON::PP::decode_json($json_data);
54 } else {
55 0 $data = $json_data;
56 }
57 0 return $class->_from_data($data);
58 }
59  
60 # Build a PromptObjectModel from YAML. Accepts either a YAML string or
61 # an already-decoded arrayref. Requires YAML::PP at runtime — loaded
62 # lazily so JSON-only callers don't pay the dependency.
63 #
64 # Python signature: ``@staticmethod from_yaml(yaml_data)`` — the
65 # ``$class_or_self`` receiver name marks it as a static helper for
66 # the cross-language diff (see ``from_json`` above).
67 0 ▶sub from_yaml {
Subroutine from_yaml: not called CC=3 SCAR=24.8
Pod undocumented
68 0 my ($class_or_self, $yaml_data) = @_;
69 0 ▶ my $class = ref($class_or_self) || $class_or_self;
Condition0%0/3
ref $class_or_self || $class_or_self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class_or_self || $class_or_self
ref $class_or_self $class_or_self
Truth table0%0/3
A || B
A= ref $class_or_selfB= $class_or_self
ABresult
1X1
011
000
70 0 my $data;
71 0 ▶ if (!ref $yaml_data) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $yaml_data) { } 0 0 0
72 0 require YAML::PP;
73 0 require YAML::PP::Common;
74 0 my $yp = YAML::PP->new(
75 preserve => YAML::PP::Common::PRESERVE_ORDER(),
76 );
77 0 $data = $yp->load_string($yaml_data);
78 } else {
79 0 $data = $yaml_data;
80 }
81 0 return $class->_from_data($data);
82 }
83  
84 # Internal factory: walk a list of section hashrefs and build the
85 # Section tree. Validates the same constraints Python's _from_dict
86 # enforces (title type, bullets type, every section has a body or
87 # bullets or subsections, only the first top-level section may omit
88 # its title).
89 3 ▶sub _from_data {
Subroutine _from_data: called CC=6 SCAR=19.2
90 3 my ($class, $data) = @_;
91 3 ▶ croak "POM data must be an arrayref of section hashes"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "ARRAY" 0 3 3
92 unless ref $data eq 'ARRAY';
93  
94 3 my $self = $class->new;
95 3 my $i = 0;
96 3 for my $sec (@$data) {
97 3 ▶ croak "Each POM section must be a hashref" unless ref $sec eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sec eq "HASH" 0 3 3
98 # Python: "only the first section can have no title" — Python
99 # silently sets a default title; mirror that.
100 3 ▶ if ($i > 0 && !exists $sec->{title}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0 and not exists $sec->{"title"}) 0 3 3
Condition33%1/3
$i > 0 and not exists $sec->{"title"}
!ll&&!rl&&r
300
MC/DC0%0/2
$i > 0 and not exists $sec->{"title"}
$i > 0 not exists $sec->{"title"}
Truth table33%1/3
A and not(B)
A= $i > 0B= not exists $sec->{"title"}
ABresult
0X0
100
111
101 0 $sec->{title} = 'Untitled Section';
102 }
103 3 push @{ $self->sections }, _build_section_from_hash($sec, 0);
104 3 $i++;
105 }
106 3 return $self;
107 }
108  
109 3 ▶sub _build_section_from_hash {
Subroutine _build_section_from_hash: called CC=37 SCAR=58.5
110 3 my ($d, $is_subsection) = @_;
111 3 ▶ croak "Each section must be a hashref" unless ref $d eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $d eq "HASH" 0 3 3
112  
113 3 ▶ if (exists $d->{title} && defined $d->{title} && ref $d->{title}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $d->{"title"} and defined $d->{"title"} and ref $d->{"title"}) 0 3 3
Condition33%2/6
exists $d->{"title"} and defined $d->{"title"} and ref $d->{"title"}
!ll&&!rl&&r
030
exists $d->{"title"} and defined $d->{"title"}
!ll&&!rl&&r
003
MC/DC0%0/3
exists $d->{"title"} and defined $d->{"title"} and ref $d->{"title"}
exists $d->{"title"} defined $d->{"title"} ref $d->{"title"}
Truth table25%1/4
A and B and C
A= exists $d->{"title"}B= defined $d->{"title"}C= ref $d->{"title"}
ABCresult
0XX0
10X0
1100
1111
114 0 croak "'title' must be a string if present";
115 }
116 3 ▶ if (exists $d->{subsections} && ref $d->{subsections} ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $d->{"subsections"} and ref $d->{"subsections"} ne "ARRAY") 0 3 3
Condition33%1/3
exists $d->{"subsections"} and ref $d->{"subsections"} ne "ARRAY"
!ll&&!rl&&r
300
MC/DC0%0/2
exists $d->{"subsections"} and ref $d->{"subsections"} ne "ARRAY"
exists $d->{"subsections"} ref $d->{"subsections"} ne "ARRAY"
Truth table33%1/3
A and B
A= exists $d->{"subsections"}B= ref $d->{"subsections"} ne "ARRAY"
ABresult
0X0
100
111
117 0 croak "'subsections' must be an arrayref if provided";
118 }
119 3 ▶ if (exists $d->{bullets} && ref $d->{bullets} ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $d->{"bullets"} and ref $d->{"bullets"} ne "ARRAY") 0 3 3
Condition33%1/3
exists $d->{"bullets"} and ref $d->{"bullets"} ne "ARRAY"
!ll&&!rl&&r
300
MC/DC0%0/2
exists $d->{"bullets"} and ref $d->{"bullets"} ne "ARRAY"
exists $d->{"bullets"} ref $d->{"bullets"} ne "ARRAY"
Truth table33%1/3
A and B
A= exists $d->{"bullets"}B= ref $d->{"bullets"} ne "ARRAY"
ABresult
0X0
100
111
120 0 croak "'bullets' must be an arrayref if provided";
121 }
122 3 ▶ if (exists $d->{numbered}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $d->{"numbered"}) 0 3 3
123 0 my $v = $d->{numbered};
124 0 ▶ croak "'numbered' must be a boolean if provided"
Branch: 1 branch
BranchTrueFalseTotal
if ref $v and not JSON::PP::is_bool($v) 0 0 0
Condition0%0/3
ref $v and not JSON::PP::is_bool($v)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $v and not JSON::PP::is_bool($v)
ref $v not JSON::PP::is_bool($v)
Truth table0%0/3
A and not(B)
A= ref $vB= not JSON::PP::is_bool($v)
ABresult
0X0
100
111
125 if ref $v && !( JSON::PP::is_bool($v) );
126 }
127 3 ▶ if (exists $d->{numberedBullets}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $d->{"numberedBullets"}) 0 3 3
128 0 my $v = $d->{numberedBullets};
129 0 ▶ croak "'numberedBullets' must be a boolean if provided"
Branch: 1 branch
BranchTrueFalseTotal
if ref $v and not JSON::PP::is_bool($v) 0 0 0
Condition0%0/3
ref $v and not JSON::PP::is_bool($v)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $v and not JSON::PP::is_bool($v)
ref $v not JSON::PP::is_bool($v)
Truth table0%0/3
A and not(B)
A= ref $vB= not JSON::PP::is_bool($v)
ABresult
0X0
100
111
130 if ref $v && !( JSON::PP::is_bool($v) );
131 }
132  
133 3 ▶ my $has_body = (exists $d->{body} && defined $d->{body} && length $d->{body}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $d->{'body'} && defined $d->{'body'} && length $d->{'body'} ? : 3 0 3
Condition33%2/6
exists $d->{"body"} and defined $d->{"body"} and length $d->{"body"}
!ll&&!rl&&r
003
exists $d->{"body"} and defined $d->{"body"}
!ll&&!rl&&r
003
MC/DC0%0/3
exists $d->{"body"} and defined $d->{"body"} and length $d->{"body"}
exists $d->{"body"} defined $d->{"body"} length $d->{"body"}
Truth table25%1/4
A and B and C
A= exists $d->{"body"}B= defined $d->{"body"}C= length $d->{"body"}
ABCresult
0XX0
10X0
1100
1111
134 0 ▶ my $has_bullets = (exists $d->{bullets} && @{ $d->{bullets} || [] }) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $d->{'bullets'} && @{$d->{'bullets'} or [];} ? : 0 3 3
Condition20%1/5
exists $d->{"bullets"} and @{$d->{"bullets"} or [];}
!ll&&!rl&&r
300
$d->{'bullets'} || []
l!l
00
MC/DC0%0/2
exists $d->{"bullets"} and @{$d->{"bullets"} or [];}
exists $d->{"bullets"} @{$d->{"bullets"} or [];}
MC/DC0%0/1
$d->{'bullets'} || []
$d->{'bullets'}
Truth table33%1/3
A and B
A= exists $d->{"bullets"}B= @{$d->{"bullets"} or [];}
ABresult
0X0
100
111
Truth table0%0/2
A
A= $d->{'bullets'}
Aresult
11
00
135 0 ▶ my $has_subsections = (exists $d->{subsections} && @{ $d->{subsections} || [] }) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $d->{'subsections'} && @{$d->{'subsections'} or [];} ? : 0 3 3
Condition20%1/5
exists $d->{"subsections"} and @{$d->{"subsections"} or [];}
!ll&&!rl&&r
300
$d->{'subsections'} || []
l!l
00
MC/DC0%0/2
exists $d->{"subsections"} and @{$d->{"subsections"} or [];}
exists $d->{"subsections"} @{$d->{"subsections"} or [];}
MC/DC0%0/1
$d->{'subsections'} || []
$d->{'subsections'}
Truth table33%1/3
A and B
A= exists $d->{"subsections"}B= @{$d->{"subsections"} or [];}
ABresult
0X0
100
111
Truth table0%0/2
A
A= $d->{'subsections'}
Aresult
11
00
136 3 ▶ if (!$has_body && !$has_bullets && !$has_subsections) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_body or $has_bullets or $has_subsections) 0 3 3
Condition33%2/6
$has_body or $has_bullets or $has_subsections
l!l&&r!l&&!r
300
$has_body or $has_bullets
l!l&&r!l&&!r
300
MC/DC0%0/3
$has_body or $has_bullets or $has_subsections
$has_body $has_bullets $has_subsections
Truth table25%1/4
A or B or C
A= $has_bodyB= $has_bulletsC= $has_subsections
ABCresult
1XX1
01X1
0011
0000
137 0 croak "All sections must have either a non-empty body, non-empty bullets, or subsections";
138 }
139  
140 3 ▶ if ($is_subsection && !exists $d->{title}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_subsection and not exists $d->{"title"}) 0 3 3
Condition33%1/3
$is_subsection and not exists $d->{"title"}
!ll&&!rl&&r
300
MC/DC0%0/2
$is_subsection and not exists $d->{"title"}
$is_subsection not exists $d->{"title"}
Truth table33%1/3
A and not(B)
A= $is_subsectionB= not exists $d->{"title"}
ABresult
0X0
100
111
141 0 croak "All subsections must have a title";
142 }
143  
144 my %args = (
145 title => $d->{title},
146 body => exists $d->{body} ? $d->{body} : '',
147 0 ▶ bullets => exists $d->{bullets} ? [ @{ $d->{bullets} } ] : [],
Branch: 2 branches
BranchTrueFalseTotal
exists $d->{'body'} ? : 3 0 3
exists $d->{'bullets'} ? : 0 3 3
148 );
149 3 ▶ if (exists $d->{numbered}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $d->{"numbered"}) 0 3 3
150 0 ▶ $args{numbered} = $d->{numbered} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$d->{'numbered'} ? : 0 0 0
151 }
152 3 ▶ if (exists $d->{numberedBullets}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $d->{"numberedBullets"}) 0 3 3
153 0 ▶ $args{numberedBullets} = $d->{numberedBullets} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$d->{'numberedBullets'} ? : 0 0 0
154 }
155  
156 3 my $section = SignalWire::POM::Section->new(%args);
157 3 ▶ for my $sub (@{ $d->{subsections} || [] }) {
Condition50%1/2
$d->{'subsections'} || []
l!l
03
MC/DC0%0/1
$d->{'subsections'} || []
$d->{'subsections'}
Truth table50%1/2
A
A= $d->{'subsections'}
Aresult
11
00
158 0 push @{ $section->subsections }, _build_section_from_hash($sub, 1);
159 }
160 3 return $section;
161 }
162  
163 # ---------- public methods ----------
164  
165 # Append a top-level section to the model and return the new Section
166 # object. ``title`` may be undef ONLY for the very first section (this
167 # is the Python contract — the first section of a POM may render
168 # title-less prose).
169 1 ▶sub add_section {
Subroutine add_section: called CC=9 SCAR=27.5
Pod undocumented
170 1 my ($self, %opts) = @_;
171  
172 0 ▶ if (!defined $opts{title} && @{ $self->sections } > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opts{"title"} and @{$self->sections;} > 0) 0 1 1
Condition33%1/3
not defined $opts{"title"} and @{$self->sections;} > 0
!ll&&!rl&&r
100
MC/DC0%0/2
not defined $opts{"title"} and @{$self->sections;} > 0
not defined $opts{"title"} @{$self->sections;} > 0
Truth table33%1/3
not(A) and B
A= not defined $opts{"title"}B= @{$self->sections;} > 0
ABresult
0X0
100
111
173 0 croak "Only the first section can have no title";
174 }
175  
176 # Python accepts either a single string OR a list for ``bullets``;
177 # normalize to a list here.
178 1 my $bullets = $opts{bullets};
179 1 ▶ if (defined $bullets && !ref $bullets) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $bullets and not ref $bullets) { } 0 1 1
elsif (not defined $bullets) { } 1 0 1
Condition33%1/3
defined $bullets and not ref $bullets
!ll&&!rl&&r
100
MC/DC0%0/2
defined $bullets and not ref $bullets
defined $bullets not ref $bullets
Truth table33%1/3
A and not(B)
A= defined $bulletsB= not ref $bullets
ABresult
0X0
100
111
180 0 $bullets = [ $bullets ];
181 } elsif (!defined $bullets) {
182 1 $bullets = [];
183 }
184  
185 my $section = SignalWire::POM::Section->new(
186 title => $opts{title},
187 body => exists $opts{body} ? $opts{body} : '',
188 bullets => $bullets,
189 numbered => exists $opts{numbered} ? $opts{numbered} : undef,
190 1 ▶ numberedBullets => exists $opts{numberedBullets} ? $opts{numberedBullets} : 0,
Branch: 3 branches
BranchTrueFalseTotal
exists $opts{'body'} ? : 1 0 1
exists $opts{'numbered'} ? : 0 1 1
exists $opts{'numberedBullets'} ? : 0 1 1
191 );
192 1 push @{ $self->sections }, $section;
193 1 return $section;
194 }
195  
196 # Recursive depth-first search by title. Returns the first matching
197 # Section (or undef). Mirrors Python's find_section.
198 0 ▶sub find_section {
Subroutine find_section: not called CC=1 SCAR=6.9
Pod undocumented
199 0 my ($self, $title) = @_;
200 0 return _find_in($self->sections, $title);
201 }
202  
203 0 ▶sub _find_in {
Subroutine _find_in: not called CC=5 SCAR=34.0
204 0 my ($sections, $title) = @_;
205 0 for my $s (@$sections) {
206 0 ▶ return $s if defined $s->title && $s->title eq $title;
Branch: 1 branch
BranchTrueFalseTotal
if defined $s->title and $s->title eq $title 0 0 0
Condition0%0/3
defined $s->title and $s->title eq $title
!ll&&!rl&&r
000
MC/DC0%0/2
defined $s->title and $s->title eq $title
defined $s->title $s->title eq $title
Truth table0%0/3
A and B
A= defined $s->titleB= $s->title eq $title
ABresult
0X0
100
111
207 0 my $found = _find_in($s->subsections, $title);
208 0 ▶ return $found if defined $found;
Branch: 1 branch
BranchTrueFalseTotal
if defined $found 0 0 0
209 }
210 0 return undef;
211 }
212  
213 # JSON-encoded representation. Output is byte-for-byte identical to
214 # Python's ``PromptObjectModel.to_json`` (2-space indent, no trailing
215 # newline, key order title/body/bullets/subsections/numbered/
216 # numberedBullets).
217 0 ▶sub to_json {
Subroutine to_json: not called CC=1 SCAR=6.9
Pod undocumented
218 0 my ($self) = @_;
219 0 my $j = JSON::PP->new
220 ->indent(1)
221 ->indent_length(2)
222 ->space_after
223 ->canonical(0);
224 0 my $out = $j->encode($self->to_hash);
225 # JSON::PP appends ``\n`` in indent mode; Python's ``json.dumps`` does not.
226 0 chomp $out;
227 0 return $out;
228 }
229  
230 # YAML-encoded representation. Output matches Python's
231 # ``yaml.dump(..., default_flow_style=False, sort_keys=False)``.
232 0 ▶sub to_yaml {
Subroutine to_yaml: not called CC=1 SCAR=6.9
Pod undocumented
233 0 my ($self) = @_;
234 0 require YAML::PP;
235 0 require YAML::PP::Common;
236 0 my $yp = YAML::PP->new(
237 header => 0,
238 preserve => YAML::PP::Common::PRESERVE_ORDER(),
239 );
240 0 return $yp->dump_string($self->to_hash);
241 }
242  
243 # Convert to an arrayref of section hashes (one per top-level section).
244 # Each Section is itself converted via Section::to_hash, preserving the
245 # Python key order so JSON / YAML output is stable.
246 0 ▶sub to_hash {
Subroutine to_hash: not called CC=1 SCAR=6.9
Pod undocumented
247 0 my ($self) = @_;
248 0 return [ map { $_->to_hash } @{ $self->sections } ];
249 }
250  
251 # Render the entire POM as Markdown. Auto-numbers top-level sections
252 # when any sibling has ``numbered => 1``. Mirrors Python's
253 # ``PromptObjectModel.render_markdown``.
254 0 ▶sub render_markdown {
Subroutine render_markdown: not called CC=8 SCAR=42.8
Pod undocumented
255 0 my ($self) = @_;
256  
257 0 my $any_section_numbered = 0;
258 0 for my $sec (@{ $self->sections }) {
259 0 ▶ if ($sec->numbered) { $any_section_numbered = 1; last; }
Branch: 1 branch
BranchTrueFalseTotal
if ($sec->numbered) 0 0 0
260 }
261  
262 0 my @md;
263 0 my $section_counter = 0;
264 0 for my $sec (@{ $self->sections }) {
265 0 my $section_number;
266 0 ▶ if (defined $sec->title) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sec->title) { } 0 0 0
267 0 $section_counter++;
268 0 ▶ if ($any_section_numbered
Branch: 1 branch
BranchTrueFalseTotal
if ($any_section_numbered and not defined $sec->numbered && !$sec->numbered) { } 0 0 0
Condition0%0/6
$any_section_numbered and not defined $sec->numbered && !$sec->numbered
!ll&&!rl&&r
000
defined $sec->numbered && !$sec->numbered
!ll&&!rl&&r
000
MC/DC0%0/3
$any_section_numbered and not defined $sec->numbered && !$sec->numbered
$any_section_numbered defined $sec->numbered !$sec->numbered
Truth table0%0/4
A and not(B && not(C))
A= $any_section_numberedB= defined $sec->numberedC= !$sec->numbered
ABCresult
0XX0
1110
10X1
1101
269 && !(defined $sec->numbered && !$sec->numbered)) {
270 0 $section_number = [ $section_counter ];
271 } else {
272 0 $section_number = [];
273 }
274 } else {
275 0 $section_number = [];
276 }
277 0 push @md, $sec->render_markdown(2, $section_number);
278 }
279 0 return join("\n", @md);
280 }
281  
282 # Render the entire POM as XML. Wraps a series of <section> elements in
283 # a <prompt> root with the standard XML declaration. Mirrors Python's
284 # ``PromptObjectModel.render_xml``.
285 0 ▶sub render_xml {
Subroutine render_xml: not called CC=8 SCAR=42.8
Pod undocumented
286 0 my ($self) = @_;
287  
288 0 my @xml = (
289 '<?xml version="1.0" encoding="UTF-8"?>',
290 '<prompt>',
291 );
292  
293 0 my $any_section_numbered = 0;
294 0 for my $sec (@{ $self->sections }) {
295 0 ▶ if ($sec->numbered) { $any_section_numbered = 1; last; }
Branch: 1 branch
BranchTrueFalseTotal
if ($sec->numbered) 0 0 0
296 }
297  
298 0 my $section_counter = 0;
299 0 for my $sec (@{ $self->sections }) {
300 0 my $section_number;
301 0 ▶ if (defined $sec->title) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sec->title) { } 0 0 0
302 0 $section_counter++;
303 0 ▶ if ($any_section_numbered
Branch: 1 branch
BranchTrueFalseTotal
if ($any_section_numbered and not defined $sec->numbered && !$sec->numbered) { } 0 0 0
Condition0%0/6
$any_section_numbered and not defined $sec->numbered && !$sec->numbered
!ll&&!rl&&r
000
defined $sec->numbered && !$sec->numbered
!ll&&!rl&&r
000
MC/DC0%0/3
$any_section_numbered and not defined $sec->numbered && !$sec->numbered
$any_section_numbered defined $sec->numbered !$sec->numbered
Truth table0%0/4
A and not(B && not(C))
A= $any_section_numberedB= defined $sec->numberedC= !$sec->numbered
ABCresult
0XX0
1110
10X1
1101
304 && !(defined $sec->numbered && !$sec->numbered)) {
305 0 $section_number = [ $section_counter ];
306 } else {
307 0 $section_number = [];
308 }
309 } else {
310 0 $section_number = [];
311 }
312 0 push @xml, $sec->render_xml(1, $section_number);
313 }
314  
315 0 push @xml, '</prompt>';
316 0 return join("\n", @xml);
317 }
318  
319 # Append the sections of another POM as subsections of a section in
320 # this POM, identified either by title (string lookup) or by passing a
321 # Section object directly. Mirrors Python's add_pom_as_subsection.
322 0 ▶sub add_pom_as_subsection {
Subroutine add_pom_as_subsection: not called CC=8 SCAR=42.8
Pod undocumented
323 0 my ($self, $target, $pom_to_add) = @_;
324 0 my $target_section;
325 0 ▶ if (!ref $target) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $target) { } 0 0 0
elsif (builtin::blessed($target) and $target->isa('SignalWire::POM::Section')) { } 0 0 0
326 0 $target_section = $self->find_section($target);
327 0 ▶ croak "No section with title '${target}' found." unless $target_section;
Branch: 1 branch
BranchTrueFalseTotal
unless $target_section 0 0 0
328 ▶ } elsif (blessed($target) && $target->isa('SignalWire::POM::Section')) {
Condition0%0/3
builtin::blessed($target) and $target->isa("SignalWire::POM::Section")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($target) and $target->isa("SignalWire::POM::Section")
builtin::blessed($target) $target->isa("SignalWire::POM::Section")
Truth table0%0/3
A and B
A= builtin::blessed($target)B= $target->isa("SignalWire::POM::Section")
ABresult
0X0
100
111
329 0 $target_section = $target;
330 } else {
331 0 croak "Target must be a string or a SignalWire::POM::Section object.";
332 }
333  
334 0 ▶ croak "pom_to_add must be a SignalWire::POM::PromptObjectModel"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($pom_to_add) and $pom_to_add->isa("SignalWire::POM::PromptObjectModel") 0 0 0
Condition0%0/3
builtin::blessed($pom_to_add) and $pom_to_add->isa("SignalWire::POM::PromptObjectModel")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($pom_to_add) and $pom_to_add->isa("SignalWire::POM::PromptObjectModel")
builtin::blessed($pom_to_add) $pom_to_add->isa("SignalWire::POM::PromptObjectModel")
Truth table0%0/3
A and B
A= builtin::blessed($pom_to_add)B= $pom_to_add->isa("SignalWire::POM::PromptObjectModel")
ABresult
0X0
100
111
335 unless blessed($pom_to_add)
336 && $pom_to_add->isa('SignalWire::POM::PromptObjectModel');
337  
338 0 for my $sec (@{ $pom_to_add->sections }) {
339 0 push @{ $target_section->subsections }, $sec;
340 }
341 0 return $self;
342 }
343  
344 1 1;
345  
346 __END__
« blib/lib/SignalWire/POM/Section.pm ↑ Summary blib/lib/SignalWire/Skills/Builtin/WebSearch.pm »