Quiq/Hash.pm

filter: Statementstmt 75.9% 218 / 287 Branchbran 64.9% 74 / 114 Conditioncond 44.4% 4 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 84.6% 44 / 52 Podpod 100% 39 / 39 totaltotal 74.9% 379 / 506 CC 69 SCAR 19.7
CC
69
Cov
72%
setDeep
42.0
memoizeWeaken
20.0
asString
20.0
CRAP 7.1
« Quiq/Sftp/Client.pm ↑ Summary Quiq/Perl.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Hash - Zugriffssicherer Hash mit automatisch generierten Attributmethoden
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Object>
12  
13 =head1 SYNOPSIS
14  
15 Klasse laden:
16  
17 use Quiq::Hash;
18  
19 Hash-Objekt instantiieren:
20  
21 my $h = Quiq::Hash->new(a=>1,b=>1,c=>3);
22  
23 Werte abfragen oder setzen:
24  
25 my $v = $h->get('a'); # oder: $v = $h->{'a'};
26 $h->set(b=>2); # oder: $h->{'b'} = 2;
27  
28 Unerlaubte Zugriffe:
29  
30 $v = $h->get('d'); # Exception!
31 $h->set(d=>4); # Exception!
32  
33 Erlaubte Zugriffe;
34  
35 $v = $h->try('d'); # undef
36 $h->add(d=>4);
37  
38 =head1 DESCRIPTION
39  
40 Ein Objekt dieser Klasse repräsentiert einen I<zugriffssicheren> Hash,
41 d.h. einen Hash, dessen Schlüsselvorrat bei der Instantiierung
42 festgelegt wird. Ein lesender oder schreibender Zugriff mit einem
43 Schlüssel, der nicht zum Schlüsselvorrat gehört, ist nicht erlaubt
44 und führt zu einer Exception.
45  
46 Der Zugriffsschutz beruht auf der Funktionalität des
47 L<Restricted Hash|http://perldoc.perl.org/Hash/Util.html#Restricted-hash>.
48  
49 Abgesehen vom Zugriffsschutz verhält sich ein Hash-Objekt der
50 Klasse wie einer normaler Perl-Hash und kann auch so angesprochen
51 werden. Bei den Methoden ist der konventionelle Zugriff als
52 C<Alternative Formulierung> angegeben.
53  
54 =cut
55  
56 # -----------------------------------------------------------------------------
57  
58 package Quiq::Hash;
59 270 ▶use base qw/Quiq::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 270 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 270 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 270 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 270 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 270 our $VERSION = '1.240';
67  
68 270 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
69 270 ▶use Quiq::Stacktrace;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 270 ▶use Hash::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 # -----------------------------------------------------------------------------
73  
74 270 our $GetCount = 0;
75 270 our $SetCount = 0;
76  
77 # -----------------------------------------------------------------------------
78  
79 =head1 METHODS
80  
81 =head2 Instantiierung
82  
83 =head3 new() - Instantiiere Hash
84  
85 =head4 Synopsis
86  
87 $h = $class->new; # [1]
88 $h = $class->new(@keyVal); # [2]
89 $h = $class->new(\@keys,\@vals[,$val]); # [3]
90 $h = $class->new(\@keys[,$val]); # [4]
91 $h = $class->new(\%hash); # [5]
92 $h = $class->new(\%hash,@keyVal); # [6]
93  
94 =head4 Description
95  
96 Instantiiere ein Hash-Objekt, setze die Schlüssel/Wert-Paare
97 und liefere eine Referenz auf dieses Objekt zurück.
98  
99 =over 4
100  
101 =item [1]
102  
103 Leerer Hash.
104  
105 =item [2]
106  
107 Die Argumentliste ist eine Aufzählung von Schlüssel/Wert-Paaren.
108  
109 =item [3]
110  
111 Schlüssel und Werte befinden sich in getrennten Arrays.
112 Ist ein Wert C<undef>, wird $val gesetzt, falls angegeben.
113  
114 =item [4]
115  
116 Nur die Schlüssel sind angegeben. Ist $val angegeben, werden
117 alle Werte auf diesen Wert gesetzt. Ist $val nicht angegeben,
118 werden alle Werte auf C<undef> gesetzt.
119  
120 =item [5]
121  
122 Blesse den Hash %hash auf Klasse $class.
123  
124 =item [6]
125  
126 Instantiiere den Hash aus den Schlüssel/Wert-Paaren @keyVal
127 und weise dem (restricted) Hash alle Komponenten aus %hash
128 zu. Dieser Aufruf ist nützlich, um einen anonymen Hash
129 zu einem Hash-Objekt mit vorgegebenen Attributen zu machen.
130 (Wenn der anonyme Hash ein nicht-vorgesehenes Attribut
131 enthält, wird eine Exception geworfen.)
132  
133 =back
134  
135 =cut
136  
137 # -----------------------------------------------------------------------------
138  
139 1321 ▶sub new {
Subroutine new: called CC=10 SCAR=23.0
Pod documented
140 1321 my $class = shift;
141 # @_: Argumente
142  
143 1321 my $h;
144 1321 ▶ if (!ref $_[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $_[0]) { } 1203 118 1321
elsif ((builtin::reftype($_[0]) || '') eq 'HASH') { } 38 80 118
145 # Aufruf: $h = $class->new;
146 # Aufruf: $h = $class->new(@keyVal);
147  
148 1203 $h = \my %h;
149 1203 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 9375 1203 10578
150 9375 my $key = shift;
151 9375 $h{$key} = shift;
152 }
153 }
154 ▶ elsif ((Scalar::Util::reftype($_[0]) || '') eq 'HASH') { # Perform.
Condition50%1/2
builtin::reftype($_[0]) || ''
l!l
1180
MC/DC0%0/1
builtin::reftype($_[0]) || ''
builtin::reftype($_[0])
Truth table50%1/2
A
A= builtin::reftype($_[0])
Aresult
11
00
155 38 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 35 3 38
156 # Aufruf: $h = $class->new(\%hash);
157 35 $h = shift;
158 # Sicherheitshalber, falls der Hash gelocked ist
159 35 Hash::Util::unlock_keys(%$h);
160 35 bless $h,$class;
161 }
162 else {
163 # Aufruf: $h = $class->new(\%hash,@keyVal);
164 3 my $anonH = shift;
165  
166 3 my $self = $class->new(@_);
167 3 $self->set(%$anonH);
168 3 return $self;
169 }
170 }
171 else {
172 # Aufruf: $h = $class->new(\@keys,...);
173 80 my $keyA = shift;
174  
175 80 $h = \my %h;
176 80 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 73 7 80
177 # Aufruf: $h = $class->new(\@keys,\@vals,...);
178 73 my $valA = shift;
179  
180 73 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 72 1 73
181 # Aufruf: $h = $class->new(\@keys,\@vals,$val);
182 72 my $val = shift;
183 72 my $i = 0;
184 72 for my $key (@$keyA) {
185 197 $h{$key} = $valA->[$i++];
186 197 ▶ if (!defined $h{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $h{$key}) 1 196 197
187 1 $h{$key} = $val;
188 }
189 }
190 }
191 else {
192 # Aufruf: $h = $class->new(\@keys,\@vals);
193 1 @h{@$keyA} = @$valA;
194 }
195 }
196 else {
197 # Aufruf: $h = $class->new(\@keys[,$val]);
198  
199 7 my $val = shift;
200 7 @h{@$keyA} = ($val) x @$keyA;
201 }
202 }
203  
204 # Sperre Schlüssel gegen Änderungen
205  
206 1318 bless $h,$class;
207 1318 $h->lockKeys;
208  
209 1318 return $h;
210 }
211  
212 # -----------------------------------------------------------------------------
213  
214 =head3 fabricate() - Instantiiere Hash für Klasse
215  
216 =head4 Synopsis
217  
218 $h = $class->fabricate($subClass,...);
219  
220 =head4 Description
221  
222 Wie new(), nur dass der Hash als Instanz der Subklasse $subClass
223 erzeugt wird. Die Subklasse wird on-the-fly erzeugt, falls sie noch
224 nicht existiert.
225  
226 =cut
227  
228 # -----------------------------------------------------------------------------
229  
230 0 ▶sub fabricate {
Subroutine fabricate: not called CC=3 SCAR=20.0
Pod documented
231 0 my ($class,$subClass) = splice @_,0,2;
232 # @_: Argumente
233  
234 270 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
235 0 ▶ if (!defined *{$subClass.'::'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined *{$subClass . '::';}) 0 0 0
236 0 eval "package $subClass; our \@ISA = ('$class');";
237 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
238 0 die;
239 }
240 }
241  
242 0 return $subClass->new(@_);
243 }
244  
245 # -----------------------------------------------------------------------------
246  
247 =head2 Akzessoren
248  
249 =head3 get() - Werte abfragen
250  
251 =head4 Synopsis
252  
253 $val = $h->get($key);
254 @vals = $h->get(@keys);
255  
256 =head4 Description
257  
258 Liefere die Werte zu den angegebenen Schlüsseln. In skalarem Kontext
259 liefere keine Liste, sondern den Wert des ersten Schlüssels.
260  
261 Alternative Formulierung:
262  
263 $val = $h->{$key}; # ein Schlüssel
264 @vals = @{$h}{@keys}; # mehrere Schlüssel
265  
266 =cut
267  
268 # -----------------------------------------------------------------------------
269  
270 1065 ▶sub get {
Subroutine get: called CC=3 SCAR=11.0
Pod documented
271 1065 my $self = shift;
272 # @_: @keys
273  
274 1065 $GetCount++;
275 1065 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 628 437 1065
276 628 my @arr;
277 628 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2108 628 2736
278 2108 my $key = shift;
279 2108 push @arr,$self->{$key};
280 }
281 628 return @arr;
282 }
283  
284 437 return $self->{$_[0]};
285 }
286  
287 # -----------------------------------------------------------------------------
288  
289 =head3 getDeep() - Werte per "tiefem Zugriff" abfragen
290  
291 =head4 Synopsis
292  
293 $val = $h->getDeep($key);
294 $val = $h->getDeep($key,$sloppy);
295  
296 =head4 Arguments
297  
298 =over 4
299  
300 =item $key
301  
302 Zugriffspfad der Form "key1.key2...". Als Schlüssel kann auch "[N]"
303 angegeben werden, dann wird auf ein Array-Element zugegriffen.
304  
305 =item $sloppy
306  
307 Wenn gesetzt, wird keine Exception geworfen, Falls der Zugriffspfad
308 nicht existiert.
309  
310 =back
311  
312 =head4 Description
313  
314 Liefere den Wert zum angegebenen Schlüssel. Der Schlüssel ist eine
315 mit Punkt (.) getrennte Kette von Einzelschlüsseln, so dass
316 sich ein "tiefer Zugriff" kompakt formulieren lässt.
317  
318 =head4 Example
319  
320 B<Folge von Hash-Zugriffen>
321  
322 $val = $h->getDeep('invoice.header.invoiceNumber');
323  
324 ist dasselbe wie
325  
326 $val = $h->{'invoice'}->{'header'}->{'invoiceNumber'};
327  
328 B<Zugriff über Array-Element>
329  
330 $val = $h->getDeep('settlement.tradeTax.[0].type');
331  
332 ist dasselbe wie
333  
334 $val = $h->{'settlement'}->{'tradeTax'}->[0]->{'type'};
335  
336 =cut
337  
338 # -----------------------------------------------------------------------------
339  
340 5 ▶sub getDeep {
Subroutine getDeep: called CC=7 SCAR=21.5
Pod documented
341 5 my ($self,$key,$sloppy) = @_;
342  
343 5 my $val = $self;
344 5 for (split /\./,$key) {
345 16 ▶ if (/\[(\d+)\]/) { # Array-Zugriff
Branch: 1 branch
BranchTrueFalseTotal
if (/\[(\d+)\]/) { } 1 15 16
346 1 ▶ if (!exists $val->[$1]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $val->[$1]) 0 1 1
347 0 ▶ if (!$sloppy) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sloppy) 0 0 0
348 0 $self->throw(
349 'HASH-00099: Non-existent access path',
350 Path => $key,
351 );
352 }
353 0 return undef;
354 }
355 1 $val = $val->[$1];
356 }
357 else { # Hash-Zugriff
358 15 ▶ if (!exists $val->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $val->{$_}) 1 14 15
359 1 ▶ if (!$sloppy) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sloppy) 1 0 1
360 1 $self->throw(
361 'HASH-00099: Non-existent access path',
362 Path => $key,
363 );
364 }
365 0 return undef;
366 }
367 14 $val = $val->{$_};
368 }
369 }
370  
371 4 return $val;
372 }
373  
374 # -----------------------------------------------------------------------------
375  
376 =head3 setDeep() - Wert per "tiefem Zugriff" setzen
377  
378 =head4 Synopsis
379  
380 $h->setDeep(@keyVal);
381  
382 =head4 Arguments
383  
384 =over 4
385  
386 =item @keyVal
387  
388 Schlüssel ist ein Zugriffspfad der Form "key1.key2...", Wert ist ein
389 beliebiger skalarer Wert.
390  
391 =back
392  
393 =head4 Description
394  
395 Setze die Werte auf die angegebenen Schlüssel. Der Schlüssel ist eine
396 mit Punkt (.) getrennte Kette von Einzelschlüsseln, so dass
397 sich ein "tiefer Zugriff" kompakt formulieren lässt.
398  
399 =head4 Example
400  
401 B<Folge von Hash-Zugriffen>
402  
403 $h->setDeep('invoice.header.invoiceNumber' => $val);
404  
405 ist dasselbe wie
406  
407 $h->{'invoice'}->{'header'}->{'invoiceNumber'} = $val;
408  
409 B<Zugriff über Array-Element>
410  
411 $h->setDeep('settlement.tradeTax.[0].type' => $val);
412  
413 ist dasselbe wie
414  
415 $h->{'settlement'}->{'tradeTax'}->[0]->{'type'} = $val;
416  
417 =cut
418  
419 # -----------------------------------------------------------------------------
420  
421 0 ▶sub setDeep {
Subroutine setDeep: not called CC=6 SCAR=37.4
Pod documented
422 0 my $self = shift;
423 # @_: @keyVal
424  
425 0 my $ref = $self;
426 0 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
427 0 my $key = shift;
428 0 my $val = shift;
429  
430 0 my @keys = split /\./,$key;
431 0 ▶ for (my $i = 0; $i < $#keys; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#keys) 0 0 0
432 0 ▶ if ($keys[$i] =~ /\[(\d+)\]/) { # Array-Zugriff
Branch: 1 branch
BranchTrueFalseTotal
if ($keys[$i] =~ /\[(\d+)\]/) { } 0 0 0
433 0 ▶ if (!exists $ref->[$1]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ref->[$1]) 0 0 0
434 0 $self->throw(
435 'HASH-00099: Non-existent access path',
436 Path => $key,
437 Key => $keys[$i],
438 );
439 }
440 0 $ref = $ref->[$1];
441 }
442 else { # Hash-Zugriff
443 0 ▶ if (!exists $ref->{$keys[$i]}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $$ref{$keys[$i]}) 0 0 0
444 0 $self->throw(
445 'HASH-00099: Non-existent access path',
446 Path => $key,
447 Key => $keys[$i],
448 );
449 }
450 0 $ref = $ref->{$keys[$i]};
451 }
452 }
453 0 $ref->{$keys[-1]} = $val;
454 }
455  
456 0 return;
457 }
458  
459 # -----------------------------------------------------------------------------
460  
461 =head3 getRef() - Referenz auf Wert
462  
463 =head4 Synopsis
464  
465 $valS = $h->getRef($key);
466  
467 =head4 Description
468  
469 Liefere nicht den Wert zum Schlüssel $key, sondern eine Referenz auf
470 den Wert.
471  
472 Dies kann praktisch sein, wenn der Wert manipuliert werden soll. Die
473 Manipulation kann dann über die Referenz erfolgen und der Wert muss
474 nicht erneut zugewiesen werden.
475  
476 Alternative Formulierung:
477  
478 $valS = \$h->{$key};
479  
480 =head4 Example
481  
482 Newline an Wert anhängen mit getRef():
483  
484 $valS = $h->getRef('x');
485 $$valS .= "\n";
486  
487 Dasselbe ohne getRef():
488  
489 $val = $h->get('x');
490 $val .= "\n";
491 $val->set(x=>$val);
492  
493 =cut
494  
495 # -----------------------------------------------------------------------------
496  
497 72 ▶sub getRef {
Subroutine getRef: called CC=1 SCAR=0.0
Pod documented
498 72 return \$_[0]->{$_[1]};
499 }
500  
501 # -----------------------------------------------------------------------------
502  
503 =head3 getArray() - Liefere Array
504  
505 =head4 Synopsis
506  
507 @arr|$arr = $h->getArray($key);
508  
509 =head4 Description
510  
511 Liefere die Liste von Werten des Schlüssels $key. Im Skalarkontext
512 liefere eine Referenz auf die Liste (der Aufruf hat dann die gleiche
513 Wirkung wie der Aufruf von $h->L<get|"get() - Werte abfragen">()). Der Wert von $key muss
514 eine Array-Referenz sein.
515  
516 =cut
517  
518 # -----------------------------------------------------------------------------
519  
520 3 ▶sub getArray {
Subroutine getArray: called CC=3 SCAR=11.1
Pod documented
521 3 my ($self,$key) = @_;
522 3 ▶ my $arr = $self->{$key} || [];
Condition50%1/2
$self->{$key} || []
l!l
30
MC/DC0%0/1
$self->{$key} || []
$self->{$key}
Truth table50%1/2
A
A= $self->{$key}
Aresult
11
00
523 3 ▶ return wantarray? @$arr: $arr;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 1 3
524 }
525  
526 # -----------------------------------------------------------------------------
527  
528 =head3 try() - Werte abfragen ohne Exception
529  
530 =head4 Synopsis
531  
532 $val = $h->try($key);
533 @vals = $h->try(@keys);
534  
535 =head4 Description
536  
537 Wie L<get|"get() - Werte abfragen">(), nur dass im Falle eines unerlaubten Schlüssels
538 keine Exception geworfen, sondern C<undef> geliefert wird.
539  
540 =cut
541  
542 # -----------------------------------------------------------------------------
543  
544 7 ▶sub try {
Subroutine try: called CC=5 SCAR=16.1
Pod documented
545 7 my $self = shift;
546 # @_: @keys
547  
548 7 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 1 6 7
549 1 my @arr;
550 1 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 1 3
551 2 my $key = shift;
552 2 ▶ push @arr,CORE::exists $self->{$key}? $self->{$key}: undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{$key} ? : 2 0 2
553 }
554 1 return @arr;
555 }
556  
557 6 ▶ return CORE::exists $self->{$_[0]}? $self->{$_[0]}: undef
Branch: 1 branch
BranchTrueFalseTotal
exists $$self{$_[0]} ? : 5 1 6
558 }
559  
560 # -----------------------------------------------------------------------------
561  
562 =head3 set() - Setze Schlüssel/Wert-Paare
563  
564 =head4 Synopsis
565  
566 $h->set(@keyVal);
567  
568 =head4 Description
569  
570 Setze die angegebenen Schlüssel/Wert-Paare.
571  
572 Alternative Formulierung:
573  
574 $h->{$key} = $val; # ein Schlüssel/Wert-Paar
575 @{$h}{@keys} = @vals; # mehrere Schlüssel/Wert-Paare
576  
577 =cut
578  
579 # -----------------------------------------------------------------------------
580  
581 1359 ▶sub set {
Subroutine set: called CC=2 SCAR=6.9
Pod documented
582 1359 my $self = shift;
583 # @_: @keyVal
584  
585 # Hash mit freiem Zugriff
586  
587 1359 $SetCount++;
588 1359 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6040 1076 7116
589 6040 my $key = shift;
590 6040 $self->{$key} = shift;
591 # eval {$self->{$key} = shift};
592 # if ($@) {
593 # $self->throw($@);
594 # }
595 }
596  
597 1076 return;
598 }
599  
600 # -----------------------------------------------------------------------------
601  
602 =head3 add() - Setze Schlüssel/Wert-Paare ohne Exception
603  
604 =head4 Synopsis
605  
606 $val = $h->add($key=>$val);
607 @vals = $h->add(@keyVal);
608  
609 =head4 Description
610  
611 Wie L<set|"set() - Setze Schlüssel/Wert-Paare">(), nur dass im Falle eines unerlaubten Schlüssels keine
612 Exception generiert, sondern der Hash um das Schlüssel/Wert-Paar
613 erweitert wird.
614  
615 =cut
616  
617 # -----------------------------------------------------------------------------
618  
619 281 ▶sub add {
Subroutine add: called CC=3 SCAR=11.0
Pod documented
620 281 my $self = shift;
621 # @_: @keyVal
622  
623 # Test auf einen gelockten Hash funktioniert erst ab 5.18.0,
624 # daher arbeiten wir hier mit eval{}.
625  
626 281 local $@;
627 281 my @arr = eval {$self->set(@_)};
628 281 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 280 1 281
629 280 $self->unlockKeys;
630 280 @arr = $self->set(@_);
631 280 $self->lockKeys;
632 }
633  
634 281 ▶ return wantarray? @arr: $arr[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 281 281
635 }
636  
637 # -----------------------------------------------------------------------------
638  
639 =head3 memoize() - Cache Wert auf berechnetem Attribut
640  
641 =head4 Synopsis
642  
643 $val = $h->memoize($key,$sub);
644  
645 =head4 Description
646  
647 Besitzt das Attribut $key einen Wert, liefere ihn. Andernfalls
648 berechne den Wert mittels der Subroutine $sub und cache ihn
649 auf dem Attribut.
650  
651 Die Methode ist nützlich, um in Objektmethoden eingebettet zu werden,
652 die einen berechneten Wert liefern, der nicht immer wieder neu
653 gerechnet werden soll.
654  
655 Alternative Formulierungen:
656  
657 $val = $h->{$key} //= $h->$sub($key);
658  
659 oder
660  
661 $val = $h->{$key} //= do {
662 # Implementierung der Subroutine
663 };
664  
665 =cut
666  
667 # -----------------------------------------------------------------------------
668  
669 12 ▶sub memoize {
Subroutine memoize: called CC=2 SCAR=7.1
Pod documented
670 12 my ($self,$key,$sub) = @_;
671 12 ▶ return $self->{$key} //= $self->$sub($key);
Condition66%2/3
$self->{$key} //= $self->$sub($key)
l!l&&r!l&&!r
660
MC/DC0%0/2
$self->{$key} //= $self->$sub($key)
$self->{$key} $self->$sub($key)
Truth table66%2/3
A //= B
A= $self->{$key}B= $self->$sub($key)
ABresult
1X1
011
000
672 }
673  
674 # -----------------------------------------------------------------------------
675  
676 =head3 memoizeWeaken() - Cache schwache Referenz auf berechnetem Attribut
677  
678 =head4 Synopsis
679  
680 $ref = $h->memoizeWeaken($key,$sub);
681  
682 =head4 Description
683  
684 Wie memozize(), nur dass $sub eine Referenz liefert, die
685 von der Methode automatisch zu einer schwachen Referenz gemacht wird.
686  
687 Bei nicht-existenter Referenz kann die Methode $sub einen Leerstring
688 liefern. Dieser wird auf C<undef> abgebildet.
689  
690 =cut
691  
692 # -----------------------------------------------------------------------------
693  
694 0 ▶sub memoizeWeaken {
Subroutine memoizeWeaken: not called CC=4 SCAR=30.0
Pod documented
695 0 my ($self,$key,$sub) = @_;
696  
697 # MEMO: Variable $ref wird hier benötigt, damit die erzeugte
698 # Referenz bei Aufruf von weaken() nicht gleich wieder
699 # destrukturiert wird!
700  
701 0 my $ref = $self->{$key};
702 0 ▶ if (!defined $ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ref) 0 0 0
703 0 $ref = $self->$sub($key);
704 0 ▶ if ($ref ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref ne "") 0 0 0
705 0 $self->weaken($key=>$ref);
706 }
707 }
708  
709 0 ▶ return ref $ref? $ref: undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $ref ? : 0 0 0
710 }
711  
712 # -----------------------------------------------------------------------------
713  
714 =head3 compute() - Wende Subroutine auf Schlüssel/Wert-Paar an
715  
716 =head4 Synopsis
717  
718 $val = $h->compute($key,$sub);
719  
720 =head4 Description
721  
722 Wende Subroutine $sub auf den Wert des Schlüssels $key an. Die
723 Subroutine hat die Struktur:
724  
725 sub {
726 my ($h,$key) = @_;
727 ...
728 return $val;
729 }
730  
731 Der Rückgabewert der Subroutine wird an Schlüssel $key zugewiesen.
732  
733 =head4 Example
734  
735 Methode L<increment|"increment() - Inkrementiere (Integer-)Wert">() mit apply() realisiert:
736  
737 $val = $h->compute($key,sub {
738 my ($h,$key) = @_;
739 return $h->{$key}+1; # nicht $h->{$key}++!
740 });
741  
742 =cut
743  
744 # -----------------------------------------------------------------------------
745  
746 1 ▶sub compute {
Subroutine compute: called CC=1 SCAR=0.0
Pod documented
747 1 my ($self,$key,$sub) = @_;
748 1 return $self->{$key} = $sub->($self,$key);
749 }
750  
751 # -----------------------------------------------------------------------------
752  
753 =head2 Automatische Akzessoren
754  
755 =head3 AUTOLOAD() - Erzeuge Akzessor-Methode
756  
757 =head4 Synopsis
758  
759 $val = $h->AUTOLOAD;
760 $val = $h->AUTOLOAD($val);
761  
762 =head4 Description
763  
764 Erzeuge eine Akzessor-Methode für eine Hash-Komponente. Die Methode
765 AUTOLOAD() wird für jede Hash-Komponente einmal aufgerufen.
766 Danach gehen alle Aufrufe für die Komponente direkt an die erzeugte
767 Akzessor-Methode.
768  
769 Die Methode AUTOLOAD() erweitert ihre Klasse um automatisch
770 generierte Akzessor-Methoden. D.h. für jede Komponente des Hash
771 wird bei Bedarf eine Methode erzeugt, durch die der Wert der
772 Komponente manipuliert werden kann. Dadurch ist es möglich, die
773 Manipulation von Attributen ohne Programmieraufwand nahtlos
774 in die Methodenschnittstelle einer Klasse zu integrieren.
775  
776 Gegenüberstellung:
777  
778 Hash-Zugriff get()/set() Methoden-Zugriff
779 -------------------- ----------------------- --------------------
780 $name = $h->{'name'} $name = $h->get('name') $name = $h->name
781 $h->{'name'} = $name $h->set(name=>$name) $h->name($name) -or-
782 $h->name = $name
783  
784 In der letzten Spalte ("Methoden-Zugriff") steht die Syntax der
785 automatisch generierten Akzessor-Methoden.
786  
787 Die Akzessor-Methode wird als lvalue-Methode generiert, d.h. die
788 Hash-Komponente kann per Akzessor-Aufruf manipuliert werden. Beispiele:
789  
790 $h->name = $name;
791 $h->name =~ s/-//g;
792  
793 Die Erzeugung einer Akzessor-Methode erfolgt (vom Aufrufer unbemerkt)
794 beim ersten Aufruf. Danach wird die Methode unmittelbar gerufen.
795  
796 Der Zugriff über eine automatisch generierte Attributmethode ist ca. 30%
797 schneller als über $h->L<get|"get() - Werte abfragen">().
798  
799 =cut
800  
801 # -----------------------------------------------------------------------------
802  
803 1376 ▶sub AUTOLOAD :lvalue {
Subroutine AUTOLOAD: called CC=5 SCAR=16.4
804 1376 my $this = shift;
805 # @_: Methodenargumente
806  
807 1376 my ($key) = our $AUTOLOAD =~ /::(\w+)$/;
808 1376 ▶ return if $key !~ /[^A-Z]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /[^A-Z]/ 1290 86 1376
809  
810 # Klassenmethoden generieren wir nicht
811  
812 86 ▶ if (!ref $this) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $this) 0 86 86
813 0 $this->throw(
814 'HASH-00002: Klassen-Methode existiert nicht',
815 Method => $key,
816 );
817 }
818  
819 # Methode nur generieren, wenn Attribut existiert
820  
821 86 ▶ if (!exists $this->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $this->{$key}) 1 85 86
822 1 ▶ $this->throw(
Branch: 1 branch
BranchTrueFalseTotal
ref $this ? : 1 0 1
823 'HASH-00001: Hash key or object method does not exist',
824 Attribute => $key,
825 Class => ref($this)? ref($this): $this,
826 );
827 }
828  
829 # Attribut-Methode generieren. Da $self ein Restricted Hash ist,
830 # brauchen wir die Existenz des Attributs nicht selbst prüfen.
831  
832 270 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
833 85 ▶ *{$AUTOLOAD} = sub :lvalue {
Subroutine __ANON__: called CC=2 SCAR=7.4
834 527 my $self = shift;
835 # @_: $val
836  
837 # if (!exists $self->{$key}) {
838 # die Quiq::Stacktrace->asString;
839 # }
840  
841 527 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 527 527
842 0 $self->{$key} = shift;
843 }
844  
845 527 return $self->{$key};
846 85 };
847  
848 # Methode aufrufen
849 85 return $this->$key(@_);
850 }
851  
852 # -----------------------------------------------------------------------------
853  
854 =head2 Schlüssel
855  
856 =head3 keys() - Liste der Schlüssel
857  
858 =head4 Synopsis
859  
860 @keys|$keyA = $h->keys;
861  
862 =head4 Description
863  
864 Liefere die Liste aller Schlüssel. Die Liste ist unsortiert.
865 Im Skalarkontext liefere eine Referenz auf die Liste.
866  
867 Die Reihenfolge der Schlüssel ist undefiniert.
868  
869 Alternative Formulierung:
870  
871 @keys = keys %$h;
872  
873 =cut
874  
875 # -----------------------------------------------------------------------------
876  
877 5 ▶sub keys {
Subroutine keys: called CC=2 SCAR=6.9
Pod documented
878 5 my $self = shift;
879 5 my @keys = CORE::keys %$self;
880 5 ▶ return wantarray? @keys: \@keys;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 3 5
881 }
882  
883 # -----------------------------------------------------------------------------
884  
885 =head3 hashSize() - Anzahl der Schlüssel
886  
887 =head4 Synopsis
888  
889 $n = $h->hashSize;
890  
891 =head4 Description
892  
893 Liefere die Anzahl der Schlüssel/Wert-Paare des Hash.
894  
895 Alternative Formulierung:
896  
897 $n = keys %$h;
898  
899 =cut
900  
901 # -----------------------------------------------------------------------------
902  
903 5 ▶sub hashSize {
Subroutine hashSize: called CC=1 SCAR=0.0
Pod documented
904 5 my $self = shift;
905 5 return scalar CORE::keys %$self;
906 }
907  
908 # -----------------------------------------------------------------------------
909  
910 =head3 validateKeys() - Überprüfe Hash-Schlüssel
911  
912 =head4 Synopsis
913  
914 $class->validateKeys(\%hash,\@keys);
915 $class->validateKeys(\%hash,\%keys);
916  
917 =head4 Description
918  
919 Prüfe die Schlüssel des Hash %hash gegen die Schlüssel in Array
920 @keys bzw. Hash %keys. Enthält %hash einen Schlüssel, der nicht in
921 @keys bzw. %keys vorkommt, wird eine Exception geworfen.
922  
923 =cut
924  
925 # -----------------------------------------------------------------------------
926  
927 6 ▶sub validateKeys {
Subroutine validateKeys: called CC=4 SCAR=14.0
Pod documented
928 6 my ($class,$h,$arg) = @_;
929  
930 6 my $refH;
931 6 ▶ if (Scalar::Util::reftype($arg) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($arg) eq 'ARRAY') { } 6 0 6
932 6 @$refH{@$arg} = (1) x @$arg;
933 }
934 else {
935 0 $refH = $arg;
936 }
937 6 for my $key (CORE::keys %$h) {
938 13 ▶ if (!exists $refH->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $refH->{$key}) 1 12 13
939 1 $class->throw(
940 'HASH-00099: Unzulässiger Hash-Schlüssel',
941 Key => $key,
942 );
943 }
944 }
945
946 5 return;
947 }
948  
949 # -----------------------------------------------------------------------------
950  
951 =head2 Kopieren
952  
953 =head3 copy() - Kopiere Hash
954  
955 =head4 Synopsis
956  
957 $h2 = $h->copy;
958 $h2 = $h->copy(@keyVal);
959  
960 =head4 Description
961  
962 Kopiere Hash, d.h. instantiiere einen neuen Hash mit den
963 gleichen Schlüssel/Wert-Paaren. Es wird I<nicht> rekursiv kopiert,
964 sondern eine "shallow copy" erzeugt.
965  
966 Sind Schlüssel/Wert-Paare @keyVal angegeben, werden
967 diese nach dem Kopieren per L<set|"set() - Setze Schlüssel/Wert-Paare">() auf dem neuen Hash gesetzt.
968  
969 =cut
970  
971 # -----------------------------------------------------------------------------
972  
973 3 ▶sub copy {
Subroutine copy: called CC=3 SCAR=11.1
Pod documented
974 3 my $self = shift;
975 # @_: @keyVal
976  
977 3 my %hash = %$self;
978 3 my $h = bless \%hash,ref $self;
979 3 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 3 3
980 0 $h->set(@_);
981 }
982 3 ▶ if ($self->isLocked) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isLocked) 1 2 3
983 1 $h->lockKeys;
984 }
985  
986 3 return $h;
987 }
988  
989 # -----------------------------------------------------------------------------
990  
991 =head3 join() - Füge Hash hinzu
992  
993 =head4 Synopsis
994  
995 $h = $h->join(\%hash);
996  
997 =head4 Returns
998  
999 Hash (für Method Chaining)
1000  
1001 =head4 Description
1002  
1003 Überschreibe die Schlüssel/Wert-Paare in Hash $h mit den
1004 Schlüssel/Wert-Paaren aus Hash %hash. Schlüssel/Wert-Paare
1005 in Hash $h, die in Hash %hash nicht vorkommen, bleiben bestehen.
1006 Enthält %hash einen Schlüssel, der in $h nicht vorkommt, wird eine
1007 Exception geworfen.
1008  
1009 =head4 Example
1010  
1011 Ein Hash-Objekt mit vorgegebenen Attributen aus einem anoymen Hash
1012 erzeugen. Der anonyme Hash darf weniger, aber nicht mehr Attribute
1013 enthalten:
1014  
1015 $h = Quiq::Hash->new([qw/
1016 name
1017 label
1018 width
1019 height
1020 /])->join(\%hash);
1021  
1022 =cut
1023  
1024 # -----------------------------------------------------------------------------
1025  
1026 5 ▶sub join {
Subroutine join: called CC=2 SCAR=6.9
Pod documented
1027 5 my ($self,$hash) = @_;
1028  
1029 5 for my $key (CORE::keys %$hash) {
1030 5 $self->{$key} = $hash->{$key};
1031 }
1032
1033 5 return $self;
1034 }
1035  
1036 # -----------------------------------------------------------------------------
1037  
1038 =head2 Löschen
1039  
1040 =head3 delete() - Entferne Schlüssel/Wert-Paare
1041  
1042 =head4 Synopsis
1043  
1044 $h->delete(@keys);
1045  
1046 =head4 Description
1047  
1048 Entferne die Schlüssel @keys (und ihre Werte) aus dem Hash. An der Menge
1049 der zulässigen Schlüssel ändert sich dadurch nichts!
1050  
1051 Alternative Formulierung:
1052  
1053 delete $h->{$key}; # einzelner Schlüssel
1054 delete @{$h}{@keys}; # mehrere Schlüssel
1055  
1056 =cut
1057  
1058 # -----------------------------------------------------------------------------
1059  
1060 2 ▶sub delete {
Subroutine delete: called CC=2 SCAR=6.9
Pod documented
1061 2 my $self = shift;
1062 # @_: @keys
1063  
1064 2 for (@_) {
1065 3 CORE::delete $self->{$_};
1066 }
1067  
1068 2 return;
1069 }
1070  
1071 # -----------------------------------------------------------------------------
1072  
1073 =head3 clear() - Leere Hash
1074  
1075 =head4 Synopsis
1076  
1077 $h->clear;
1078  
1079 =head4 Description
1080  
1081 Leere Hash, d.h. entferne alle Schlüssel/Wert-Paare.
1082  
1083 Alternative Formulierung:
1084  
1085 %$h = ();
1086  
1087 =cut
1088  
1089 # -----------------------------------------------------------------------------
1090  
1091 2 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
1092 2 my $self = shift;
1093 2 %$self = ();
1094 2 return;
1095 }
1096  
1097 # -----------------------------------------------------------------------------
1098  
1099 =head2 Externe Repräsentation
1100  
1101 =head3 asString() - Darstellung in einer Zeile
1102  
1103 =head4 Synopsis
1104  
1105 $str = $h->asString;
1106  
1107 =head4 Alias
1108  
1109 asLine()
1110  
1111 =head4 Description
1112  
1113 Liefere den Hash in der Darstellung:
1114  
1115 KEY1=>VAL1;KEY2=>VAL2;...
1116  
1117 Die Schlüssel sind in alphanumerischer Reihenfolge.
1118  
1119 =cut
1120  
1121 # -----------------------------------------------------------------------------
1122  
1123 0 ▶sub asString {
Subroutine asString: not called CC=4 SCAR=30.0
Pod documented
1124 0 my $self = shift;
1125  
1126 0 my $str = '';
1127 0 my @keys = CORE::keys %$self;
1128 0 for my $key (sort @keys) {
1129 0 ▶ if ($str) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str) 0 0 0
1130 0 $str .= ';';
1131 }
1132 0 ▶ my $val = $self->{$key} // 'undef';
Condition0%0/2
$self->{$key} // "undef"
l!l
00
MC/DC0%0/1
$self->{$key} // "undef"
$self->{$key}
Truth table0%0/2
A
A= $self->{$key}
Aresult
11
00
1133 0 $str .= "$key=>$val";
1134 }
1135  
1136 0 return $str;
1137 }
1138  
1139 {
1140 270 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
1141 270 *asLine = \&asString;
1142 }
1143  
1144 # -----------------------------------------------------------------------------
1145  
1146 =head2 Tests
1147  
1148 =head3 exists() - Prüfe Schlüssel auf Existenz
1149  
1150 =head4 Synopsis
1151  
1152 $bool = $h->exists($key);
1153  
1154 =head4 Description
1155  
1156 Prüfe, ob der angegebene Schlüssel im Hash existiert. Wenn ja,
1157 liefere I<wahr>, andernfalls I<falsch>.
1158  
1159 Alternative Formulierung:
1160  
1161 $bool = exists $self->{$key};
1162  
1163 =cut
1164  
1165 # -----------------------------------------------------------------------------
1166  
1167 12 ▶sub exists {
Subroutine exists: called CC=2 SCAR=6.9
Pod documented
1168 12 my ($self,$key) = @_;
1169 12 ▶ return CORE::exists $self->{$key}? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{$key} ? : 4 8 12
1170 }
1171  
1172 # -----------------------------------------------------------------------------
1173  
1174 =head3 defined() - Prüfe Wert auf Existenz
1175  
1176 =head4 Synopsis
1177  
1178 $bool = $h->defined($key);
1179  
1180 =head4 Description
1181  
1182 Prüfe, ob der angegebene Schlüssel im Hash einen Wert hat. Wenn ja,
1183 liefere I<wahr>, andernfalls I<falsch>.
1184  
1185 Alternative Formulierung:
1186  
1187 $bool = defined $h->{$key};
1188  
1189 =cut
1190  
1191 # -----------------------------------------------------------------------------
1192  
1193 2 ▶sub defined {
Subroutine defined: called CC=2 SCAR=6.9
Pod documented
1194 2 my ($self,$key) = @_;
1195 2 ▶ return CORE::defined $self->{$key}? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{$key} ? : 1 1 2
1196 }
1197  
1198 # -----------------------------------------------------------------------------
1199  
1200 =head3 isEmpty() - Prüfe auf leeren Hash
1201  
1202 =head4 Synopsis
1203  
1204 $bool = $h->isEmpty;
1205  
1206 =head4 Description
1207  
1208 Prüfe, ob der Hash leer ist. Wenn ja, liefere I<wahr>,
1209 andernfalls I<falsch>.
1210  
1211 Alternative Formulierung:
1212  
1213 $bool = %$h;
1214  
1215 =cut
1216  
1217 # -----------------------------------------------------------------------------
1218  
1219 2 ▶sub isEmpty {
Subroutine isEmpty: called CC=2 SCAR=6.9
Pod documented
1220 2 my $self = shift;
1221 2 ▶ return CORE::keys(%$self) == 0? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
CORE::keys %$self == 0 ? : 1 1 2
1222 }
1223  
1224 # -----------------------------------------------------------------------------
1225  
1226 =head2 Sperren
1227  
1228 =head3 isLocked() - Prüfe, ob Hash gesperrt ist
1229  
1230 =head4 Synopsis
1231  
1232 $bool = $h->isLocked;
1233  
1234 =head4 Description
1235  
1236 Prüfe, ob der Hash gelockt ist. Wenn ja, liefere I<wahr>,
1237 andernfalls I<falsch>.
1238  
1239 =cut
1240  
1241 # -----------------------------------------------------------------------------
1242  
1243 6 ▶sub isLocked {
Subroutine isLocked: called CC=4 SCAR=20.7
Pod documented
1244 6 my $self = shift;
1245  
1246 6 ▶ if ($] < 5.018) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.018") 0 6 6
1247 0 local $@;
1248 # Ohne $r gibt es eine Warning
1249 0 my $r = eval {$self->{'this_key_does_not_exist'}};
1250 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1251 0 return 1;
1252 }
1253 0 delete $self->{'this_key_does_not_exist'};
1254 0 return 0;
1255 }
1256  
1257 6 ▶ return Hash::Util::hashref_unlocked($self)? 0: 1;
Branch: 1 branch
BranchTrueFalseTotal
Hash::Util::hashref_unlocked($self) ? : 3 3 6
1258 }
1259  
1260 # -----------------------------------------------------------------------------
1261  
1262 =head3 lockKeys() - Sperre Hash
1263  
1264 =head4 Synopsis
1265  
1266 $h = $h->lockKeys;
1267  
1268 =head4 Description
1269  
1270 Sperre den Hash. Anschließend kann kein weiterer Schlüssel zugegriffen
1271 werden. Wird dies versucht, wird eine Exception geworfen.
1272  
1273 Alternative Formulierung:
1274  
1275 Hash::Util::lock_keys(%$h);
1276  
1277 Die Methode liefert eine Referenz auf den Hash zurück.
1278  
1279 =cut
1280  
1281 # -----------------------------------------------------------------------------
1282  
1283 1620 ▶sub lockKeys {
Subroutine lockKeys: called CC=1 SCAR=0.0
Pod documented
1284 1620 my $self = shift;
1285 1620 Hash::Util::lock_keys(%$self);
1286 1620 return $self;
1287 }
1288  
1289 # -----------------------------------------------------------------------------
1290  
1291 =head3 unlockKeys() - Entsperre Hash
1292  
1293 =head4 Synopsis
1294  
1295 $h = $h->unlockKeys;
1296  
1297 =head4 Description
1298  
1299 Entsperre den Hash. Anschließend kann der Hash uneingeschränkt
1300 manipuliert werden. Die Methode liefert eine Referenz auf den Hash
1301 zurück. Damit kann der Hash gleich nach der Instantiierung
1302 entsperrt werden:
1303  
1304 return Quiq::Hash->new(...)->unlockKeys;
1305  
1306 Alternative Formulierung:
1307  
1308 Hash::Util::unlock_keys(%$h);
1309  
1310 =cut
1311  
1312 # -----------------------------------------------------------------------------
1313  
1314 465 ▶sub unlockKeys {
Subroutine unlockKeys: called CC=1 SCAR=0.0
Pod documented
1315 465 my $self = shift;
1316 465 Hash::Util::unlock_keys(%$self);
1317 465 return $self;
1318 }
1319  
1320 # -----------------------------------------------------------------------------
1321  
1322 =head2 Sonstiges
1323  
1324 =head3 arraySize() - Größe des referenzierten Arrays
1325  
1326 =head4 Synopsis
1327  
1328 $n = $h->arraySize($key);
1329  
1330 =cut
1331  
1332 # -----------------------------------------------------------------------------
1333  
1334 3 ▶sub arraySize {
Subroutine arraySize: called CC=3 SCAR=11.0
Pod documented
1335 3 my ($self,$key) = @_;
1336  
1337 3 ▶ if (!defined $self->{$key}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $self->{$key}) { } 1 2 3
elsif (builtin::reftype($self->{$key}) eq 'ARRAY') { } 1 1 2
1338 1 return 0;
1339 }
1340 elsif (Scalar::Util::reftype($self->{$key}) eq 'ARRAY') {
1341 1 return @{$self->{$key}};
1342 }
1343
1344 $self->throw(
1345 1 'HASH-00005: Keine Array-Referenz',
1346 Key => $key,
1347 Class => ref($self),
1348 );
1349 }
1350  
1351 # -----------------------------------------------------------------------------
1352  
1353 =head3 setOrPush() - Setze Skalare Komponente oder pushe auf Array-Komponente
1354  
1355 =head4 Synopsis
1356  
1357 $h->setOrPush($key=>$arg);
1358  
1359 =head4 Arguments
1360  
1361 =over 4
1362  
1363 =item $key
1364  
1365 Skalare oder Array-Komponente des Hash
1366  
1367 =item $arg
1368  
1369 Skalarer Wert oder Array-Referenz
1370  
1371 =back
1372  
1373 =head4 Description
1374  
1375 Ist $key eine skalare Komponente des Hash, setze das Attribut auf $arg.
1376 Ist $key eine Array-Komponente des Hash, pushe $arg (wenn skalarer Wert)
1377 oder @$arg (wenn Array-Referenz) auf das Array.
1378  
1379 =cut
1380  
1381 # -----------------------------------------------------------------------------
1382  
1383 15 ▶sub setOrPush {
Subroutine setOrPush: called CC=3 SCAR=11.0
Pod documented
1384 15 my ($self,$key,$arg) = @_;
1385  
1386 15 my $val = $self->{$key};
1387 15 ▶ if (ref $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val) { } 11 4 15
1388 11 ▶ CORE::push @$val,ref $arg? @$arg: $arg;
Branch: 1 branch
BranchTrueFalseTotal
ref $arg ? : 2 9 11
1389 }
1390 else {
1391 4 $self->{$key} = $arg;
1392 }
1393  
1394 15 return;
1395 }
1396  
1397 # -----------------------------------------------------------------------------
1398  
1399 =head3 push() - Füge Werte zu Arraykomponente hinzu
1400  
1401 =head4 Synopsis
1402  
1403 $h->push($key,@values);
1404  
1405 =head4 Arguments
1406  
1407 =over 4
1408  
1409 =item $key
1410  
1411 Arraykomponente.
1412  
1413 =item @values
1414  
1415 Werte, die zum Array hinzugefügt werden.
1416  
1417 =back
1418  
1419 =head4 Description
1420  
1421 Füge Werte @values zur Arraykomponente $key hinzu. Die Methode
1422 liefert keinen Wert zurück.
1423  
1424 =cut
1425  
1426 # -----------------------------------------------------------------------------
1427  
1428 29 ▶sub push {
Subroutine push: called CC=1 SCAR=0.0
Pod documented
1429 29 my $self = shift;
1430 29 my $key = shift;
1431 # @_: @values
1432  
1433 29 CORE::push @{$self->{$key}},@_;
1434  
1435 29 return;
1436 }
1437  
1438 # -----------------------------------------------------------------------------
1439  
1440 =head3 unshift() - Füge Element am Anfang zu Arraykomponente hinzu
1441  
1442 =head4 Synopsis
1443  
1444 $h->unshift($key,$val);
1445  
1446 =head4 Arguments
1447  
1448 =over 4
1449  
1450 =item $key
1451  
1452 Arraykomponente.
1453  
1454 =item $val
1455  
1456 Wert, der zum Array hinzugefügt wird.
1457  
1458 =back
1459  
1460 =head4 Description
1461  
1462 Füge Wert $val am Anfang zur Arraykomponente $key hinzu. Die
1463 Methode liefert keinen Wert zurück.
1464  
1465 =cut
1466  
1467 # -----------------------------------------------------------------------------
1468  
1469 0 ▶sub unshift {
Subroutine unshift: not called CC=1 SCAR=6.9
Pod documented
1470 0 my ($self,$key,$val) = @_;
1471 0 CORE::unshift @{$self->{$key}},$val;
1472 0 return;
1473 }
1474  
1475 # -----------------------------------------------------------------------------
1476  
1477 =head3 increment() - Inkrementiere (Integer-)Wert
1478  
1479 =head4 Synopsis
1480  
1481 $n = $h->increment($key);
1482  
1483 =head4 Description
1484  
1485 Inkrementiere (Integer-)Wert zu Schlüssel $key und liefere das
1486 Resultat zurück.
1487  
1488 Alternative Formulierung:
1489  
1490 $n = ++$h->{$key};
1491  
1492 =cut
1493  
1494 # -----------------------------------------------------------------------------
1495  
1496 3 ▶sub increment {
Subroutine increment: called CC=1 SCAR=0.0
Pod documented
1497 3 my ($self,$key) = @_;
1498 3 return ++$self->{$key};
1499 }
1500  
1501 # -----------------------------------------------------------------------------
1502  
1503 =head3 addNumber() - Addiere numerischen Wert
1504  
1505 =head4 Synopsis
1506  
1507 $y = $h->addNumber($key,$x);
1508  
1509 =head4 Description
1510  
1511 Addiere numerischen Wert $x zum Wert des Schlüssels $key hinzu und
1512 liefere das Resultat zurück.
1513  
1514 Alternative Formulierung:
1515  
1516 $y = $h->{$key} += $x;
1517  
1518 =cut
1519  
1520 # -----------------------------------------------------------------------------
1521  
1522 1 ▶sub addNumber {
Subroutine addNumber: called CC=1 SCAR=0.0
Pod documented
1523 1 my ($self,$key,$x) = @_;
1524 1 return $self->{$key} += $x;
1525 }
1526  
1527 # -----------------------------------------------------------------------------
1528  
1529 =head3 keyVal() - Liste von Schlüssel/Wert-Paaren
1530  
1531 =head4 Synopsis
1532  
1533 @keyVal | $keyValA = $h->keyVal;
1534  
1535 =head4 Description
1536  
1537 Liefere die Liste der Schlüssel/Wert-Paare. Im Skalarkontext liefere eine
1538 Referenz auf die Liste.
1539  
1540 =cut
1541  
1542 # -----------------------------------------------------------------------------
1543  
1544 0 ▶sub keyVal {
Subroutine keyVal: not called CC=3 SCAR=24.8
Pod documented
1545 0 my $self = shift;
1546  
1547 0 my @arr;
1548 0 my @keys = CORE::keys %$self;
1549 0 for my $key (sort @keys) {
1550 0 CORE::push @arr,$key,$self->{$key};
1551 }
1552  
1553 0 ▶ return wantarray? @arr: \@arr;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
1554 }
1555  
1556 # -----------------------------------------------------------------------------
1557  
1558 =head3 weaken() - Erzeuge schwache Referenz
1559  
1560 =head4 Synopsis
1561  
1562 $ref = $h->weaken($key);
1563 $ref = $h->weaken($key=>$ref);
1564  
1565 =head4 Description
1566  
1567 Mache die Referenz von Schlüssel $key zu einer schwachen Referenz
1568 und liefere sie zurück. Ist eine Referenz $ref als Parameter angegeben,
1569 setze die Referenz zuvor.
1570  
1571 =cut
1572  
1573 # -----------------------------------------------------------------------------
1574  
1575 219 ▶sub weaken {
Subroutine weaken: called CC=2 SCAR=7.2
Pod documented
1576 219 my $self = shift;
1577 219 my $key = shift;
1578 # @_: $ref
1579  
1580 219 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 219 219
1581 0 $self->{$key} = shift;
1582 }
1583 219 Scalar::Util::weaken($self->{$key});
1584  
1585 219 return $self->{$key};
1586 }
1587  
1588 # -----------------------------------------------------------------------------
1589  
1590 =head2 Interna
1591  
1592 =head3 buckets() - Ermittele Bucket-Anzahl
1593  
1594 =head4 Synopsis
1595  
1596 $n = $h->buckets;
1597  
1598 =head4 Description
1599  
1600 Liefere die Anzahl der Hash-Buckets.
1601  
1602 =cut
1603  
1604 # -----------------------------------------------------------------------------
1605  
1606 1 ▶sub buckets {
Subroutine buckets: called CC=2 SCAR=7.0
Pod documented
1607 1 my $self = shift;
1608  
1609 # scalar(%hash) liefert 0, wenn der Hash leer ist, andernfalls
1610 # $x/$n, wobei $n die Anzahl der zur Verfügung stehenden Buckets ist
1611 # und $x die Anzahl der genutzten Buckets. Um die Bucketanzahl
1612 # eines leeren Hash zu ermitteln, müssen wir also temporär ein
1613 # Element hinzufügen.
1614  
1615 1 my $n;
1616 1 ▶ unless ($n = scalar %$self) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n = scalar %$self) 1 0 1
1617 1 $self->add(this_is_a_pseudo_key=>1);
1618 1 $n = scalar %$self;
1619 1 $self->delete('this_is_a_pseudo_key');
1620 }
1621 1 $n =~ s|.*/||;
1622  
1623 1 return $n;
1624 }
1625  
1626 # -----------------------------------------------------------------------------
1627  
1628 =head3 bucketsUsed() - Anzahl der genutzten Buckets
1629  
1630 =head4 Synopsis
1631  
1632 $n = $h->bucketsUsed;
1633  
1634 =head4 Description
1635  
1636 Liefere die Anzahl der genutzten Hash-Buckets.
1637  
1638 =cut
1639  
1640 # -----------------------------------------------------------------------------
1641  
1642 2 ▶sub bucketsUsed {
Subroutine bucketsUsed: called CC=2 SCAR=6.9
Pod documented
1643 2 my $self = shift;
1644  
1645 2 my $n = scalar %$self;
1646 2 ▶ if ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 1 1 2
1647 1 $n =~ s|/.*||;
1648 }
1649  
1650 2 return $n;
1651 }
1652  
1653 # -----------------------------------------------------------------------------
1654  
1655 =head3 getCount() - Anzahl der get-Aufrufe
1656  
1657 =head4 Synopsis
1658  
1659 $n = $this->getCount;
1660  
1661 =head4 Description
1662  
1663 Liefere die Anzahl der get-Aufrufe seit Start des Programms.
1664  
1665 =cut
1666  
1667 # -----------------------------------------------------------------------------
1668  
1669 0 ▶sub getCount {
Subroutine getCount: not called CC=1 SCAR=6.9
Pod documented
1670 0 my $self = shift;
1671 0 return $GetCount;
1672 }
1673  
1674 # -----------------------------------------------------------------------------
1675  
1676 =head3 setCount() - Anzahl der set-Aufrufe
1677  
1678 =head4 Synopsis
1679  
1680 $n = $this->setCount;
1681  
1682 =head4 Description
1683  
1684 Liefere die Anzahl der set-Aufrufe seit Start des Programms.
1685  
1686 =cut
1687  
1688 # -----------------------------------------------------------------------------
1689  
1690 0 ▶sub setCount {
Subroutine setCount: not called CC=1 SCAR=6.9
Pod documented
1691 0 my $self = shift;
1692 0 return $SetCount;
1693 }
1694  
1695 # -----------------------------------------------------------------------------
1696  
1697 =head1 DETAILS
1698  
1699 =head2 Benchmark
1700  
1701 Anzahl Zugriffe pro CPU-Sekunde im Vergleich zwischen verschiedenen
1702 Zugriffsmethoden:
1703  
1704 A - Hash: $h->{$k}
1705 B - Hash: eval{$h->{$k}}
1706 C - Restricted Hash: $h->{$k}
1707 D - Restricted Hash: eval{$h->{$k}}
1708 E - Quiq::Hash: $h->{$k}
1709 F - Quiq::Hash: $h->get($k)
1710
1711 Rate F D B E C A
1712 F 1401111/s -- -71% -74% -82% -83% -84%
1713 D 4879104/s 248% -- -8% -37% -40% -44%
1714 B 5297295/s 278% 9% -- -32% -35% -39%
1715 E 7803910/s 457% 60% 47% -- -4% -11%
1716 C 8104988/s 478% 66% 53% 4% -- -7%
1717 A 8745272/s 524% 79% 65% 12% 8% --
1718  
1719 Den Hash via $h->L<get|"get() - Werte abfragen">() zuzugreifen (F) ist ca. 85% langsamer
1720 als der einfachste Hash-Lookup (A). Wird auf den Methodenaufruf
1721 verzichtet und per $h->{$key} zugegriffen (E), ist der Zugriff nur
1722 11% langsamer. Es ist also ratsam, intern per $h->{$key}
1723 zuzugreifen. Per $h->get() können immerhin 1.400.000 Lookups pro
1724 CPU-Sekunde ausgeführt werden. Bei nicht-zugriffsintensiven
1725 Anwendungen ist das sicherlich schnell genug. Die Anzahl der
1726 Aufrufe von $h->get() und $h->set() wird intern gezählt und kann
1727 per $class->L<getCount|"getCount() - Anzahl der get-Aufrufe">() und $class->L<setCount|"setCount() - Anzahl der set-Aufrufe">() abgefragt
1728 werden.
1729  
1730 Das Benchmark-Programm (bench-hash):
1731  
1732 #!/usr/bin/env perl
1733
1734 use strict;
1735 use warnings;
1736
1737 use Benchmark;
1738 use Hash::Util;
1739 use Quiq::Hash;
1740
1741 my $h1 = {0=>'a',1=>'b',2=>'c',3=>'d',4=>'e',5=>'f'};
1742 my $h2 = Hash::Util::lock_ref_keys({0=>'a',1=>'b',2=>'c',3=>'d',4=>'e',5=>'f'});
1743 my $h3 = Quiq::Hash->new({0=>'a',1=>'b',2=>'c',3=>'d',4=>'e',5=>'f'});
1744
1745 my $i = 0;
1746 Benchmark::cmpthese(-10,{
1747 A => sub {
1748 $h1->{$i++%5};
1749 },
1750 B => sub {
1751 eval{$h1->{$i++%5}};
1752 },
1753 C => sub {
1754 $h2->{$i++%5};
1755 },
1756 D => sub {
1757 eval{$h2->{$i++%5}};
1758 },
1759 E => sub {
1760 $h3->{$i++%5};
1761 },
1762 F => sub {
1763 $h3->get($i++%5);
1764 },
1765 });
1766  
1767 =head1 VERSION
1768  
1769 1.240
1770  
1771 =head1 AUTHOR
1772  
1773 Frank Seitz, L<http://fseitz.de/>
1774  
1775 =head1 COPYRIGHT
1776  
1777 Copyright (C) 2026 Frank Seitz
1778  
1779 =head1 LICENSE
1780  
1781 This code is free software; you can redistribute it and/or modify
1782 it under the same terms as Perl itself.
1783  
1784 =cut
1785  
1786 # -----------------------------------------------------------------------------
1787  
1788 270 1;
1789  
1790 # eof
« Quiq/Sftp/Client.pm ↑ Summary Quiq/Perl.pm »