| 1 | package Zoidberg::DispatchTable; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 65 | our $VERSION = '0.981'; | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 65 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 65 | ▶ | use Zoidberg::Utils qw/debug bug error/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 65 | ▶ | use Exporter::Tidy all => [qw/stack wipe tag tags/]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 65 | our $ERROR_CALLER = 1; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # reserved keys _AUTOLOAD and _META | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # $self->[0] hash with arrays of dispatch strings/refs | ||||||||||||||||||||||||||||
| 14 | # $self->[1] hash with arrays of tags | ||||||||||||||||||||||||||||
| 15 | # $self->[2] object ref | ||||||||||||||||||||||||||||
| 16 | # $self->[3] object can parent bit | ||||||||||||||||||||||||||||
| 17 | # $self->[4] array with keys to keep them in order | ||||||||||||||||||||||||||||
| 18 | # $self->[5] iteration index for keys() | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # keys are kept in order to avoid inconsistencies | ||||||||||||||||||||||||||||
| 21 | # for example when iterating trough {parser} | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 184 | ▶ | sub new { # create a blessed AND tie'ed hash | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | 184 | my $class = shift; | |||||||||||||||||||||||||||
| 25 | 184 | my %hash; | |||||||||||||||||||||||||||
| 26 | 184 | tie %hash, $class, @_; | |||||||||||||||||||||||||||
| 27 | 184 | bless \%hash, $class; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 316 | ▶ | sub TIEHASH { | ||||||||||||||||||||||||||
|
Subroutine
TIEHASH: called CC=5 SCAR=16.6
| |||||||||||||||||||||||||||||
| 31 | 316 | my $class = shift; | |||||||||||||||||||||||||||
| 32 | 316 | ▶ | my $ref = shift || error 'need object ref to tie hash'; | ||||||||||||||||||||||||||
|
Condition33%1/3
shift() || error('need object ref to tie hash')
MC/DC0%0/2
shift() || error('need object ref to tie hash')
shift()
error('need object ref to tie hash')
Truth table33%1/3
A || B
A= shift()B= error('need object ref to tie hash')
| |||||||||||||||||||||||||||||
| 33 | # $ref is either array ref or object ref | ||||||||||||||||||||||||||||
| 34 | 316 | ▶ | my $self = (ref($ref) eq 'ARRAY') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | ? $ref | ||||||||||||||||||||||||||||
| 36 | : [{}, {}, $ref, $ref->can('parent'), [], 0]; | ||||||||||||||||||||||||||||
| 37 | 316 | bless $self, $class; | |||||||||||||||||||||||||||
| 38 | 316 | ▶ | while (my $hash = shift @_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 253 | $self->STORE($_, $$hash{$_}) for keys %$hash; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 316 | return $self; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 1578 | ▶ | sub STORE { | ||||||||||||||||||||||||||
|
Subroutine
STORE: called CC=8 SCAR=21.4
| |||||||||||||||||||||||||||||
| 45 | 1578 | my ($self, $key, $value) = @_; | |||||||||||||||||||||||||||
| 46 | 1578 | my $tag = 'undef'; | |||||||||||||||||||||||||||
| 47 | 1578 | ▶ | ($value, $tag) = @$value if ref($value) eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 1578 | my $t = ref $value; | |||||||||||||||||||||||||||
| 50 | 1578 | ▶ | if ($t eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 51 | 130 | ▶ | unless (tied $value) { # recurs tie'ing | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 130 | tie %$value, __PACKAGE__, $$self[2], $value; | |||||||||||||||||||||||||||
| 53 | # be careful to reuse same ref - else perl bugs :( | ||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | # else just store the tied hash | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | elsif (! $t) { | ||||||||||||||||||||||||||||
| 58 | 1142 | $value =~ s/(^\s*|\s*$)//g; | |||||||||||||||||||||||||||
| 59 | 1142 | ▶ | error "Can't use ==>$value<== as subroutine." | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not length $value or $value =~ /^\$/
MC/DC0%0/2
not length $value or $value =~ /^\$/
not length $value
$value =~ /^\$/
Truth table33%1/3
not(A) or B
A= not length $valueB= $value =~ /^\$/
| |||||||||||||||||||||||||||||
| 60 | if ! length $value | ||||||||||||||||||||||||||||
| 61 | or $value =~ /^\$/; # no vars | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 0 | elsif ($t ne 'CODE') { bug "Can't store ref of type $t in DispatchTable" } | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 1578 | push @{$self->[0]{$key}}, $value; | |||||||||||||||||||||||||||
| 66 | 1578 | push @{$self->[1]{$key}}, $tag; | |||||||||||||||||||||||||||
| 67 | 1578 | push @{$self->[4]}, $key; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | sub add { | ||||||||||||||||||||||||||
|
Subroutine
add: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 0 | my $self = tied %{ shift() }; | |||||||||||||||||||||||||||
| 72 | 0 | $self->STORE(@_); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 1020 | ▶ | sub FETCH { | ||||||||||||||||||||||||||
|
Subroutine
FETCH: called CC=8 SCAR=21.4
| |||||||||||||||||||||||||||||
| 76 | 1020 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 77 | 1020 | ▶ | if ( exists $$self[0]{$key} and scalar @{$$self[0]{$key}} ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%2/2
exists $self->[0]{$key} and scalar @{$self->[0]{$key};}
MC/DC100%1/1
exists $self->[0]{$key} and scalar @{$self->[0]{$key};}
exists $self->[0]{$key}
Truth table100%2/2
A
A= exists $self->[0]{$key}
| |||||||||||||||||||||||||||||
| 78 | $$self[0]{$key}[-1] = $self->convert($self->[0]{$key}[-1]) | ||||||||||||||||||||||||||||
| 79 | 580 | ▶ | unless ref $self->[0]{$key}[-1]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 580 | return $self->[0]{$key}[-1]; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | elsif ($self->EXISTS('_AUTOLOAD')) { | ||||||||||||||||||||||||||||
| 83 | 128 | my $sub; | |||||||||||||||||||||||||||
| 84 | 128 | for (@{$self->[0]{_AUTOLOAD}}) { | |||||||||||||||||||||||||||
| 85 | 128 | $sub = $_->($key); | |||||||||||||||||||||||||||
| 86 | 128 | ▶ | next unless $sub; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | $self->STORE($key, $sub) unless $self->EXISTS($key); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | return $self->FETCH($key); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 440 | return undef; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 38 | ▶ | sub convert { | ||||||||||||||||||||||||||
|
Subroutine
convert: called CC=8 SCAR=23.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 95 | 38 | my ($self, $ding) = @_; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 38 | ▶ | if ($ding =~ /^\s*sub\s*{.*}\s*$/) { # undocumented hack | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 0 | debug "going to eval: $ding"; | |||||||||||||||||||||||||||
| 99 | 0 | my $closure = eval $ding; | |||||||||||||||||||||||||||
| 100 | 0 | ▶ | die if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | return sub { $closure->($$self[2], @_) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 38 | $ding =~ s#^->((\w+)->)?# | |||||||||||||||||||||||||||
| 105 | 35 | ▶ | ( $self->[3] ? q/parent->/ : '' ) . | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 106 | ( $1 ? qq/{objects}{$2}->/ : '' ) | ||||||||||||||||||||||||||||
| 107 | #e; | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | if ($ding =~ /\(\s*\)$/s) { $ding =~ s/\s*\)$/\@_\)/ } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 110 | 6 | elsif ($ding =~ /\(.*\)$/s) { $ding =~ s/\)$/, \@_\)/ } | |||||||||||||||||||||||||||
| 111 | 32 | else { $ding .= '(@_)' } | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 38 | debug "going to eval: sub { \$self->[2]->$ding }"; | |||||||||||||||||||||||||||
| 114 | 38 | my $sub = eval "sub { \$\$self[2]->$ding }"; | |||||||||||||||||||||||||||
| 115 | 38 | ▶ | die if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 38 | return $sub; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 1529195 | ▶ | sub EXISTS { exists $_[0][0]->{$_[1]} and scalar @{$_[0][0]->{$_[1]}} } | ||||||||||||||||||||||||||
|
Subroutine
EXISTS: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 1 | ▶ | sub DELETE { # doesn't really delete, merely pops | ||||||||||||||||||||||||||
|
Subroutine
DELETE: called CC=3 SCAR=13.0
| |||||||||||||||||||||||||||||
| 122 | 1 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 123 | 1 | ▶ | return undef unless exists $self->[0]{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 1 | pop @{$self->[1]{$key}}; | |||||||||||||||||||||||||||
| 126 | 1 | my $re = pop @{$self->[0]{$key}}; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 1 | ▶ | unless (scalar @{$self->[0]{$key}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 0 | delete $self->[0]{$key}; | |||||||||||||||||||||||||||
| 130 | 0 | delete $self->[1]{$key}; | |||||||||||||||||||||||||||
| 131 | 0 | @{$self->[4]} = grep {$_ ne $key} @{$self->[4]}; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 1 | return $re; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | sub pop { | ||||||||||||||||||||||||||
|
Subroutine
pop: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 138 | 0 | my $self = tied %{ shift() }; | |||||||||||||||||||||||||||
| 139 | 0 | $self->DELETE(@_); | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 1 | ▶ | sub CLEAR { | ||||||||||||||||||||||||||
|
Subroutine
CLEAR: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 143 | 1 | %{$_[0][0]} = (); | |||||||||||||||||||||||||||
| 144 | 1 | %{$_[0][1]} = (); | |||||||||||||||||||||||||||
| 145 | 1 | @{$_[0][4]} = (); | |||||||||||||||||||||||||||
| 146 | 1 | $_[0][5] = 0; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 2 | ▶ | sub FIRSTKEY { | ||||||||||||||||||||||||||
|
Subroutine
FIRSTKEY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 150 | 2 | $_[0][5] = 0; | |||||||||||||||||||||||||||
| 151 | 2 | goto \&NEXTKEY | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 8 | ▶ | sub NEXTKEY { | ||||||||||||||||||||||||||
|
Subroutine
NEXTKEY: called CC=4 SCAR=14.8
| |||||||||||||||||||||||||||||
| 155 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 156 | 8 | ▶ | if ($$self[5] > $#{$$self[4]}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 157 | 2 | $$self[5] = 0; | |||||||||||||||||||||||||||
| 158 | 2 | ▶ | return wantarray ? () : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | elsif (wantarray) { # ($key, $value) = each(%table) | ||||||||||||||||||||||||||||
| 161 | 0 | my $key = $$self[4][$$self[5]++]; | |||||||||||||||||||||||||||
| 162 | 0 | return $key, $self->FETCH($key); | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | 6 | else { return $self->[4][$$self[5]++] } # for $key (keys %table) | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 5644 | ▶ | sub stack { | ||||||||||||||||||||||||||
|
Subroutine
stack: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 168 | 5644 | my $self = tied %{ shift() }; | |||||||||||||||||||||||||||
| 169 | 5644 | my ($key, $use_tag) = @_; | |||||||||||||||||||||||||||
| 170 | 5644 | ▶ | return () unless exists $$self[0]{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 2232 | ▶ | for (@{$self->[0]{$key}}) { $_ = $self->convert($_) unless ref $_ } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 2232 | ▶ | return map [ $$self[0]{$key}[$_], $$self[1]{$key}[$_] ], (0..$#{$$self[0]{$key}}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | if $use_tag; | ||||||||||||||||||||||||||||
| 174 | 2231 | return @{$self->[0]{$key}}; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 0 | ▶ | sub tag { | ||||||||||||||||||||||||||
|
Subroutine
tag: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 178 | 0 | my $self = tied %{ shift() }; | |||||||||||||||||||||||||||
| 179 | 0 | my $key = shift; | |||||||||||||||||||||||||||
| 180 | 0 | ▶ | return undef unless exists $$self[1]{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 0 | return $$self[1]{$key}[-1]; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 0 | ▶ | sub tags { | ||||||||||||||||||||||||||
|
Subroutine
tags: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 185 | 0 | my $self = tied %{ shift() }; | |||||||||||||||||||||||||||
| 186 | 0 | my $key = shift; | |||||||||||||||||||||||||||
| 187 | 0 | ▶ | return undef unless exists $$self[1]{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 0 | return @{$self->[1]{$key}}; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 1 | ▶ | sub wipe { | ||||||||||||||||||||||||||
|
Subroutine
wipe: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 192 | 1 | my $self = tied %{ shift() }; | |||||||||||||||||||||||||||
| 193 | 1 | my ($tag, @keys) = @_; | |||||||||||||||||||||||||||
| 194 | 1 | ▶ | @keys = keys %{$self->[0]} unless scalar @keys; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | 1 | my %old; | |||||||||||||||||||||||||||
| 196 | 1 | for my $key (@keys) { | |||||||||||||||||||||||||||
| 197 | 5 | ▶ | for (my $i = 0; $i < @{$self->[1]{$key}}; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 5 | ▶ | next unless $self->[1]{$key}[$i] eq $tag; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | 2 | $old{$key} = [$self->[0]{$key}[$i], $tag]; | |||||||||||||||||||||||||||
| 200 | 2 | $self->[0]{$key}[$i] = undef; | |||||||||||||||||||||||||||
| 201 | 2 | $self->[1]{$key}[$i] = undef; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | 5 | @{$self->[0]{$key}} = grep {defined $_} @{$self->[0]{$key}}; | |||||||||||||||||||||||||||
| 204 | 5 | @{$self->[1]{$key}} = grep {defined $_} @{$self->[1]{$key}}; | |||||||||||||||||||||||||||
| 205 | 5 | ▶ | unless (scalar @{$self->[0]{$key}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | 2 | delete $self->[0]{$key}; | |||||||||||||||||||||||||||
| 207 | 2 | delete $self->[1]{$key}; | |||||||||||||||||||||||||||
| 208 | 2 | @{$self->[4]} = grep {$_ ne $key} @{$self->[4]}; | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | 1 | return \%old; | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 65 | 1; | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | __END__ | ||||||||||||||||||||||||||||