| 1 | # ----------------------------------------------------------------------------- | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | =encoding utf8 | ||||||||||||||||||
| 4 | |||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||
| 6 | |||||||||||||||||||
| 7 | Quiq::Tree - Operatonen auf Perl-Baumstrukturen | ||||||||||||||||||
| 8 | |||||||||||||||||||
| 9 | =head1 BASE CLASS | ||||||||||||||||||
| 10 | |||||||||||||||||||
| 11 | L<Quiq::Object> | ||||||||||||||||||
| 12 | |||||||||||||||||||
| 13 | =head1 DESCRIPTION | ||||||||||||||||||
| 14 | |||||||||||||||||||
| 15 | Diese Klasse stellt Methoden zur Verfügung, um auf beliebigen | ||||||||||||||||||
| 16 | baumartigen Perl-Datenstrukturen operieren zu können. D.h. der | ||||||||||||||||||
| 17 | Baum wird als Datenstruktur aus Hashes und Arrays angesehen - ohne dass | ||||||||||||||||||
| 18 | die Knoten einer bestimmten Klasse angehören müssen. Die Klasse | ||||||||||||||||||
| 19 | besitzt daher ausschließlich Klassenmethoden. Der erste Parameter jeder | ||||||||||||||||||
| 20 | Klassenmethode ist eine Referenz auf den Wurzelknoten des Baums. | ||||||||||||||||||
| 21 | |||||||||||||||||||
| 22 | =cut | ||||||||||||||||||
| 23 | |||||||||||||||||||
| 24 | # ----------------------------------------------------------------------------- | ||||||||||||||||||
| 25 | |||||||||||||||||||
| 26 | package Quiq::Tree; | ||||||||||||||||||
| 27 | 3 | ▶ | use base qw/Quiq::Object/; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 28 | |||||||||||||||||||
| 29 | 3 | ▶ | use v5.10; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 30 | 3 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 31 | 3 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 32 | |||||||||||||||||||
| 33 | 3 | our $VERSION = '1.240'; | |||||||||||||||||
| 34 | |||||||||||||||||||
| 35 | 3 | ▶ | use Scalar::Util (); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 36 | 3 | ▶ | use Quiq::AnsiColor; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | # ----------------------------------------------------------------------------- | ||||||||||||||||||
| 39 | |||||||||||||||||||
| 40 | =head1 METHODS | ||||||||||||||||||
| 41 | |||||||||||||||||||
| 42 | =head2 Klassenmethoden | ||||||||||||||||||
| 43 | |||||||||||||||||||
| 44 | =head3 blessNodes() - Blesse Knotentypen auf bestimmte Klassen | ||||||||||||||||||
| 45 | |||||||||||||||||||
| 46 | =head4 Synopsis | ||||||||||||||||||
| 47 | |||||||||||||||||||
| 48 | $class->blessNodes($ref,{$type=>$class,...}); | ||||||||||||||||||
| 49 | |||||||||||||||||||
| 50 | =head4 Arguments | ||||||||||||||||||
| 51 | |||||||||||||||||||
| 52 | =over 4 | ||||||||||||||||||
| 53 | |||||||||||||||||||
| 54 | =item $ref | ||||||||||||||||||
| 55 | |||||||||||||||||||
| 56 | Referenz auf hierarchische Datenstruktur | ||||||||||||||||||
| 57 | |||||||||||||||||||
| 58 | =item {$type=>$class,...} | ||||||||||||||||||
| 59 | |||||||||||||||||||
| 60 | Abbildung von Typ $type auf Klasse $class | ||||||||||||||||||
| 61 | |||||||||||||||||||
| 62 | =back | ||||||||||||||||||
| 63 | |||||||||||||||||||
| 64 | =head4 Description | ||||||||||||||||||
| 65 | |||||||||||||||||||
| 66 | Durchlaufe die Datenstruktur $ref rekursiv und blesse Knoten | ||||||||||||||||||
| 67 | vom Typ $type auf Klasse $class. | ||||||||||||||||||
| 68 | |||||||||||||||||||
| 69 | B<ACHTUNG>: Wir eleminieren skalare Referenzen. In einem JSON-Baum | ||||||||||||||||||
| 70 | (erzeugt von JSON::decode_json() können Referenzen \1 und \0 als | ||||||||||||||||||
| 71 | Repräsentation von true und false vorkommen. | ||||||||||||||||||
| 72 | |||||||||||||||||||
| 73 | =head4 Example | ||||||||||||||||||
| 74 | |||||||||||||||||||
| 75 | $class->blessNodes($ref,{ | ||||||||||||||||||
| 76 | HASH => 'Quiq::Hash', | ||||||||||||||||||
| 77 | ARRAY => 'Quiq::Array', | ||||||||||||||||||
| 78 | }); | ||||||||||||||||||
| 79 | |||||||||||||||||||
| 80 | =cut | ||||||||||||||||||
| 81 | |||||||||||||||||||
| 82 | # ----------------------------------------------------------------------------- | ||||||||||||||||||
| 83 | |||||||||||||||||||
| 84 | 0 | ▶ | sub blessNodes { | ||||||||||||||||
|
Subroutine
blessNodes: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||
| 85 | 0 | my ($class,$ref,$typeH) = @_; | |||||||||||||||||
| 86 | |||||||||||||||||||
| 87 | 0 | my $type = Scalar::Util::reftype($ref); | |||||||||||||||||
| 88 | 0 | ▶ | if (!defined $type) { # Terminaler Knoten | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 89 | # nichts tun | ||||||||||||||||||
| 90 | 0 | return; | |||||||||||||||||
| 91 | } | ||||||||||||||||||
| 92 | 0 | ▶ | if (my $nodeClass = $typeH->{$type}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 93 | 0 | $ref = bless $ref,$nodeClass; | |||||||||||||||||
| 94 | } | ||||||||||||||||||
| 95 | 0 | ▶ | if ($type eq 'HASH') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 96 | 0 | for my $key (keys %$ref) { | |||||||||||||||||
| 97 | 0 | $class->blessNodes($ref->{$key},$typeH); | |||||||||||||||||
| 98 | } | ||||||||||||||||||
| 99 | } | ||||||||||||||||||
| 100 | elsif ($type eq 'ARRAY') { | ||||||||||||||||||
| 101 | 0 | for my $e (@$ref) { | |||||||||||||||||
| 102 | 0 | $class->blessNodes($e,$typeH); | |||||||||||||||||
| 103 | } | ||||||||||||||||||
| 104 | } | ||||||||||||||||||
| 105 | elsif ($type eq 'SCALAR') { | ||||||||||||||||||
| 106 | # Wir eleminieren skalare Referenzen. In einem JSON-Baum (erzeugt | ||||||||||||||||||
| 107 | # von JSON::decode_json() können Referenzen \1 und \0 als | ||||||||||||||||||
| 108 | # Repräsentation von true und false vorkommen. | ||||||||||||||||||
| 109 | 0 | $ref = $$ref; | |||||||||||||||||
| 110 | } | ||||||||||||||||||
| 111 | else { | ||||||||||||||||||
| 112 | 0 | $class->throw( | |||||||||||||||||
| 113 | 'TREE-00099: Unknown reference type', | ||||||||||||||||||
| 114 | Reference => "$ref", | ||||||||||||||||||
| 115 | Type => $type, | ||||||||||||||||||
| 116 | ); | ||||||||||||||||||
| 117 | } | ||||||||||||||||||
| 118 | |||||||||||||||||||
| 119 | 0 | return; | |||||||||||||||||
| 120 | } | ||||||||||||||||||
| 121 | |||||||||||||||||||
| 122 | # ----------------------------------------------------------------------------- | ||||||||||||||||||
| 123 | |||||||||||||||||||
| 124 | =head3 leafPaths() - Liste der Pfade | ||||||||||||||||||
| 125 | |||||||||||||||||||
| 126 | =head4 Synopsis | ||||||||||||||||||
| 127 | |||||||||||||||||||
| 128 | @paths|$pathA = $class->leafPaths($ref); | ||||||||||||||||||
| 129 | |||||||||||||||||||
| 130 | =head4 Arguments | ||||||||||||||||||
| 131 | |||||||||||||||||||
| 132 | =over 4 | ||||||||||||||||||
| 133 | |||||||||||||||||||
| 134 | =item $ref | ||||||||||||||||||
| 135 | |||||||||||||||||||
| 136 | Referenz auf den Baum | ||||||||||||||||||
| 137 | |||||||||||||||||||
| 138 | =back | ||||||||||||||||||
| 139 | |||||||||||||||||||
| 140 | =head4 Returns | ||||||||||||||||||
| 141 | |||||||||||||||||||
| 142 | (Array of Pairs) Liste der Pfade. Im Skalarkontext wird eine Referenz auf die | ||||||||||||||||||
| 143 | Liste geliefert. | ||||||||||||||||||
| 144 | |||||||||||||||||||
| 145 | =head4 Description | ||||||||||||||||||
| 146 | |||||||||||||||||||
| 147 | Liefere die Liste der Pfade [$path,$value] zu den Blattknoten des Baums $ref. | ||||||||||||||||||
| 148 | Diese Liste ist nützlich, um die Zugriffspfade zu den Blattknoten | ||||||||||||||||||
| 149 | zu ermitteln. | ||||||||||||||||||
| 150 | |||||||||||||||||||
| 151 | =head4 Examples | ||||||||||||||||||
| 152 | |||||||||||||||||||
| 153 | Zeige die Pfade aller Blattknoten eines ZUGFeRD-Baums: | ||||||||||||||||||
| 154 | |||||||||||||||||||
| 155 | $ perl -MQuiq::Zugferd -E '$t = Quiq::Zugferd->new("en16931")->tree; for (Quiq::Tree->leafPaths($t)) {say "@$_"}' | ||||||||||||||||||
| 156 | |||||||||||||||||||
| 157 | =cut | ||||||||||||||||||
| 158 | |||||||||||||||||||
| 159 | # ----------------------------------------------------------------------------- | ||||||||||||||||||
| 160 | |||||||||||||||||||
| 161 | 3 | my $a = Quiq::AnsiColor->new(1); | |||||||||||||||||
| 162 | |||||||||||||||||||
| 163 | 0 | ▶ | sub leafPaths { | ||||||||||||||||
|
Subroutine
leafPaths: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||
| 164 | 0 | my ($class,$ref,$path) = @_; | |||||||||||||||||
| 165 | |||||||||||||||||||
| 166 | 0 | ▶ | $path //= ''; | ||||||||||||||||
|
Condition0%0/2
$path //= ""
MC/DC0%0/1
$path //= ""
$path
Truth table0%0/2
A
A= $path
| |||||||||||||||||||
| 167 | 0 | my @paths; | |||||||||||||||||
| 168 | |||||||||||||||||||
| 169 | 0 | my $type = Scalar::Util::reftype($ref); | |||||||||||||||||
| 170 | 0 | ▶ | if (!defined $type) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 171 | 0 | ▶ | $ref //= ''; | ||||||||||||||||
|
Condition0%0/2
$ref //= ""
MC/DC0%0/1
$ref //= ""
$ref
Truth table0%0/2
A
A= $ref
| |||||||||||||||||||
| 172 | 0 | $ref =~ s/\n/\\n/g; | |||||||||||||||||
| 173 | # return "$path ".$a->str('cyan',$ref); # Pfad und terminaler skalarer Wert | ||||||||||||||||||
| 174 | 0 | return [$path,$ref]; # ".$a->str('cyan',$ref); # Pfad und terminaler skalarer Wert | |||||||||||||||||
| 175 | } | ||||||||||||||||||
| 176 | elsif ($type eq 'HASH') { | ||||||||||||||||||
| 177 | 0 | for my $key (keys %$ref) { | |||||||||||||||||
| 178 | 0 | ▶ | push @paths,$class->leafPaths($ref->{$key},$path? "$path.$key": $key); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 179 | } | ||||||||||||||||||
| 180 | } | ||||||||||||||||||
| 181 | elsif ($type eq 'ARRAY') { | ||||||||||||||||||
| 182 | 0 | my $i = 0; | |||||||||||||||||
| 183 | 0 | for my $e (@$ref) { | |||||||||||||||||
| 184 | 0 | ▶ | push @paths,$class->leafPaths($ref->[$i],$path? "$path.[$i]": "[$i]"); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 185 | 0 | $i++; | |||||||||||||||||
| 186 | } | ||||||||||||||||||
| 187 | } | ||||||||||||||||||
| 188 | else { | ||||||||||||||||||
| 189 | 0 | $class->throw( | |||||||||||||||||
| 190 | 'TREE-00099: Unknown reference type', | ||||||||||||||||||
| 191 | Reference => "$ref", | ||||||||||||||||||
| 192 | Type => $type, | ||||||||||||||||||
| 193 | ); | ||||||||||||||||||
| 194 | } | ||||||||||||||||||
| 195 | |||||||||||||||||||
| 196 | 0 | ▶ | return wantarray? @paths: \@paths; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 197 | } | ||||||||||||||||||
| 198 | |||||||||||||||||||
| 199 | # ----------------------------------------------------------------------------- | ||||||||||||||||||
| 200 | |||||||||||||||||||
| 201 | =head3 removeEmptyNodes() - Entferne alle leeren Knoten | ||||||||||||||||||
| 202 | |||||||||||||||||||
| 203 | =head4 Synopsis | ||||||||||||||||||
| 204 | |||||||||||||||||||
| 205 | $class->removeEmptyNodes($ref); | ||||||||||||||||||
| 206 | |||||||||||||||||||
| 207 | =head4 Arguments | ||||||||||||||||||
| 208 | |||||||||||||||||||
| 209 | =over 4 | ||||||||||||||||||
| 210 | |||||||||||||||||||
| 211 | =item $ref | ||||||||||||||||||
| 212 | |||||||||||||||||||
| 213 | Referenz auf hierarchische Datenstruktur | ||||||||||||||||||
| 214 | |||||||||||||||||||
| 215 | =back | ||||||||||||||||||
| 216 | |||||||||||||||||||
| 217 | =head4 Description | ||||||||||||||||||
| 218 | |||||||||||||||||||
| 219 | Durchlaufe die Datenstruktur $ref rekursiv und entferne alle leeren | ||||||||||||||||||
| 220 | Knoten. | ||||||||||||||||||
| 221 | |||||||||||||||||||
| 222 | =over 2 | ||||||||||||||||||
| 223 | |||||||||||||||||||
| 224 | =item * | ||||||||||||||||||
| 225 | |||||||||||||||||||
| 226 | Ein Blattknoten ist leer, wenn wenn sein Wert C<undef> ist. | ||||||||||||||||||
| 227 | |||||||||||||||||||
| 228 | =item * | ||||||||||||||||||
| 229 | |||||||||||||||||||
| 230 | Ein Hashknoten ist leer, wenn er kein Element enthält. | ||||||||||||||||||
| 231 | |||||||||||||||||||
| 232 | =item * | ||||||||||||||||||
| 233 | |||||||||||||||||||
| 234 | Ein Arrayknoten ist leer, wenn er kein Element enthält. | ||||||||||||||||||
| 235 | |||||||||||||||||||
| 236 | =back | ||||||||||||||||||
| 237 | |||||||||||||||||||
| 238 | =cut | ||||||||||||||||||
| 239 | |||||||||||||||||||
| 240 | # ----------------------------------------------------------------------------- | ||||||||||||||||||
| 241 | |||||||||||||||||||
| 242 | 0 | ▶ | sub removeEmptyNodes { | ||||||||||||||||
|
Subroutine
removeEmptyNodes: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||
| 243 | 0 | my ($class,$ref) = @_; | |||||||||||||||||
| 244 | |||||||||||||||||||
| 245 | # Durchlaufe den Baum so lange immmer wieder rekursiv, | ||||||||||||||||||
| 246 | # bis keine Knoten mehr entfernt werden | ||||||||||||||||||
| 247 | |||||||||||||||||||
| 248 | 0 | my $n = 0; | |||||||||||||||||
| 249 | 0 | ▶ | do { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 250 | 0 | $n = $class->removeEmptyNodesRecursive($ref); | |||||||||||||||||
| 251 | } while $n; | ||||||||||||||||||
| 252 | |||||||||||||||||||
| 253 | 0 | return; | |||||||||||||||||
| 254 | } | ||||||||||||||||||
| 255 | |||||||||||||||||||
| 256 | # ----------------------------------------------------------------------------- | ||||||||||||||||||
| 257 | |||||||||||||||||||
| 258 | =head3 removeEmptyNodesRecursive() - Entferne leere Knoten | ||||||||||||||||||
| 259 | |||||||||||||||||||
| 260 | =head4 Synopsis | ||||||||||||||||||
| 261 | |||||||||||||||||||
| 262 | $class->removeEmptyNodesRecursive($ref); | ||||||||||||||||||
| 263 | |||||||||||||||||||
| 264 | =head4 Arguments | ||||||||||||||||||
| 265 | |||||||||||||||||||
| 266 | =over 4 | ||||||||||||||||||
| 267 | |||||||||||||||||||
| 268 | =item $ref | ||||||||||||||||||
| 269 | |||||||||||||||||||
| 270 | Referenz auf Baum | ||||||||||||||||||
| 271 | |||||||||||||||||||
| 272 | =back | ||||||||||||||||||
| 273 | |||||||||||||||||||
| 274 | =head4 Description | ||||||||||||||||||
| 275 | |||||||||||||||||||
| 276 | Interne Methode, die den Baum rekursiv durchläuft und die leeren Knoten | ||||||||||||||||||
| 277 | entfernt. Es sind i.d.R. mehrere Durchläufe nötig, um I<alle> leeren | ||||||||||||||||||
| 278 | Knoten zu entfernen, siehe $class->removeEmptyNodes(). | ||||||||||||||||||
| 279 | |||||||||||||||||||
| 280 | =cut | ||||||||||||||||||
| 281 | |||||||||||||||||||
| 282 | # ----------------------------------------------------------------------------- | ||||||||||||||||||
| 283 | |||||||||||||||||||
| 284 | 0 | ▶ | sub removeEmptyNodesRecursive { | ||||||||||||||||
|
Subroutine
removeEmptyNodesRecursive: not called CC=12 SCAR=50.5
Pod
documented
| |||||||||||||||||||
| 285 | 0 | my $class = shift; | |||||||||||||||||
| 286 | 0 | my $ref = $_[0]; | |||||||||||||||||
| 287 | |||||||||||||||||||
| 288 | 0 | ▶ | if (!defined $ref) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 289 | # Kein Baum (mehr) | ||||||||||||||||||
| 290 | 0 | return 0; | |||||||||||||||||
| 291 | } | ||||||||||||||||||
| 292 | |||||||||||||||||||
| 293 | 0 | my $n = 0; | |||||||||||||||||
| 294 | |||||||||||||||||||
| 295 | 0 | my $type = Scalar::Util::reftype($ref); | |||||||||||||||||
| 296 | 0 | ▶ | if ($type eq 'HASH') { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 297 | 0 | my @keys = keys %$ref; | |||||||||||||||||
| 298 | 0 | for my $key (@keys) { | |||||||||||||||||
| 299 | 0 | ▶ | if (!defined Scalar::Util::reftype($ref->{$key})) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 300 | 0 | ▶ | if (!defined $ref->{$key}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 301 | 0 | delete $ref->{$key}; | |||||||||||||||||
| 302 | 0 | $n++; | |||||||||||||||||
| 303 | } | ||||||||||||||||||
| 304 | } | ||||||||||||||||||
| 305 | else { | ||||||||||||||||||
| 306 | 0 | $n += $class->removeEmptyNodesRecursive($ref->{$key}); | |||||||||||||||||
| 307 | } | ||||||||||||||||||
| 308 | } | ||||||||||||||||||
| 309 | 0 | ▶ | if (!keys %$ref) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 310 | 0 | $_[0] = undef; | |||||||||||||||||
| 311 | 0 | $n++; | |||||||||||||||||
| 312 | } | ||||||||||||||||||
| 313 | } | ||||||||||||||||||
| 314 | elsif ($type eq 'ARRAY') { | ||||||||||||||||||
| 315 | 0 | ▶ | for (my $i = 0; $i < @$ref; $i++) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 316 | 0 | ▶ | if (!defined Scalar::Util::reftype($ref->[$i])) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 317 | 0 | ▶ | if (!defined $ref->[$i]) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 318 | 0 | splice @$ref,$i--,1; | |||||||||||||||||
| 319 | 0 | $n++; | |||||||||||||||||
| 320 | } | ||||||||||||||||||
| 321 | } | ||||||||||||||||||
| 322 | else { | ||||||||||||||||||
| 323 | 0 | $n += $class->removeEmptyNodesRecursive($ref->[$i]); | |||||||||||||||||
| 324 | } | ||||||||||||||||||
| 325 | } | ||||||||||||||||||
| 326 | 0 | ▶ | if (!@$ref) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 327 | 0 | $_[0] = undef; | |||||||||||||||||
| 328 | 0 | $n++; | |||||||||||||||||
| 329 | } | ||||||||||||||||||
| 330 | } | ||||||||||||||||||
| 331 | else { | ||||||||||||||||||
| 332 | 0 | $class->throw( | |||||||||||||||||
| 333 | 'TREE-00099: Unknown reference type', | ||||||||||||||||||
| 334 | Reference => "$ref", | ||||||||||||||||||
| 335 | Type => $type, | ||||||||||||||||||
| 336 | ); | ||||||||||||||||||
| 337 | } | ||||||||||||||||||
| 338 | |||||||||||||||||||
| 339 | 0 | return $n; | |||||||||||||||||
| 340 | } | ||||||||||||||||||
| 341 | |||||||||||||||||||
| 342 | # ----------------------------------------------------------------------------- | ||||||||||||||||||
| 343 | |||||||||||||||||||
| 344 | =head3 setLeafValue() - Setze den Wert von Blattknoten | ||||||||||||||||||
| 345 | |||||||||||||||||||
| 346 | =head4 Synopsis | ||||||||||||||||||
| 347 | |||||||||||||||||||
| 348 | $class->setLeafValue($ref,$sub); | ||||||||||||||||||
| 349 | |||||||||||||||||||
| 350 | =head4 Arguments | ||||||||||||||||||
| 351 | |||||||||||||||||||
| 352 | =over 4 | ||||||||||||||||||
| 353 | |||||||||||||||||||
| 354 | =item $ref | ||||||||||||||||||
| 355 | |||||||||||||||||||
| 356 | Referenz auf hierarchische Datenstruktur | ||||||||||||||||||
| 357 | |||||||||||||||||||
| 358 | =item $sub | ||||||||||||||||||
| 359 | |||||||||||||||||||
| 360 | Referenz auf Subroutine, die für jeden Blattknoten gerufen wird. | ||||||||||||||||||
| 361 | |||||||||||||||||||
| 362 | =back | ||||||||||||||||||
| 363 | |||||||||||||||||||
| 364 | =head4 Description | ||||||||||||||||||
| 365 | |||||||||||||||||||
| 366 | Durchlaufe die Datenstruktur $ref rekursiv, rufe auf allen Blattknoten | ||||||||||||||||||
| 367 | die Subroutine $sub mit dem aktuellen Wert des Knotens auf und setze | ||||||||||||||||||
| 368 | auf dem Knoten den gelieferten Wert. | ||||||||||||||||||
| 369 | |||||||||||||||||||
| 370 | Ein Blattknoten des Baums ist dadurch gekennzeichner, dass er einen | ||||||||||||||||||
| 371 | "einfachen" skalaren Wert besitzt, also auf keine Substruktur | ||||||||||||||||||
| 372 | (Hash- oder Array-Referenz) verweist. | ||||||||||||||||||
| 373 | |||||||||||||||||||
| 374 | =cut | ||||||||||||||||||
| 375 | |||||||||||||||||||
| 376 | # ----------------------------------------------------------------------------- | ||||||||||||||||||
| 377 | |||||||||||||||||||
| 378 | 0 | ▶ | sub setLeafValue { | ||||||||||||||||
|
Subroutine
setLeafValue: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||
| 379 | 0 | my ($class,$ref,$sub) = @_; | |||||||||||||||||
| 380 | |||||||||||||||||||
| 381 | 0 | my $type = Scalar::Util::reftype($ref); | |||||||||||||||||
| 382 | 0 | ▶ | if ($type eq 'HASH') { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 383 | 0 | for my $key (keys %$ref) { | |||||||||||||||||
| 384 | 0 | ▶ | if (!defined Scalar::Util::reftype($ref->{$key})) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 385 | 0 | $ref->{$key} = $sub->($ref->{$key}); | |||||||||||||||||
| 386 | } | ||||||||||||||||||
| 387 | else { | ||||||||||||||||||
| 388 | 0 | $class->setLeafValue($ref->{$key},$sub); | |||||||||||||||||
| 389 | } | ||||||||||||||||||
| 390 | } | ||||||||||||||||||
| 391 | } | ||||||||||||||||||
| 392 | elsif ($type eq 'ARRAY') { | ||||||||||||||||||
| 393 | 0 | for my $e (@$ref) { | |||||||||||||||||
| 394 | 0 | ▶ | if (!defined Scalar::Util::reftype($e)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 395 | 0 | $e = $sub->($e); | |||||||||||||||||
| 396 | } | ||||||||||||||||||
| 397 | else { | ||||||||||||||||||
| 398 | 0 | $class->setLeafValue($e,$sub); | |||||||||||||||||
| 399 | } | ||||||||||||||||||
| 400 | } | ||||||||||||||||||
| 401 | } | ||||||||||||||||||
| 402 | else { | ||||||||||||||||||
| 403 | 0 | $class->throw( | |||||||||||||||||
| 404 | 'TREE-00099: Unknown reference type', | ||||||||||||||||||
| 405 | Reference => "$ref", | ||||||||||||||||||
| 406 | Type => $type, | ||||||||||||||||||
| 407 | ); | ||||||||||||||||||
| 408 | } | ||||||||||||||||||
| 409 | |||||||||||||||||||
| 410 | 0 | return; | |||||||||||||||||
| 411 | } | ||||||||||||||||||
| 412 | |||||||||||||||||||
| 413 | # ----------------------------------------------------------------------------- | ||||||||||||||||||
| 414 | |||||||||||||||||||
| 415 | =head1 VERSION | ||||||||||||||||||
| 416 | |||||||||||||||||||
| 417 | 1.240 | ||||||||||||||||||
| 418 | |||||||||||||||||||
| 419 | =head1 AUTHOR | ||||||||||||||||||
| 420 | |||||||||||||||||||
| 421 | Frank Seitz, L<http://fseitz.de/> | ||||||||||||||||||
| 422 | |||||||||||||||||||
| 423 | =head1 COPYRIGHT | ||||||||||||||||||
| 424 | |||||||||||||||||||
| 425 | Copyright (C) 2026 Frank Seitz | ||||||||||||||||||
| 426 | |||||||||||||||||||
| 427 | =head1 LICENSE | ||||||||||||||||||
| 428 | |||||||||||||||||||
| 429 | This code is free software; you can redistribute it and/or modify | ||||||||||||||||||
| 430 | it under the same terms as Perl itself. | ||||||||||||||||||
| 431 | |||||||||||||||||||
| 432 | =cut | ||||||||||||||||||
| 433 | |||||||||||||||||||
| 434 | # ----------------------------------------------------------------------------- | ||||||||||||||||||
| 435 | |||||||||||||||||||
| 436 | 3 | 1; | |||||||||||||||||
| 437 | |||||||||||||||||||
| 438 | # eof | ||||||||||||||||||