| 1 | # ----------------------------------------------------------------------------# | ||||||||||||||||||||||||||||
| 2 | # Tie::Coupler # | ||||||||||||||||||||||||||||
| 3 | # # | ||||||||||||||||||||||||||||
| 4 | # Copyright (c) 2001-02 Arun Kumar U <u_arunkumar@yahoo.com>. # | ||||||||||||||||||||||||||||
| 5 | # All rights reserved. # | ||||||||||||||||||||||||||||
| 6 | # # | ||||||||||||||||||||||||||||
| 7 | # This program is free software; you can redistribute it and/or # | ||||||||||||||||||||||||||||
| 8 | # modify it under the same terms as Perl itself. # | ||||||||||||||||||||||||||||
| 9 | # ----------------------------------------------------------------------------# | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | package Tie::Coupler; | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | sub new | ||||||||||||||||||||||||||||
| 21 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 0 | my ($class) = shift; | |||||||||||||||||||||||||||
| 23 | 0 | my ($args) = $_[2]; | |||||||||||||||||||||||||||
| 24 | 0 | my (%sargs, %cargs); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | if (defined($args) && ref($args) ne 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $args and ref $args ne "HASH"
MC/DC0%0/2
defined $args and ref $args ne "HASH"
defined $args
ref $args ne "HASH"
Truth table0%0/3
A and B
A= defined $argsB= ref $args ne "HASH"
| |||||||||||||||||||||||||||||
| 27 | 0 | warn("Invalid options: Should be a hash reference\n"); | |||||||||||||||||||||||||||
| 28 | 0 | croak('Usage: ' . __PACKAGE__ . '->new($scalar1, $scalar2, $opthash)'); | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | $args = {} if (!defined($args)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | %sargs = %cargs = %{$args}; | |||||||||||||||||||||||||||
| 33 | 0 | $sargs{'convert'} = $sargs{'fconvert'}; | |||||||||||||||||||||||||||
| 34 | 0 | $sargs{'this'} = \$_[0]; $sargs{'couple'} = \$_[1]; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | $cargs{'convert'} = $cargs{'rconvert'}; | |||||||||||||||||||||||||||
| 37 | 0 | $cargs{'this'} = \$_[1]; $cargs{'couple'} = \$_[0]; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | my $simpl = tie($_[0], 'Tie::Coupling', \%sargs); | |||||||||||||||||||||||||||
| 40 | 0 | my $cimpl = tie($_[1], 'Tie::Coupling', \%cargs); | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | my $self = {}; | |||||||||||||||||||||||||||
| 43 | 0 | $self->{'_simpl'} = $simpl; | |||||||||||||||||||||||||||
| 44 | 0 | $self->{'_cimpl'} = $cimpl; | |||||||||||||||||||||||||||
| 45 | 0 | $self->{'_source'} = \$_[0]; | |||||||||||||||||||||||||||
| 46 | 0 | $self->{'_couple'} = \$_[1]; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | bless $self, $class; | |||||||||||||||||||||||||||
| 49 | 0 | return $self; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | sub decouple | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
decouple: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 0 | my $var = $_[0]->{'_source'}; | |||||||||||||||||||||||||||
| 55 | 0 | my $couple = $_[0]->{'_couple'}; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | ## Ugly hack to prevent -w from spitting the warning message | ||||||||||||||||||||||||||||
| 58 | ## "untie attempted while 1 inner references still exist" | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | undef($_[0]); | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | untie(${$var}); | |||||||||||||||||||||||||||
| 63 | 0 | untie(${$couple}); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | sub fconvert | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
fconvert: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 0 | my ($self) = shift; | |||||||||||||||||||||||||||
| 69 | 0 | $self->{'_simpl'}->convert(@_); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | sub rconvert | ||||||||||||||||||||||||||||
| 73 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
rconvert: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 0 | my ($self) = shift; | |||||||||||||||||||||||||||
| 75 | 0 | $self->{'_cimpl'}->convert(@_); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 1 | 1; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | package Tie::Coupling; | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 83 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 1 | my @options = qw(this couple convert init); | |||||||||||||||||||||||||||
| 86 | 1 | my @attribs = qw(couple value this init); | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | sub TIESCALAR | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
TIESCALAR: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 90 | 0 | my ($proto, $args) = @_; | |||||||||||||||||||||||||||
| 91 | 0 | my ($self, $class); | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | $self = {}; | |||||||||||||||||||||||||||
| 94 | 0 | ▶ | $class = ref($proto) || $proto; | ||||||||||||||||||||||||||
|
Condition0%0/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||
| 95 | 0 | bless $self, $class; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | map { $self->{"_" . $_} = $args->{$_}; } @options; | |||||||||||||||||||||||||||
| 98 | 0 | $self->_checkOptions(); | |||||||||||||||||||||||||||
| 99 | 0 | $self->_value($self->_this()); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | if ($self->{'_init'}) { $self->STORE($self->_value()); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | return $self; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | sub FETCH | ||||||||||||||||||||||||||||
| 107 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
FETCH: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 108 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 109 | 0 | return $self->_value(); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | sub STORE | ||||||||||||||||||||||||||||
| 113 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
STORE: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 114 | 0 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | my $convert = $self->convert(); | |||||||||||||||||||||||||||
| 117 | 0 | my $pattern = qr{(?i)retain}; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | ▶ | if (!defined($convert)) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 120 | 0 | $self->_value($value); | |||||||||||||||||||||||||||
| 121 | 0 | $self->_couple($value); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | 0 | elsif ($convert =~ $pattern) { $self->_value($value); } | |||||||||||||||||||||||||||
| 124 | elsif (defined($convert)) { | ||||||||||||||||||||||||||||
| 125 | 0 | my $nvalue = $self->_transform($value); | |||||||||||||||||||||||||||
| 126 | 0 | $self->_value($value); | |||||||||||||||||||||||||||
| 127 | 0 | $self->_couple($nvalue); | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | 0 | else { confess("Should have never got this far !!"); } | |||||||||||||||||||||||||||
| 130 | 0 | return $self->_value(); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | sub convert | ||||||||||||||||||||||||||||
| 134 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
convert: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 135 | 0 | my ($self) = shift; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | $self->{'_convert'} = $_[0] if (@_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 0 | return $self->{'_convert'}; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | sub _checkOptions | ||||||||||||||||||||||||||||
| 142 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_checkOptions: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 143 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | my $convert = $self->convert(); | |||||||||||||||||||||||||||
| 146 | 0 | my $ref = ref($convert); | |||||||||||||||||||||||||||
| 147 | 0 | my $pattern = qr{(?i)retain}; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | ▶ | if (defined($convert) && $convert !~ $pattern) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $convert and not $convert =~ /$pattern/
MC/DC0%0/2
defined $convert and not $convert =~ /$pattern/
defined $convert
not $convert =~ /$pattern/
Truth table0%0/3
A and not(B)
A= defined $convertB= not $convert =~ /$pattern/
| |||||||||||||||||||||||||||||
| 150 | 0 | ▶ | if (!($ref eq 'CODE' || $ref eq 'ARRAY')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$ref eq 'CODE' || $ref eq 'ARRAY'
MC/DC0%0/2
$ref eq 'CODE' || $ref eq 'ARRAY'
$ref eq 'CODE'
$ref eq 'ARRAY'
Truth table0%0/3
A || B
A= $ref eq 'CODE'B= $ref eq 'ARRAY'
| |||||||||||||||||||||||||||||
| 151 | 0 | carp("Conversion callback should be either a code reference or an array reference\n"); | |||||||||||||||||||||||||||
| 152 | 0 | croak("Usage: tie \$s, \'" . __PACKAGE__ . | |||||||||||||||||||||||||||
| 153 | "\', { couple => \\\$var, convert => \\&coderef }"); | ||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | sub _value | ||||||||||||||||||||||||||||
| 159 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_value: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 160 | 0 | my ($self) = shift; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | ▶ | $self->{'_value'} = $_[0] if (@_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 0 | return $self->{'_value'}; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | sub _couple | ||||||||||||||||||||||||||||
| 167 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_couple: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 168 | 0 | my ($self) = shift; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 0 | ▶ | ${$self->{'_couple'}} = $_[0] if (@_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 0 | return ${$self->{'_couple'}}; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | sub _this | ||||||||||||||||||||||||||||
| 175 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_this: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 176 | 0 | my ($self) = shift; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 0 | ▶ | ${$self->{'_this'}} = $_[0] if (@_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 0 | return ${$self->{'_this'}}; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | sub _transform | ||||||||||||||||||||||||||||
| 183 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_transform: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 184 | 0 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 185 | 0 | my ($convert, $ref); | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 0 | $convert = $self->convert(); | |||||||||||||||||||||||||||
| 188 | 0 | $ref = ref($convert); | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 0 | ▶ | return $value if (!defined($convert)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 0 | ▶ | if (!($ref eq 'CODE' || $ref eq 'ARRAY')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$ref eq "CODE" or $ref eq "ARRAY"
MC/DC0%0/2
$ref eq "CODE" or $ref eq "ARRAY"
$ref eq "CODE"
$ref eq "ARRAY"
Truth table0%0/3
A or B
A= $ref eq "CODE"B= $ref eq "ARRAY"
| |||||||||||||||||||||||||||||
| 193 | 0 | croak("Conversion callback should be either a CODE reference or an ARRAY reference\n"); | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 0 | ▶ | if ($ref eq 'CODE') { return $convert->($value); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | else { | ||||||||||||||||||||||||||||
| 198 | 0 | ▶ | if (ref($convert->[0]) eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | 0 | my $function = $convert->[0]; | |||||||||||||||||||||||||||
| 200 | 0 | my @params = (@{$convert})[1 .. $#{$convert}]; | |||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 0 | return $function->($value, @params); | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | else { | ||||||||||||||||||||||||||||
| 205 | 0 | my $pack = $convert->[0]; | |||||||||||||||||||||||||||
| 206 | 0 | my $method = $convert->[1]; | |||||||||||||||||||||||||||
| 207 | 0 | my @params = (@{$convert})[2 .. $#{$convert}]; | |||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 0 | return $pack->$method($value, @params); | |||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 1 | 1; | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | __END__; | ||||||||||||||||||||||||||||