blib/lib/DDC/PP/Object.pm

filter: Statementstmt 86.4% 128 / 148 Branchbran 65.6% 42 / 64 Conditioncond 56.8% 29 / 51 MC/DCmcdc 22.5% 7 / 31 Subroutinesub 84.3% 27 / 32 Podpod 0.0% 0 / 21 totaltotal 67.1% 233 / 347 CC 52 SCAR 20.4
CC
52
Cov
76%
mapVisit
18.1
newFromHash
14.8
valFromPerl
7.0
CRAP 7.7
« blib/lib/DDC/PP/CQuery.pm ↑ Summary blib/lib/DDC/HitList.pm »
1 ##-*- Mode: CPerl -*-
2  
3 ##======================================================================
4 package DDC::PP::Object;
5 20 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 ▶use Carp qw(carp confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 ##======================================================================
10 ## debugging & wrapping utilities
11  
12 ## undef = $CLASS->nomethod($method_name)
13 ## + defines a method $CLASS::$method_name which just throws an error
14 100 ▶sub nomethod {
Subroutine nomethod: called CC=1 SCAR=0.0
Pod undocumented
15 100 my ($class,$method_name) = @_;
16 100 my $method = "${class}::${method_name}";
17 20 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
18 0 ▶ *$method = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
19 0 confess("${method}(): method not implemented");
20 100 };
21 }
22  
23 ## undef = $CLASS->defprop($property)
24 ## + defines $CLASS::get$Property and $CLASS::set$Property methods
25 1240 ▶sub defprop {
Subroutine defprop: called CC=1 SCAR=0.0
Pod undocumented
26 1240 my ($class,$prop)=@_;
27 1240 my $getmethod = "${class}::get".ucfirst($prop);
28 1240 my $setmethod = "${class}::set".ucfirst($prop);
29 20 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1240 ▶ *$getmethod = sub { return $_[0]{$prop}; };
Subroutine __ANON__: called CC=1 SCAR=0.0
31 1240 ▶ *$setmethod = sub { return $_[0]{$prop}=$_[1]; };
Subroutine __ANON__: called CC=1 SCAR=0.0
32 }
33  
34 ## undef = $CLASS->defalias($propertyFrom,$propertyTo, $doGet=1, $doSet=1)
35 ## + aliases $CLASS::get$PropertyFrom and $CLASS::set$PropertyFrom methods to $CLASS::get$PropertyTo etc.
36 160 ▶sub defalias {
Subroutine defalias: called CC=5 SCAR=17.8
Pod undocumented
37 160 my ($class,$pfrom,$pto, $doGet,$doSet)=@_;
38 160 my $getmethod = "${class}::get".ucfirst($pfrom);
39 160 my $setmethod = "${class}::set".ucfirst($pfrom);
40 20 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 160 ▶ *$getmethod = $class->can('get'.ucfirst($pto)) if (!defined($doGet) || $doGet);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $doGet or $doGet 0 160 160
Condition33%1/3
not defined $doGet or $doGet
l!l&&r!l&&!r
00160
MC/DC0%0/2
not defined $doGet or $doGet
not defined $doGet $doGet
Truth table33%1/3
not(A) or B
A= not defined $doGetB= $doGet
ABresult
1X1
011
000
42 160 ▶ *$setmethod = $class->can('set'.ucfirst($pto)) if (!defined($doSet) || $doSet);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $doSet or $doSet 160 0 160
Condition33%1/3
not defined $doSet or $doSet
l!l&&r!l&&!r
01600
MC/DC0%0/2
not defined $doSet or $doSet
not defined $doSet $doSet
Truth table33%1/3
not(A) or B
A= not defined $doSetB= $doSet
ABresult
1X1
011
000
43 }
44  
45 ##======================================================================
46 ## xs replacements
47  
48 913 ▶sub new {
Subroutine new: called CC=2 SCAR=8.1
Pod undocumented
49 913 my $that = shift;
50 913 ▶ return bless { @_ }, ref($that)||$that;
Condition33%1/3
ref $that || $that
l!l&&r!l&&!r
09130
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table33%1/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
51 }
52  
53 20 __PACKAGE__->nomethod('DumpTree');
54 20 __PACKAGE__->nomethod('refcnt');
55 20 __PACKAGE__->nomethod('self');
56 20 __PACKAGE__->nomethod('free');
57  
58 #__PACKAGE__->nomethod('Children');
59 # + override this if order is important (e.g. for DiaCollo CQWith, CQAnd, etc.)
60 8 ▶sub Children {
Subroutine Children: called CC=2 SCAR=7.1
Pod undocumented
61 8 ▶ return UNIVERSAL::isa($_[0],'HASH') ? [grep {UNIVERSAL::isa($_,'DDC::PP::Object')} values %{$_[0]}] : [];
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], 'HASH') ? : 8 0 8
62 }
63  
64 #__PACKAGE__->nomethod('Descendants');
65 2 ▶sub Descendants {
Subroutine Descendants: called CC=4 SCAR=13.9
Pod undocumented
66 2 my @stack = (shift);
67 2 my %visited = qw();
68 2 my @kids = qw();
69 2 my ($obj);
70 2 ▶ while (@stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 14 2 16
71 14 $obj = shift(@stack);
72 14 ▶ next if (exists $visited{$obj});
Branch: 1 branch
BranchTrueFalseTotal
if exists $visited{$obj} 0 14 14
73 14 push(@kids,$obj);
74 14 $visited{$obj} = undef;
75 14 ▶ unshift(@stack, @{$obj->Children}) if (ref($obj));
Branch: 1 branch
BranchTrueFalseTotal
if ref $obj 14 0 14
76 }
77 2 return \@kids;
78 }
79  
80 #__PACKAGE__->nomethod('DisownChildren');
81 0 ▶sub DisownChildren {
Subroutine DisownChildren: not called CC=2 SCAR=17.9
Pod undocumented
82 0 my $obj = shift;
83 0 ▶ return if (!ref($obj));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj 0 0 0
84 0 delete @$obj{$obj->members};
85 }
86  
87 #__PACKAGE__->nomethod('toString');
88 0 ▶sub toString {
Subroutine toString: not called CC=1 SCAR=6.9
Pod undocumented
89 0 return "$_[0]";
90 }
91  
92 168 ▶sub toJson {
Subroutine toJson: called CC=1 SCAR=0.0
Pod undocumented
93 168 return JSON::to_json( $_[0], {utf8=>1,pretty=>0,canonical=>1,allow_blessed=>1,convert_blessed=>1} );
94 }
95  
96 ##-- json utils
97 212 ▶sub jsonClass {
Subroutine jsonClass: called CC=2 SCAR=8.1
Pod undocumented
98 212 ▶ (my $class = ref($_[0]) || $_[0]) =~ s/^DDC::PP:://;
Condition33%1/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
21200
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
99 212 return $class;
100 }
101  
102  
103 ##======================================================================
104 ## Traversal
105  
106 ##--------------------------------------------------------------
107 ## $obj = $obj->mapTraverse(\&CODE)
108 ## + calls \&CODE on $obj and each DDC::PP::Object descendant in turn
109 ## + \&CODE is called as \&CODE->($obj), and should return a new value for the corresponding slot
110 ## + object tree is traversed in depth-first visit-last order
111 10 ▶sub mapTraverse {
Subroutine mapTraverse: called CC=1 SCAR=0.0
Pod undocumented
112 10 my ($obj,$code) = @_;
113 10 return $obj->mapVisit($obj,$code);
114 }
115  
116 ## $oldval = CLASS->mapVisit($curval, \$code)
117 134 ▶sub mapVisit {
Subroutine mapVisit: called CC=15 SCAR=29.0
Pod undocumented
118 134 my ($that,$nod,$code) = @_;
119 134 ▶ if (#UNIVERSAL::isa($nod,'DDC::PP::Object') ##-- breaks DDC::Any
Branch: 3 branches
BranchTrueFalseTotal
if (ref $nod and UNIVERSAL::can($nod, 'members')) { } 10 124 134
elsif (ref $nod and UNIVERSAL::isa($nod, 'ARRAY')) { } 32 92 124
elsif (ref $nod and UNIVERSAL::isa($nod, 'HASH')) { } 0 184 184
Condition100%3/3
ref $nod and UNIVERSAL::can($nod, "members")
!ll&&!rl&&r
923210
MC/DC100%2/2
ref $nod and UNIVERSAL::can($nod, "members")
ref $nod UNIVERSAL::can($nod, "members")
Truth table100%3/3
A and B
A= ref $nodB= UNIVERSAL::can($nod, "members")
ABresult
0X0
100
111
120 ref($nod) && UNIVERSAL::can($nod,'members')
121 ) {
122 10 my ($oldval,$newval);
123 10 foreach my $slot (grep {$nod->can("get$_")} $nod->members) {
124 80 $oldval = $nod->can("get${slot}")->($nod);
125 80 $newval = $that->mapVisit($oldval, $code);
126 80 ▶ $nod->can("set${slot}")->($nod,$newval) if ((defined($newval) && defined($oldval) && $newval ne $oldval)
Branch: 1 branch
BranchTrueFalseTotal
if defined $newval and defined $oldval and $newval ne $oldval or defined $newval or defined $oldval 70 10 80
Condition83%10/12
defined $newval and defined $oldval and $newval ne $oldval or defined $newval or defined $oldval
l!l&&r!l&&!r
70010
defined $newval and defined $oldval and $newval ne $oldval or defined $newval
l!l&&r!l&&!r
284210
defined $newval and defined $oldval and $newval ne $oldval
!ll&&!rl&&r
104228
defined $newval and defined $oldval
!ll&&!rl&&r
10070
MC/DC40%2/5
defined $newval and defined $oldval and $newval ne $oldval or defined $newval or defined $oldval
defined $newval defined $oldval $newval ne $oldval defined $newval defined $oldval
Truth table30%3/10
A and B and C or D or E
A= defined $newvalB= defined $oldvalC= $newval ne $oldvalD= defined $newvalE= defined $oldval
ABCDEresult
111XX1
0XX1X1
10X1X1
1101X1
0XX011
10X011
110011
0XX000
10X000
110000
127 || defined($newval)
128 || defined($oldval));
129 }
130 10 return $code->($nod);
131 }
132 ▶ elsif (ref($nod) && UNIVERSAL::isa($nod,'ARRAY')) {
Condition66%2/3
ref $nod and UNIVERSAL::isa($nod, "ARRAY")
!ll&&!rl&&r
92032
MC/DC50%1/2
ref $nod and UNIVERSAL::isa($nod, "ARRAY")
ref $nod UNIVERSAL::isa($nod, "ARRAY")
Truth table66%2/3
A and B
A= ref $nodB= UNIVERSAL::isa($nod, "ARRAY")
ABresult
0X0
100
111
133 32 my $newval = [grep {defined($_)} map {$that->mapVisit($_,$code)} @$nod];
134 32 ▶ return ref($newval) eq 'ARRAY' ? $newval : bless($newval, ref($nod));
Branch: 1 branch
BranchTrueFalseTotal
ref $newval eq 'ARRAY' ? : 32 0 32
135 }
136 ▶ elsif (ref($nod) && UNIVERSAL::isa($nod,'HASH')) {
Condition33%1/3
ref $nod and UNIVERSAL::isa($nod, "HASH")
!ll&&!rl&&r
9200
MC/DC0%0/2
ref $nod and UNIVERSAL::isa($nod, "HASH")
ref $nod UNIVERSAL::isa($nod, "HASH")
Truth table33%1/3
A and B
A= ref $nodB= UNIVERSAL::isa($nod, "HASH")
ABresult
0X0
100
111
137 0 my $newval = {map {($_=>$that->mapVisit($nod->{$_},$code))} keys %$nod};
138 0 ▶ return ref($newval) eq 'HASH' ? $newval : bless($newval, ref($nod));
Branch: 1 branch
BranchTrueFalseTotal
ref $newval eq 'HASH' ? : 0 0 0
139 }
140 92 return $nod;
141 }
142  
143  
144 ##======================================================================
145 ## C->Perl
146  
147 ##--------------------------------------------------------------
148 ## \%hash = $obj->toHash(%opts)
149 ## + %opts:
150 ## (
151 ## trimClassNames => $bool, ##-- auto-trim class-names?
152 ## json => $bool, ##-- for JSON-ification?
153 ## )
154 ## + returns an object as a (nested) perl hash
155 ## + pure-perl variant just returns object
156 316 ▶sub toHash {
Subroutine toHash: called CC=5 SCAR=16.7
Pod undocumented
157 316 my ($obj,%opts) = @_;
158 316 ▶ return $obj if (!defined($obj) && !ref($obj));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj or ref $obj 0 316 316
Condition33%1/3
defined $obj or ref $obj
l!l&&r!l&&!r
31600
MC/DC0%0/2
defined $obj or ref $obj
defined $obj ref $obj
Truth table33%1/3
A or B
A= defined $objB= ref $obj
ABresult
1X1
011
000
159 316 my $class = ref($obj);
160 316 ▶ $class =~ s/^DDC::(?:XS|PP|Any)::// if ($opts{trimClassNames} || $opts{json}); ##-- use toJson()-style class names
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"trimClassNames"} or $opts{"json"} 310 6 316
Condition66%2/3
$opts{"trimClassNames"} or $opts{"json"}
l!l&&r!l&&!r
03106
MC/DC50%1/2
$opts{"trimClassNames"} or $opts{"json"}
$opts{"trimClassNames"} $opts{"json"}
Truth table66%2/3
A or B
A= $opts{"trimClassNames"}B= $opts{"json"}
ABresult
1X1
011
000
161 return {
162 (map {
163 2162 ( $_ => $obj->valToPerl($obj->can("get$_")->($obj),%opts) )
164 } grep {
165 316 $obj->can("get$_")
166 } $obj->members),
167 class => $class,
168 };
169 }
170  
171 ##--------------------------------------------------------------
172 ## $perlval = $CLASS_OR_OBJECT->valToPerl($cval,%opts)
173 ## + %opts: as for toHash()
174 ## + returns a perl-encoded representation of $cval
175 2378 ▶sub valToPerl {
Subroutine valToPerl: called CC=5 SCAR=17.3
Pod undocumented
176 2378 my ($that,$cval,%opts) = @_;
177 2378 ▶ if (!ref($cval)) {
Branch: 4 branches
BranchTrueFalseTotal
if (not ref $cval) { } 1630 748 2378
elsif (UNIVERSAL::can($cval, 'toHash')) { } 334 414 748
elsif (UNIVERSAL::isa($cval, 'HASH')) { } 0 414 414
elsif (UNIVERSAL::isa($cval, 'ARRAY')) { } 414 0 414
178 1630 return $cval;
179 } elsif (UNIVERSAL::can($cval,'toHash')) {
180 334 return $cval->toHash(%opts);
181 } elsif (UNIVERSAL::isa($cval,'HASH')) {
182 0 return {(map {($_=>$that->valToPerl($cval->{$_},%opts))} keys %$cval)};
183 } elsif (UNIVERSAL::isa($cval,'ARRAY')) {
184 414 return [map {$that->valToPerl($_,%opts)} @$cval];
185 }
186 0 return $cval; ##-- CODE- or GLOB-ref?
187 }
188  
189  
190 ##--------------------------------------------------------------
191 ## @classes = $CLASS_OR_OBJ->inherits()
192 ## + returns a list of all classes from which $CLASS_OR_OBJ inherits
193 ## + called by toHash()
194 5635 ▶sub inherits {
Subroutine inherits: called CC=2 SCAR=6.9
Pod undocumented
195 20 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
196 5635 my $that = shift;
197 5635 ▶ my $class = ref($that) || $that;
Condition66%2/3
ref $that || $that
l!l&&r!l&&!r
32653090
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table66%2/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
198 5635 return ($class, map {inherits($_)} @{"${class}::ISA"});
199 }
200  
201 ##--------------------------------------------------------------
202 ## @keys = $CLASS_OR_OBJ->members()
203 ## + returns a list of all members with a "set${Key}" method supported by $CLASS_OR_OBJ or any superclasss
204 ## + called by toHash()
205 326 ▶sub members {
Subroutine members: called CC=3 SCAR=11.0
Pod undocumented
206 20 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
207 326 my $that = shift;
208 326 my ($class,$symtab,%keys);
209 326 foreach $class ($that->inherits) {
210 5635 $symtab = \%{"${class}::"};
211 @keys{(
212 7622 grep {exists $symtab->{"set$_"}}
213 5635 ▶ map { /^get([[:upper:]].*)$/ ? $1 : qw() }
Branch: 1 branch
BranchTrueFalseTotal
/^get([[:upper:]].*)$/ ? : 7622 100446 108068
214 keys %$symtab
215 )} = qw();
216 }
217 326 return keys %keys;
218 }
219  
220 ##======================================================================
221 ## Perl->C-like
222  
223 ##--------------------------------------------------------------
224 ## $obj = CLASS->newFromHash(\%hash)
225 ## + creates a C++-like object from a (nested) perl hash
226 6 ▶sub newFromHash {
Subroutine newFromHash: called CC=11 SCAR=27.0
Pod undocumented
227 6 my ($that,$hash) = @_;
228 6 ▶ my $class = ref($that) || $that;
Condition33%1/3
ref $that || $that
l!l&&r!l&&!r
060
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table33%1/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
229 6 ▶ return $hash if (!defined($hash) || UNIVERSAL::isa($hash,$class));
Branch: 1 branch
BranchTrueFalseTotal
if not defined $hash or UNIVERSAL::isa($hash, $class) 0 6 6
Condition33%1/3
not defined $hash or UNIVERSAL::isa($hash, $class)
l!l&&r!l&&!r
006
MC/DC0%0/2
not defined $hash or UNIVERSAL::isa($hash, $class)
not defined $hash UNIVERSAL::isa($hash, $class)
Truth table33%1/3
not(A) or B
A= not defined $hashB= UNIVERSAL::isa($hash, $class)
ABresult
1X1
011
000
230 6 ▶ confess(__PACKAGE__ , "::newFromHash(): argument '$hash' is neither undef, a HASH-ref, nor an object of class $class")
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($hash, "HASH") 0 6 6
231 if (!UNIVERSAL::isa($hash,'HASH'));
232  
233 6 ▶ $class = $hash->{class} if (defined($hash->{class}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $hash->{"class"} 6 0 6
234 6 ▶ $class = "DDC::PP::$class" if ($class !~ /:/); ##-- honor toJson()-style class names
Branch: 1 branch
BranchTrueFalseTotal
unless $class =~ /:/ 0 6 6
235 6 ▶ my $obj = $class->new()
Branch: 1 branch
BranchTrueFalseTotal
my $obj = $class->new or confess(__PACKAGE__, "::newFromHash(): $class->new() failed") 6 0 6
236 or confess(__PACKAGE__, "::newFromHash(): $class->new() failed");
237  
238 6 my ($key,$val,$valobj, $setsub);
239 6 ▶ while (($key,$val) = each %$hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$hash) 44 6 50
240 44 ▶ next if ($key eq 'class');
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "class" 6 38 44
241  
242 38 ▶ if ( !($setsub = $obj->can("set".ucfirst($key))) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($setsub = $obj->can('set' . ucfirst($key))) 0 38 38
243 0 warn(__PACKAGE__, "::newFromHash(): ignoring key '$key' for object of class '$class'");
244 0 next;
245 }
246 38 $valobj = $that->valFromPerl($val);
247 38 $setsub->($obj,$valobj);
248 }
249  
250 6 return $obj;
251 }
252  
253 ##--------------------------------------------------------------
254 ## $cval = $CLASS_OR_OBJECT->valFromPerl($perlval)
255 ## + returns a c-like representation of $perlval
256 40 ▶sub valFromPerl {
Subroutine valFromPerl: called CC=6 SCAR=19.4
Pod undocumented
257 40 my ($that,$pval) = @_;
258 40 ▶ if (!ref($pval)) {
Branch: 4 branches
BranchTrueFalseTotal
if (not ref $pval) { } 22 18 40
elsif (UNIVERSAL::isa($pval, 'HASH') and $pval->{'class'}) { } 4 14 18
elsif (UNIVERSAL::isa($pval, 'HASH')) { } 0 14 14
elsif (UNIVERSAL::isa($pval, 'ARRAY')) { } 14 0 14
259 22 return $pval;
260 ▶ } elsif (UNIVERSAL::isa($pval,'HASH') && $pval->{class}) {
Condition66%2/3
UNIVERSAL::isa($pval, "HASH") and $pval->{"class"}
!ll&&!rl&&r
1404
MC/DC50%1/2
UNIVERSAL::isa($pval, "HASH") and $pval->{"class"}
UNIVERSAL::isa($pval, "HASH") $pval->{"class"}
Truth table66%2/3
A and B
A= UNIVERSAL::isa($pval, "HASH")B= $pval->{"class"}
ABresult
0X0
100
111
261 4 return $that->newFromHash($pval);
262 } elsif (UNIVERSAL::isa($pval,'HASH')) {
263 0 return {(map {($_=>$that->valFromPerl($pval->{$_}))} keys %$pval)};
264 } elsif (UNIVERSAL::isa($pval,'ARRAY')) {
265 14 return [map {$that->valFromPerl($_)} @$pval];
266 }
267 0 return $pval; ##-- CODE- or GLOB-ref?
268 }
269  
270  
271 ##======================================================================
272 ## Clone
273  
274 ## $obj2 = $obj->clone()
275 0 ▶sub clone {
Subroutine clone: not called CC=1 SCAR=6.9
Pod undocumented
276 0 return $_[0]->newFromHash($_[0]->toHash);
277 }
278  
279 ##======================================================================
280 ## JSON
281  
282 ##--------------------------------------------------------------
283 ## $obj = CLASS->newFromJson($json_string,%json_opts)
284 ## + creates a C++ object from a json string
285 0 ▶sub newFromJson {
Subroutine newFromJson: not called CC=1 SCAR=6.9
Pod undocumented
286 0 my ($that,$json,%opts) = @_;
287 0 my $hash = JSON::from_json($json, { utf8=>!utf8::is_utf8($json), relaxed=>1, allow_nonref=>1, %opts });
288 0 return $that->newFromHash($hash);
289 }
290  
291 ## $json = $obj->TO_JSON
292 176 ▶sub TO_JSON {
Subroutine TO_JSON: called CC=1 SCAR=0.0
Pod undocumented
293 176 return $_[0]->toHash(json=>1);
294 }
295  
296  
297 20 1; ##-- be happy
298  
299 =pod
300  
301 =head1 NAME
302  
303 DDC::PP::Object - common perl base class for DDC::PP objects
304  
305 =head1 SYNOPSIS
306  
307 #-- Preliminaries
308 use DDC::PP;
309 $CLASS = 'DDC::PP::Object';
310
311 ##---------------------------------------------------------------------
312 ## C -> Perl
313 $q = DDC::PP->parse("foo && bar");
314 $qs = $q->toString; ##-- $qs is "('foo' && 'bar')"
315 $hash = $q->toHash(); ##-- query encoded as perl hash-ref
316
317 #... the perl object can be manipulated directly (perl refcounting applies)
318 $hash->{Dtr1} = {class=>'CQTokExact',Value=>'baz'}; ##-- NO memory leak!
319
320 ##---------------------------------------------------------------------
321 ## Perl->C
322 $q2 = $CLASS->newFromHash($hash); ##-- $q2 needs explicit free()
323 $qs2 = $q2->toString(); ##-- $qs2 is "(@'baz' && 'bar')
324
325 ##---------------------------------------------------------------------
326 ## Deep copy & Traversal
327
328 $q3 = $q->clone(); ##-- wraps newFromHash($q->toHash)
329 $q = $q->mapTraverse(\&CODE); ##-- recursively tweak sub-objects
330
331 ##---------------------------------------------------------------------
332 ## JSON utilities
333 $json = $q->toJson(); ##-- ddc-internal json-ification
334 $json = $q->TO_JSON(); ##-- wraps toHash() for the JSON module
335 $obj = $CLASS->newFromJson($str); ##-- wraps newFromHash(from_json($str))
336
337 ##---------------------------------------------------------------------
338 ## Debugging
339 $obj->DumpTree(); ##-- dumps substructure to STDERR
340 $obj->free(); ##-- expplicit deep destruction, use at your own risk
341 \@kids = $obj->Children(); ##-- ARRAY-ref of direct children
342 \@desc = $obj->Descendants(); ##-- ARRAY-ref of descendants
343 undef = $obj->DisownChildren(); ##-- prevent deep destruction (dummy method; you should never need this)
344 $cnt = $obj->refcnt(); ##-- get internal reference count (dummy method)
345  
346  
347  
348 =head1 DESCRIPTION
349  
350 The DDC::PP::Object class is a pure-perl fork of the L<DDC::XS::Object|DDC::XS::Object> class, which see.
351  
352  
353 =head1 SEE ALSO
354  
355 perl(1),
356 DDC::PP(3perl),
357 DDC::PP::CQuery(3perl),
358 DDC::PP::CQCount(3perl),
359 DDC::PP::CQFilter(3perl),
360 DDC::PP::CQueryOptions(3perl),
361 DDC::PP::CQueryCompiler(3perl).
362  
363 =head1 AUTHOR
364  
365 Bryan Jurish E<lt>moocow@cpan.orgE<gt>
366  
367 =head1 COPYRIGHT AND LICENSE
368  
369 Copyright (C) 2016 by Bryan Jurish
370  
371 This library is free software; you can redistribute it and/or modify
372 it under the same terms as Perl itself, either Perl version 5.14.2 or,
373 at your option, any later version of Perl 5 you may have available.
374  
375 =cut
376  
« blib/lib/DDC/PP/CQuery.pm ↑ Summary blib/lib/DDC/HitList.pm »