| 1 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | Net::OSCAR::TLV -- tied hash for OSCAR TLVs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | version 1.928 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | Keys in hashes tied to this class will be treated as numbers. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | This class also preserves the ordering of its keys. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | package Net::OSCAR::TLV; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 5 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 5 | $Net::OSCAR::TLV::VERSION = '1.928'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 5 | $REVISION = '$Revision$'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 5 | ▶ | use vars qw(@EXPORT @ISA); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 5 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 5 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 5 | @EXPORT = qw(tlv); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # Extra arguments: an optional scalar which modifies the behavior of $self->{foo}->{bar} = "baz" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | # Iff foo doesn't exist, the scalar will be evaluated and assigned as the value of foo. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | # So, instead of having foo be {bar => "baz"} , it could be another TLV. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | # It will be given the key bar. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | my $pkg = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | my $self = $pkg->TIEHASH(@_); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | sub getorder { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
getorder: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | return map { (unpack("n", $_))[0] } @{$self->{ORDER}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | sub setorder { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
setorder: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | # Anything not specified gets shoved at the end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | my @end = grep { my $inbud = $_; not grep { $_ eq $inbud } @_ } @{$self->{ORDER}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | @{$self->{ORDER}} = map { pack("n", 0+$_) } @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | push @{$self->{ORDER}}, @end; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 5 | ▶ | sub TIEHASH { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
TIEHASH: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 5 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 5 | my $self = { DATA => {}, ORDER => [], CURRKEY => -1, AUTOVIVIFY => shift}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 5 | return bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | sub FETCH { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
FETCH: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | my($self, $key) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | $self->{DATA}->{pack("n", 0+$key)}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 105 | ▶ | sub STORE { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
STORE: called CC=6 SCAR=21.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 105 | my($self, $key, $value) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 105 | my($normalkey) = pack("n", 0+$key); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | #print STDERR "Storing: ", Data::Dumper->Dump([$value], ["${self}->{$key}"]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 105 | ▶ | if(!exists $self->{DATA}->{$normalkey}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 105 | ▶ | if( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%3/9
$self->{"AUTOVIVIFY"} and ref $value eq "HASH" and not tied %$value and scalar keys %$value == 0
$self->{"AUTOVIVIFY"} and ref $value eq "HASH" and not tied %$value
$self->{"AUTOVIVIFY"} and ref $value eq "HASH"
MC/DC0%0/4
$self->{"AUTOVIVIFY"} and ref $value eq "HASH" and not tied %$value and scalar keys %$value == 0
$self->{"AUTOVIVIFY"}
ref $value eq "HASH"
not tied %$value
scalar keys %$value == 0
Truth table20%1/5
A and B and not(C) and D
A= $self->{"AUTOVIVIFY"}B= ref $value eq "HASH"C= not tied %$valueD= scalar keys %$value == 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | $self->{AUTOVIVIFY} and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | ref($value) eq "HASH" and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | !tied(%$value) and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | scalar keys %$value == 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | #print STDERR "Autovivifying $key: $self->{AUTOVIVIFY}\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | eval $self->{AUTOVIVIFY}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | #print STDERR "New value: ", Data::Dumper->Dump([$self->{DATA}->{$normalkey}], ["${self}->{$key}"]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | #print STDERR "Not autovivifying $key.\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | #print STDERR "No autovivify.\n" unless $self->{AUTOVIVIFY}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | #printf STDERR "ref(\$value) eq %s\n", ref($value) unless ref($value) eq "HASH"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | #print STDERR "tied(\%\$value)\n" unless !tied(%$value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | #printf STDERR "scalar keys \%\$value == %d\n", scalar keys %$value unless scalar keys %$value == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 105 | push @{$self->{ORDER}}, $normalkey; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | #print STDERR "Not autovivifying $key: already exists\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 105 | $self->{DATA}->{$normalkey} = $value; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 105 | return $value; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | sub DELETE { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DELETE: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | my($self, $key) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | my($packedkey) = pack("n", 0+$key); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | delete $self->{DATA}->{$packedkey}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | for(my $i = 0; $i < scalar @{$self->{ORDER}}; $i++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | next unless $packedkey eq $self->{ORDER}->[$i]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | splice(@{$self->{ORDER}}, $i, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | # What if the user deletes a key while iterating? We need to correct for the new index. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | if($self->{CURRKEY} != -1 and $i <= $self->{CURRKEY}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"CURRKEY"} != -1 and $i <= $self->{"CURRKEY"}
MC/DC0%0/2
$self->{"CURRKEY"} != -1 and $i <= $self->{"CURRKEY"}
$self->{"CURRKEY"} != -1
$i <= $self->{"CURRKEY"}
Truth table0%0/3
A and B
A= $self->{"CURRKEY"} != -1B= $i <= $self->{"CURRKEY"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | $self->{CURRKEY}--; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | sub CLEAR { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
CLEAR: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | $self->{DATA} = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | $self->{ORDER} = []; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | $self->{CURRKEY} = -1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | sub EXISTS { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
EXISTS: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | my($self, $key) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | my($packedkey) = pack("n", 0+$key); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | return exists $self->{DATA}->{$packedkey}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | sub FIRSTKEY { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
FIRSTKEY: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | $_[0]->{CURRKEY} = -1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | goto &NEXTKEY; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | sub NEXTKEY { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
NEXTKEY: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | my $currkey = ++$self->{CURRKEY}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | if($currkey >= scalar @{$self->{ORDER}}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | return wantarray ? () : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | my $packedkey = $self->{ORDER}->[$currkey]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | my($key) = unpack("n", $packedkey); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | return wantarray ? ($key, $self->{DATA}->{$packedkey}) : $key; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 5 | ▶ | sub tlv(;@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tlv: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 5 | my %tlv = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 5 | tie %tlv, "Net::OSCAR::TLV"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 5 | ▶ | while(@_) { my($key, $value) = (shift, shift); $tlv{$key} = $value; } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 5 | return \%tlv; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||