blib/lib/Lib/Pepper/OptionList.pm

filter: Statementstmt 32.3% 43 / 133 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 53.8% 14 / 26 Podpod 100% 11 / 11 totaltotal 29.9% 68 / 227 SCAR 58.7
CC
27
Cov
23%
fromHashref
72.0
addChild
30.0
toHashref
30.0
CRAP 353.4
« blib/lib/Lib/Pepper/Instance.pm ↑ Summary blib/lib/Lib/Pepper.pm »
1 package Lib::Pepper::OptionList;
2 #---AUTOPRAGMASTART---
3 5 ▶use v5.42;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use diagnostics;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Carp qw[carp croak confess cluck longmess shortmess];
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 our $VERSION = 0.5;
10 5 ▶use autodie qw( close );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Array::Contains;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Data::Printer;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 #---AUTOPRAGMAEND---
16  
17  
18 5 ▶use Lib::Pepper;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use Lib::Pepper::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use Lib::Pepper::Constants qw(:special);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 0 ▶sub new($class, $handle = undef) {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
23 0 my $self = {
24 handle => undef,
25 owned => 0,
26 };
27  
28 0 bless $self, $class;
29  
30 0 ▶ if(defined $handle) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $handle) { } 0 0 0
31 # Wrap an existing handle
32 0 $self->{handle} = $handle;
33 0 $self->{owned} = 0;
34 } else {
35 # Create a new option list
36 0 my ($result, $newHandle) = Lib::Pepper::pepOptionListCreate();
37 0 Lib::Pepper::Exception->checkResult($result, 'pepOptionListCreate');
38 0 $self->{handle} = $newHandle;
39 0 $self->{owned} = 1;
40 }
41  
42 0 return $self;
43 }
44  
45 ▶sub getHandle($self) {
Subroutine getHandle: not called CC=1 SCAR=6.9
Pod documented
46 0 return $self->{handle};
47 }
48  
49 ▶sub getString($self, $key) {
Subroutine getString: not called CC=2 SCAR=17.9
Pod documented
50 0 ▶ if(!defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key) 0 0 0
51 0 croak('getString: key parameter is required');
52 }
53  
54 0 my ($result, $value) = Lib::Pepper::pepOptionListGetStringElement($self->{handle}, $key);
55 0 Lib::Pepper::Exception->checkResult($result, "getString('$key')");
56  
57 0 return $value;
58 }
59  
60 ▶sub getInt($self, $key) {
Subroutine getInt: not called CC=2 SCAR=17.9
Pod documented
61 0 ▶ if(!defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key) 0 0 0
62 0 croak('getInt: key parameter is required');
63 }
64  
65 0 my ($result, $value) = Lib::Pepper::pepOptionListGetIntElement($self->{handle}, $key);
66 0 Lib::Pepper::Exception->checkResult($result, "getInt('$key')");
67  
68 0 return $value;
69 }
70  
71 ▶sub getChild($self, $key) {
Subroutine getChild: not called CC=2 SCAR=17.9
Pod documented
72 0 ▶ if(!defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key) 0 0 0
73 0 croak('getChild: key parameter is required');
74 }
75  
76 0 my ($result, $childHandle) = Lib::Pepper::pepOptionListGetChildOptionListElement($self->{handle}, $key);
77 0 Lib::Pepper::Exception->checkResult($result, "getChild('$key')");
78  
79 # Wrap the child handle in a new OptionList object (non-owned)
80 0 return Lib::Pepper::OptionList->new($childHandle);
81 }
82  
83 ▶sub addString($self, $key, $value) {
Subroutine addString: not called CC=3 SCAR=24.8
Pod documented
84 0 ▶ if(!defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key) 0 0 0
85 0 croak('addString: key parameter is required');
86 }
87 0 ▶ if(!defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
88 0 croak('addString: value parameter is required');
89 }
90  
91 0 my $result = Lib::Pepper::pepOptionListAddStringElement($self->{handle}, $key, $value);
92 0 Lib::Pepper::Exception->checkResult($result, "addString('$key')");
93  
94 0 return $self;
95 }
96  
97 ▶sub addInt($self, $key, $value) {
Subroutine addInt: not called CC=3 SCAR=24.8
Pod documented
98 0 ▶ if(!defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key) 0 0 0
99 0 croak('addInt: key parameter is required');
100 }
101 0 ▶ if(!defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
102 0 croak('addInt: value parameter is required');
103 }
104  
105 0 my $result = Lib::Pepper::pepOptionListAddIntElement($self->{handle}, $key, $value);
106 0 Lib::Pepper::Exception->checkResult($result, "addInt('$key')");
107  
108 0 return $self;
109 }
110  
111 ▶sub addChild($self, $key, $childOptionList) {
Subroutine addChild: not called CC=5 SCAR=34.0
Pod documented
112 0 ▶ if(!defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key) 0 0 0
113 0 croak('addChild: key parameter is required');
114 }
115 0 ▶ if(!defined $childOptionList || !ref($childOptionList) || !$childOptionList->isa('Lib::Pepper::OptionList')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $childOptionList and ref $childOptionList and $childOptionList->isa("Lib::Pepper::OptionList")) 0 0 0
Condition0%0/6
defined $childOptionList and ref $childOptionList and $childOptionList->isa("Lib::Pepper::OptionList")
!ll&&!rl&&r
000
defined $childOptionList and ref $childOptionList
!ll&&!rl&&r
000
MC/DC0%0/3
defined $childOptionList and ref $childOptionList and $childOptionList->isa("Lib::Pepper::OptionList")
defined $childOptionList ref $childOptionList $childOptionList->isa("Lib::Pepper::OptionList")
Truth table0%0/4
A and B and C
A= defined $childOptionListB= ref $childOptionListC= $childOptionList->isa("Lib::Pepper::OptionList")
ABCresult
0XX0
10X0
1100
1111
116 0 croak('addChild: childOptionList must be a Lib::Pepper::OptionList object');
117 }
118  
119 0 my $childHandle = $childOptionList->getHandle();
120 0 my $result = Lib::Pepper::pepOptionListAddChildOptionListElement($self->{handle}, $key, $childHandle);
121 0 Lib::Pepper::Exception->checkResult($result, "addChild('$key')");
122  
123 0 return $self;
124 }
125  
126 ▶sub getElementList($self) {
Subroutine getElementList: not called CC=3 SCAR=24.8
Pod documented
127 0 my ($result, $elementList) = Lib::Pepper::pepOptionListGetElementList($self->{handle});
128 0 Lib::Pepper::Exception->checkResult($result, 'getElementList');
129  
130 # Parse comma-separated list
131 0 ▶ if(!defined $elementList || $elementList eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $elementList or $elementList eq "") 0 0 0
Condition0%0/3
not defined $elementList or $elementList eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $elementList or $elementList eq ""
not defined $elementList $elementList eq ""
Truth table0%0/3
not(A) or B
A= not defined $elementListB= $elementList eq ""
ABresult
1X1
011
000
132 0 return [];
133 }
134  
135 0 my @elements = split(/,/, $elementList);
136 0 return \@elements;
137 }
138  
139 ▶sub toHashref($self) {
Subroutine toHashref: not called CC=5 SCAR=34.0
Pod documented
140 0 my $elements = $self->getElementList();
141 0 my $hashref = {};
142  
143 0 for my $key (@{$elements}) {
144 # Try to get as int first
145 0 my ($result, $value) = Lib::Pepper::pepOptionListGetIntElement($self->{handle}, $key);
146  
147 0 ▶ if(Lib::Pepper::Exception->isSuccess($result)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Lib::Pepper::Exception"->isSuccess($result)) 0 0 0
148 0 $hashref->{$key} = $value;
149 0 next;
150 }
151  
152 # Try as string
153 0 ($result, $value) = Lib::Pepper::pepOptionListGetStringElement($self->{handle}, $key);
154  
155 0 ▶ if(Lib::Pepper::Exception->isSuccess($result)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Lib::Pepper::Exception"->isSuccess($result)) 0 0 0
156 0 $hashref->{$key} = $value;
157 0 next;
158 }
159  
160 # Try as child option list
161 0 ($result, my $childHandle) = Lib::Pepper::pepOptionListGetChildOptionListElement($self->{handle}, $key);
162  
163 0 ▶ if(Lib::Pepper::Exception->isSuccess($result)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Lib::Pepper::Exception"->isSuccess($result)) 0 0 0
164 0 my $childList = Lib::Pepper::OptionList->new($childHandle);
165 0 $hashref->{$key} = $childList->toHashref();
166 0 next;
167 }
168  
169 # If we get here, we couldn't get the value
170 0 carp("Warning: Could not retrieve value for key '$key'");
171 }
172  
173 0 return $hashref;
174 }
175  
176 ▶sub fromHashref($class, $hashref) {
Subroutine fromHashref: not called CC=8 SCAR=42.8
Pod documented
177 0 ▶ if(!defined $hashref || ref($hashref) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $hashref or ref $hashref ne "HASH") 0 0 0
Condition0%0/3
not defined $hashref or ref $hashref ne "HASH"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $hashref or ref $hashref ne "HASH"
not defined $hashref ref $hashref ne "HASH"
Truth table0%0/3
not(A) or B
A= not defined $hashrefB= ref $hashref ne "HASH"
ABresult
1X1
011
000
178 0 croak('fromHashref: hashref parameter must be a hash reference');
179 }
180  
181 0 my $optionList = $class->new();
182  
183 0 for my $key (sort keys %{$hashref}) {
184 0 my $value = $hashref->{$key};
185  
186 0 ▶ if(!defined $value) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $value) { } 0 0 0
elsif (ref $value eq 'HASH') { } 0 0 0
elsif (ref $value eq '') { } 0 0 0
187 # Skip undefined values
188 0 next;
189 } elsif(ref($value) eq 'HASH') {
190 # Nested hash - create child option list
191 0 my $childList = $class->fromHashref($value);
192 0 $optionList->addChild($key, $childList);
193 } elsif(ref($value) eq '') {
194 # Scalar value - use key prefix to determine type: i=int, s=string, h=handle/child
195 0 ▶ if($key =~ /^i/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^i/u) { } 0 0 0
196 0 $optionList->addInt($key, $value);
197 } else {
198 0 $optionList->addString($key, $value);
199 }
200 } else {
201 0 croak("fromHashref: unsupported value type for key '$key': " . ref($value));
202 }
203 }
204  
205 0 return $optionList;
206 }
207  
208 ▶sub DESTROY($self) {
Subroutine DESTROY: not called CC=1 SCAR=6.9
209 # Note: We don't free the handle here because the Pepper library
210 # manages option list memory internally. Option lists are typically
211 # freed when the instance they're associated with is freed.
212 # Only free if we explicitly own it (future enhancement)
213 0 return;
214 }
215  
216 5 1;
217  
218 __END__
« blib/lib/Lib/Pepper/Instance.pm ↑ Summary blib/lib/Lib/Pepper.pm »