| 1 | package Data::Iter; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | $Carp::Verbose = 1; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | require Exporter; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | our %EXPORT_TAGS = ( 'all' => [ qw(iter counter COUNTER LAST_COUNTER value VALUE key KEY get GET getnext GETNEXT GETPREV IS_LAST IS_FIRST transform_array_to_hash) ] ); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | our @EXPORT = qw(); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | our $VERSION = '0.2'; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # Preloaded methods go here. | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | #prototype of ($$) is important for sort functions | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | sub sort_alpha($$) { $_[0] cmp $_[1] } | ||||||||||||||||||||||||||
|
Subroutine
sort_alpha: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | sub sort_num($$) { $_[0] <=> $_[1] } | ||||||||||||||||||||||||||
|
Subroutine
sort_num: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | our $cref_sort = sub { $a cmp $b }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | sub default_sort { $cref_sort->(@_) } | ||||||||||||||||||||||||||
|
Subroutine
default_sort: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | our $Sort = 'default_sort'; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1 | our $options = { }; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | sub _KEY { 0 } | ||||||||||||||||||||||||||
|
Subroutine
_KEY: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | sub _VALUE { 1 } | ||||||||||||||||||||||||||
|
Subroutine
_VALUE: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | sub _COUNT { 2 } | ||||||||||||||||||||||||||
|
Subroutine
_COUNT: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 43 | 0 | ▶ | sub _KEY_REF { 3 } | ||||||||||||||||||||||||||
|
Subroutine
_KEY_REF: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | sub _VALUE_REF { 5 } | ||||||||||||||||||||||||||
|
Subroutine
_VALUE_REF: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | sub _LIST_REF { 6 } | ||||||||||||||||||||||||||
|
Subroutine
_LIST_REF: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | sub iter | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
iter: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | my $this = shift if ( ref $_[0] || $_[0] ) eq __PACKAGE__; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $_[0] || $_[0]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0]
$_[0]
Truth table0%0/3
A || B
A= ref $_[0]B= $_[0]
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | my $cnt = 0; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | my $ref_data = shift; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | my @result = (); | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | unless( @_ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | { | ||||||||||||||||||||||||||||
| 59 | 0 | ▶ | if( ref $ref_data eq 'HASH' ) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 60 | { | ||||||||||||||||||||||||||||
| 61 | 0 | foreach my $key ( sort $Sort keys %{ $ref_data } ) | |||||||||||||||||||||||||||
| 62 | { | ||||||||||||||||||||||||||||
| 63 | 0 | my $obj = []; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | @$obj[ _KEY, _VALUE, _COUNT, _KEY_REF, _VALUE_REF, _LIST_REF ] = ( $key, $ref_data->{$key}, $cnt, \$key, \( $ref_data->{$key}, \@result ) ); | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | push @result, bless $obj, __PACKAGE__; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | $cnt++; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | elsif( ref $ref_data eq 'ARRAY' ) | ||||||||||||||||||||||||||||
| 73 | { | ||||||||||||||||||||||||||||
| 74 | 0 | @result = (); | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | foreach my $value ( @$ref_data ) | |||||||||||||||||||||||||||
| 77 | { | ||||||||||||||||||||||||||||
| 78 | 0 | my $obj = []; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | @$obj[ _KEY, _VALUE, _COUNT, _VALUE_REF, _LIST_REF ] = ( $cnt, $value, $cnt, \( $ref_data->[$cnt] ), \@result ); | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | push @result, bless $obj, __PACKAGE__; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | $cnt++; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | else | ||||||||||||||||||||||||||||
| 88 | { | ||||||||||||||||||||||||||||
| 89 | 0 | croak "iter() only accepts reference to ARRAY or HASH. Found: ". ref( $ref_data ); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | else | ||||||||||||||||||||||||||||
| 93 | { | ||||||||||||||||||||||||||||
| 94 | 0 | croak "iter() only accepts one parameter (reference to ARRAY or HASH). Found extra args: ", scalar @_; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | return @result; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | sub _handle_this | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_handle_this: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 102 | 0 | my $this; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # called as method ? | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | $this = shift @{$_[0]} if ( ref $_[0]->[0] || defined $_[0]->[0] ) eq __PACKAGE__; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $_[0][0] || defined $_[0][0]
MC/DC0%0/2
ref $_[0][0] || defined $_[0][0]
ref $_[0][0]
defined $_[0][0]
Truth table0%0/3
A || B
A= ref $_[0][0]B= defined $_[0][0]
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # no, so use $_ as obj | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | $this = $_ unless $this; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | return $this; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | sub counter | ||||||||||||||||||||||||||||
| 118 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
counter: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 119 | 0 | my $this = _handle_this( \@_ ); | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | return $this->[_COUNT]; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | ▶ | sub COUNTER { goto &counter } | ||||||||||||||||||||||||||
|
Subroutine
COUNTER: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | sub value | ||||||||||||||||||||||||||||
| 127 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
value: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 128 | 0 | my $this = _handle_this( \@_ ); | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # set value if argument given | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | $this->[_VALUE] = ${ $this->[_VALUE_REF] } = $_[0] if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | return $this->[_VALUE]; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | sub VALUE { goto &value } | ||||||||||||||||||||||||||
|
Subroutine
VALUE: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | sub key | ||||||||||||||||||||||||||||
| 140 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
key: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 141 | 0 | my $this = _handle_this( \@_ ); | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # set value if argument given | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | ▶ | $this->[_VALUE] = ${ $this->[_KEY_REF] } = $_[0] if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | return $this->[_KEY]; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 0 | ▶ | sub KEY { goto &key } | ||||||||||||||||||||||||||
|
Subroutine
KEY: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | sub get | ||||||||||||||||||||||||||||
| 153 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
get: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 154 | 0 | my $this = _handle_this( \@_ ); | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | ▶ | my $pos = shift || -1; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || -1
MC/DC0%0/1
shift() || -1
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | return $this->[_LIST_REF]->[$pos]; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 0 | ▶ | sub GET { goto &get } | ||||||||||||||||||||||||||
|
Subroutine
GET: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | sub getnext | ||||||||||||||||||||||||||||
| 164 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
getnext: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 165 | 0 | my $this = _handle_this( \@_ ); | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 0 | my $pos = -1; | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 0 | my $result = $this->get( $this->counter+1 ); | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 0 | ▶ | return $result unless $result; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 0 | return $result; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 0 | ▶ | sub GETNEXT { goto &getnext } | ||||||||||||||||||||||||||
|
Subroutine
GETNEXT: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | sub GETPREV | ||||||||||||||||||||||||||||
| 179 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
GETPREV: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 180 | 0 | my $this = _handle_this( \@_ ); | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 0 | return $this->getnext( $this->counter-1 ); | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | sub LAST_COUNTER | ||||||||||||||||||||||||||||
| 186 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
LAST_COUNTER: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 187 | 0 | my $this = _handle_this( \@_ ); | |||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 0 | return scalar @{ $this->[_LIST_REF] } - 1; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | sub IS_LAST | ||||||||||||||||||||||||||||
| 193 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
IS_LAST: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 194 | 0 | my $this = _handle_this( \@_ ); | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 0 | return $this->COUNTER == $this->LAST_COUNTER; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | sub IS_FIRST | ||||||||||||||||||||||||||||
| 200 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
IS_FIRST: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 201 | 0 | my $this = _handle_this( \@_ ); | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 0 | return $this->COUNTER == 0; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | sub pair | ||||||||||||||||||||||||||||
| 209 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
pair: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 210 | 0 | my $this = _handle_this( \@_ ); | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 0 | ▶ | if( $this->COUNTER() % 2 == 0 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 214 | { | ||||||||||||||||||||||||||||
| 215 | 0 | return ( $this->VALUE, $this->GETNEXT->VALUE ); | |||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 0 | return (); | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 0 | ▶ | sub PAIR { goto &pair } | ||||||||||||||||||||||||||
|
Subroutine
PAIR: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | # some nice service functions | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | sub transform_array_to_hash | ||||||||||||||||||||||||||||
| 226 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
transform_array_to_hash: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 227 | 0 | my $array = shift; | |||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 0 | my $result; | |||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 0 | foreach ( iter $array ) | |||||||||||||||||||||||||||
| 233 | { | ||||||||||||||||||||||||||||
| 234 | 0 | ▶ | if( COUNTER() % 2 == 0 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | { | ||||||||||||||||||||||||||||
| 236 | #printfln q{%s => %s}, VALUE, GETNEXT->VALUE; | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 0 | ▶ | if( exists $result->{ VALUE() } ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 239 | { | ||||||||||||||||||||||||||||
| 240 | 0 | push @{ $result->{ VALUE() } }, GETNEXT->VALUE; | |||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | else | ||||||||||||||||||||||||||||
| 243 | { | ||||||||||||||||||||||||||||
| 244 | 0 | $result->{ VALUE() } = [ GETNEXT->VALUE ]; | |||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 0 | return $result; | |||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | 1 | 1; | |||||||||||||||||||||||||||
| 253 | __END__ | ||||||||||||||||||||||||||||