| 1 | package Array::Assign; | ||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use base qw(Exporter); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | our @EXPORT = qw( | |||||||||
| 6 | arry_assign_i arry_assign_s | ||||||||||
| 7 | arry_extract_i arry_extract_s | ||||||||||
| 8 | ); | ||||||||||
| 9 | |||||||||||
| 10 | 2 | our $IDX_MAX = 1000; | |||||||||
| 11 | 2 | our $VERSION = 0.01; | |||||||||
| 12 | |||||||||||
| 13 | 2 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||
| 14 | 2 | my ($cls,$idx_map) = @_; | |||||||||
| 15 | 2 | my $self = {}; | |||||||||
| 16 | 2 | ▶ | if(ref $idx_map eq 'HASH') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 17 | 0 | %$self = %$idx_map; | |||||||||
| 18 | } else { | ||||||||||
| 19 | 2 | my @idx_arry; | |||||||||
| 20 | 2 | ▶ | if(ref $idx_map eq 'ARRAY') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | 0 | @idx_arry = @$idx_map; | |||||||||
| 22 | } else { | ||||||||||
| 23 | 2 | @idx_arry = @_[1..$#_]; | |||||||||
| 24 | } | ||||||||||
| 25 | 2 | @{$self}{@idx_arry} = (0..$#idx_arry); | |||||||||
| 26 | } | ||||||||||
| 27 | 2 | bless $self, $cls; | |||||||||
| 28 | 2 | return $self; | |||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | 3 | ▶ | sub assign_s { | ||||||||
|
Subroutine
assign_s: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 32 | 3 | my ($self,$target,%fields) = @_; | |||||||||
| 33 | 3 | &arry_assign_s($target, $self, %fields); | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 2 | ▶ | sub assign_i { | ||||||||
|
Subroutine
assign_i: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 37 | 2 | shift; | |||||||||
| 38 | 2 | goto &arry_assign_i; | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 2 | ▶ | sub extract_s { | ||||||||
|
Subroutine
extract_s: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 42 | 2 | my ($self,$source,%fields) = @_; | |||||||||
| 43 | 2 | &arry_extract_s($source, $self, %fields); | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 2 | ▶ | sub extract_i { | ||||||||
|
Subroutine
extract_i: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 47 | 2 | shift; | |||||||||
| 48 | 2 | goto &arry_extract_i; | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | |||||||||||
| 52 | 5 | ▶ | sub arry_assign_s(\@$%) { | ||||||||
|
Subroutine
arry_assign_s: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 53 | 5 | my ($target,$mapping,%assignments) = @_; | |||||||||
| 54 | 5 | ▶ | while (my ($name,$value) = each %assignments) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 11 | my $idx = $mapping->{$name}; | |||||||||
| 56 | 11 | ▶ | die("Unknown name '$name'") unless defined $idx; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 11 | $target->[$idx] = $value; | |||||||||
| 58 | } | ||||||||||
| 59 | 5 | $target; | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | sub arry_assign_i(\@%) | ||||||||||
| 63 | 4 | ▶ | { | ||||||||
|
Subroutine
arry_assign_i: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 64 | 4 | my ($target,%mappings) = @_; | |||||||||
| 65 | 4 | _idx_sanity_check(%mappings); | |||||||||
| 66 | |||||||||||
| 67 | 4 | @{$target}[keys %mappings] = values %mappings; | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | |||||||||||
| 71 | sub arry_extract_i(\@%) | ||||||||||
| 72 | 4 | ▶ | { | ||||||||
|
Subroutine
arry_extract_i: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 73 | 4 | my ($source,%targets) = @_; | |||||||||
| 74 | 4 | _idx_sanity_check(%targets); | |||||||||
| 75 | 4 | ▶ | while ( my ($idx,$ref) = each %targets) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 8 | $$ref = $source->[$idx]; | |||||||||
| 77 | } | ||||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | 4 | ▶ | sub arry_extract_s(\@$%) { | ||||||||
|
Subroutine
arry_extract_s: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||
| 81 | 4 | my ($source,$mappings,%targets) = @_; | |||||||||
| 82 | 4 | ▶ | while (my ($name,$ref) = each %targets) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 9 | my $idx = $mappings->{$name}; | |||||||||
| 84 | 9 | ▶ | if(!defined $idx) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 0 | die("unknown parameter '$name'"); | |||||||||
| 86 | } | ||||||||||
| 87 | 9 | $$ref = $source->[$idx]; | |||||||||
| 88 | } | ||||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | 8 | ▶ | sub _idx_sanity_check { | ||||||||
|
Subroutine
_idx_sanity_check: called CC=2 SCAR=8.1
| |||||||||||
| 92 | 8 | my %mappings = @_; | |||||||||
| 93 | 8 | ▶ | if(scalar (grep $_ > $IDX_MAX, keys %mappings) > 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | 0 | die("Abnormally large index found. ". | |||||||||
| 95 | "Bump up \$IDX_MAX if this is not a mistake"); | ||||||||||
| 96 | } | ||||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | __END__ | ||||||||||