blib/lib/Tie/Trace.pm

filter: Statementstmt 92.7% 293 / 316 Branchbran 77.9% 134 / 172 Conditioncond 70.4% 31 / 44 MC/DCmcdc 51.8% 14 / 27 Subroutinesub 88.2% 45 / 51 Podpod 100% 3 / 3 totaltotal 84.8% 520 / 613 CC 109 SCAR 24.9
CC
109
Cov
86%
_output_message
24.2
_carpit
22.8
_data_filter
18.1
CRAP 12.1
↑ Summary
1 package Tie::Trace;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use PadWalker ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Tie::Hash ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Tie::Array ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Tie::Scalar ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Data::Dumper ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use base qw/Exporter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 6 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 SCALAR => 0,
15 SCALARREF => 1,
16 ARRAYREF => 2,
17 HASHREF => 4,
18 BLESSED => 8,
19 TIED => 16,
20 6 };
21  
22 6 our @EXPORT_OK = ('watch');
23 6 our %EXPORT_TAGS = (all => \@EXPORT_OK);
24  
25 6 our %OPTIONS = (debug => 'dumper');
26 6 our $QUIET = 0;
27  
28 6 our $AUTOLOAD;
29  
30 111 ▶sub AUTOLOAD{
Subroutine AUTOLOAD: called CC=2 SCAR=6.9
31 # proxy to Tie::Std***
32 111 my($self, @args) = @_;
33 111 my($class, $method) = (split /::/, $AUTOLOAD)[2, 3];
34 111 my $sub = \&{'Tie::Std' . $class . '::' . $method};
35 111 ▶ defined &$sub ? $sub->($self->{storage}, @args) : return;
Branch: 1 branch
BranchTrueFalseTotal
defined &$sub ? : 104 7 111
36 }
37  
38 19 ▶sub TIEHASH { Tie::Trace::_tieit({}, @_); }
Subroutine TIEHASH: called CC=1 SCAR=0.0
39 6 ▶sub TIEARRAY { Tie::Trace::_tieit([], @_); }
Subroutine TIEARRAY: called CC=1 SCAR=0.0
40 6 ▶sub TIESCALAR{ my $tmp; Tie::Trace::_tieit(\$tmp, @_); }
Subroutine TIESCALAR: called CC=1 SCAR=0.0
41  
42 7 ▶sub watch(\[$@%]@){
Subroutine watch: called CC=13 SCAR=27.5
Pod documented
43 7 my $s = shift;
44 7 my $s_type = ref $s;
45 7 my $s_ = $s;
46  
47 7 ▶ if($s_type eq 'SCALAR'){
Branch: 3 branches
BranchTrueFalseTotal
if ($s_type eq 'SCALAR') { } 0 7 7
elsif ($s_type eq 'ARRAY') { } 1 6 7
elsif ($s_type eq 'HASH') { } 6 0 6
48 0 $s_ = $$s;
49 }elsif($s_type eq 'ARRAY'){
50 1 $s_ = [ @$s ];
51 }elsif($s_type eq 'HASH'){
52 6 $s_ = { %$s };
53 }
54  
55 7 ▶ Carp::croak("must pass one argument.") unless $s;
Branch: 1 branch
BranchTrueFalseTotal
unless $s 0 7 7
56 7 my @options = @_;
57 7 my $var_name;
58 7 eval{
59 7 $var_name = PadWalker::var_name(1, $s);
60 };
61 7 ▶ my $pkg = defined $var_name ? (caller)[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $var_name ? : 7 0 7
62 7 ▶ my $tied_value = tie $s_type eq 'SCALAR' ? $$s : $s_type eq 'ARRAY' ? @$s : %$s, "Tie::Trace", var => $var_name, pkg => $pkg, @options;
Branch: 2 branches
BranchTrueFalseTotal
$s_type eq 'SCALAR' ? : 0 7 7
$s_type eq 'ARRAY' ? : 1 6 7
63 7 local $QUIET = 1;
64  
65 7 ▶ if($s_type eq 'SCALAR'){
Branch: 3 branches
BranchTrueFalseTotal
if ($s_type eq 'SCALAR') { } 0 7 7
elsif ($s_type eq 'ARRAY') { } 1 6 7
elsif ($s_type eq 'HASH') { } 6 0 6
66 0 $$s = $s_;
67 }elsif($s_type eq 'ARRAY'){
68 1 ▶ @$s = @$s_ if @$s_;
Branch: 1 branch
BranchTrueFalseTotal
if @$s_ 0 1 1
69 }elsif($s_type eq 'HASH'){
70 6 ▶ %$s = %$s_ if %$s_;
Branch: 1 branch
BranchTrueFalseTotal
if %$s_ 0 6 6
71 }
72 7 return $tied_value;
73 }
74  
75 47 ▶sub _dumper{
Subroutine _dumper: called CC=1 SCAR=0.0
76 47 my($self, $value) = @_;
77 47 local $Data::Dumper::Terse = 1;
78 47 local $Data::Dumper::Indent = 0;
79 47 local $Data::Dumper::Deparse = 1;
80 47 $value = Data::Dumper::Dumper($value);
81 }
82  
83 0 ▶sub storage{
Subroutine storage: not called CC=1 SCAR=6.9
Pod documented
84 0 my($self) = @_;
85 0 return $self->{storage};
86 }
87  
88 95 ▶sub parent{
Subroutine parent: called CC=1 SCAR=0.0
Pod documented
89 95 my($self) = @_;
90 95 return $self->{parent};
91 }
92  
93 52 ▶sub _match{
Subroutine _match: called CC=3 SCAR=11.8
94 52 my($self, $test, $value) = @_;
95 52 ▶ if(ref $test eq 'Regexp'){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $test eq 'Regexp') { } 7 45 52
elsif (ref $test eq 'CODE') { } 0 45 45
96 7 return $value =~ $_;
97 }elsif(ref $test eq 'CODE'){
98 0 return $test->($self, $value);
99 }else{
100 45 return $test eq $value;
101 }
102 0 return;
103 }
104  
105 107 ▶sub _matching{
Subroutine _matching: called CC=4 SCAR=13.9
106 107 my($self, $test, $tested) = @_;
107 107 ▶ return 1 unless $test;
Branch: 1 branch
BranchTrueFalseTotal
unless $test 78 29 107
108 29 ▶ if($tested){
Branch: 1 branch
BranchTrueFalseTotal
if ($tested) 28 1 29
109 28 ▶ return 1 if grep $self->_match($_, $tested), @$test;
Branch: 1 branch
BranchTrueFalseTotal
if grep $self->_match($_, $tested), @$test 19 9 28
110 }
111 10 return 0;
112 }
113  
114 66 ▶sub _carpit{
Subroutine _carpit: called CC=18 SCAR=31.3
115 66 my($self, %args) = @_;
116 66 ▶ return if $QUIET;
Branch: 1 branch
BranchTrueFalseTotal
if $Tie::Trace::QUIET 0 66 66
117  
118 66 my $class = (split /::/, ref $self)[2];
119 66 ▶ my $op = $self->{options} || {};
Condition50%1/2
$self->{'options'} || {}
l!l
660
MC/DC0%0/1
$self->{'options'} || {}
$self->{'options'}
Truth table50%1/2
A
A= $self->{'options'}
Aresult
11
00
120  
121 # key/value checking
122 66 ▶ if($op->{key} or $op->{value}){
Branch: 1 branch
BranchTrueFalseTotal
if ($op->{"key"} or $op->{"value"}) 20 46 66
Condition100%3/3
$op->{"key"} or $op->{"value"}
l!l&&r!l&&!r
61446
MC/DC100%2/2
$op->{"key"} or $op->{"value"}
$op->{"key"} $op->{"value"}
Truth table100%3/3
A or B
A= $op->{"key"}B= $op->{"value"}
ABresult
1X1
011
000
123 20 my $key = $self->_matching($self->{options}->{key}, $args{key});
124 20 my $value = $self->_matching($self->{options}->{value}, $args{value});
125 20 ▶ if(($args{key} and $op->{key}) and $op->{value}){
Branch: 3 branches
BranchTrueFalseTotal
if ($args{'key'} and $op->{'key'} and $op->{'value'}) { } 0 20 20
elsif ($args{'key'} and $op->{'key'}) { } 6 14 20
elsif ($op->{'value'}) { } 14 0 14
Condition66%4/6
$args{"key"} and $op->{"key"} and $op->{"value"}
!ll&&!rl&&r
1460
$args{"key"} and $op->{"key"}
!ll&&!rl&&r
0146
MC/DC0%0/3
$args{"key"} and $op->{"key"} and $op->{"value"}
$args{"key"} $op->{"key"} $op->{"value"}
Truth table50%2/4
A and B and C
A= $args{"key"}B= $op->{"key"}C= $op->{"value"}
ABCresult
0XX0
10X0
1100
1111
126 0 ▶ return unless $key or $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $key or $value 0 0 0
Condition0%0/3
$key or $value
l!l&&r!l&&!r
000
MC/DC0%0/2
$key or $value
$key $value
Truth table0%0/3
A or B
A= $keyB= $value
ABresult
1X1
011
000
127 ▶ }elsif($args{key} and $op->{key}){
Condition66%2/3
$args{"key"} and $op->{"key"}
!ll&&!rl&&r
0146
MC/DC50%1/2
$args{"key"} and $op->{"key"}
$args{"key"} $op->{"key"}
Truth table66%2/3
A and B
A= $args{"key"}B= $op->{"key"}
ABresult
0X0
100
111
128 6 ▶ return unless $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 2 4 6
129 }elsif($op->{value}){
130 14 ▶ return unless $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 6 8 14
131 }
132 }
133  
134 # debug type
135 58 my $value = $self->_debug_message($args{value}, $op->{debug}, $args{filter});
136 # debug_value checking
137 58 ▶ return unless $self->_matching($self->{options}->{debug_value}, $value);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_matching($self->{"options"}{"debug_value"}, $value) 0 58 58
138 # use scalar/array/hash ?
139 58 ▶ return unless grep lc($class) eq lc($_) , @{$op->{use}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep((lc $class eq lc $_), @{$$op{"use"};}) 0 58 58
140 # create warning message
141 58 my $watch_msg = '';
142 58 my $msg = $self->_output_message($class, $value, \%args);
143 58 ▶ if(defined $self->{options}->{pkg}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'options'}{'pkg'}) { } 56 2 58
144 56 $watch_msg = sprintf("%s:: %s", @{$self->{options}}{qw/pkg var/});
145 }else{
146 2 $msg =~ s/^ => //;
147 }
148 58 warn $watch_msg . $msg . "\n";
149 }
150  
151 58 ▶sub _output_message{
Subroutine _output_message: called CC=22 SCAR=31.8
152 58 my($self, $class, $value, $args) = @_;
153 58 my($msg, @msg) = ('');
154  
155 58 my $caller = $self->{options}->{caller};
156 58 my $_caller_n = 1;
157 58 ▶ while (my $c = (caller $_caller_n)[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = (caller $_caller_n)[0]) 124 0 124
158 124 ▶ if (not $c) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $c) { } 0 124 124
elsif (not $c =~ /^Tie::Trace/) { } 58 66 124
159 0 last;
160 } elsif ($c !~ /^Tie::Trace/) {
161 58 last;
162 }
163 66 $_caller_n++;
164 }
165  
166 0 ▶ my @caller = map $_ + $_caller_n, ref $caller ? @{$caller} : $caller;
Branch: 1 branch
BranchTrueFalseTotal
ref $caller ? : 0 58 58
167 58 my(@filename, @line);
168 58 foreach(@caller){
169 58 my($f, $l) = (caller($_))[1, 2];
170 58 ▶ next unless $f and $l;
Branch: 1 branch
BranchTrueFalseTotal
unless $f and $l 0 58 58
Condition33%1/3
$f and $l
!ll&&!rl&&r
0058
MC/DC0%0/2
$f and $l
$f $l
Truth table33%1/3
A and B
A= $fB= $l
ABresult
0X0
100
111
171  
172 58 push @filename, $f;
173 58 push @line, $l;
174  
175 }
176  
177 58 ▶ my $location = @line == 1 ? " at $filename[0] line $line[0]." :
Branch: 1 branch
BranchTrueFalseTotal
@line == 1 ? : 58 0 58
178 join "\n", map " at $filename[$_] line $line[$_].", (0 .. $#filename);
179 58 my($_p, $p) = ($self, $self->parent);
180 58 ▶ while($p){
Branch: 1 branch
BranchTrueFalseTotal
if ($p) 36 32 68
181 36 my $s_type = ref $p->{storage};
182 36 my $s = $p->{storage};
183 36 ▶ if($s_type eq 'HASH'){
Branch: 2 branches
BranchTrueFalseTotal
if ($s_type eq 'HASH') { } 35 1 36
elsif ($s_type eq 'ARRAY') { } 1 0 1
184 35 push @msg, "{$_p->{__key}}";
185 }elsif($s_type eq 'ARRAY'){
186 1 push @msg, "[$_p->{__point}]";
187 }
188 36 $_p = $p;
189 36 ▶ last if ! ref $p or ! ($p = $p->parent);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $p and $p = $p->parent 26 10 36
Condition66%2/3
ref $p and $p = $p->parent
!ll&&!rl&&r
02610
MC/DC50%1/2
ref $p and $p = $p->parent
ref $p $p = $p->parent
Truth table66%2/3
A and B
A= ref $pB= $p = $p->parent
ABresult
0X0
100
111
190 }
191 58 ▶ $msg = @msg > 0 ? ' => ' . join "", reverse @msg : "";
Branch: 1 branch
BranchTrueFalseTotal
@msg > 0 ? : 26 32 58
192  
193  
194 58 ▶ $value = '' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 58 58
195 58 ▶ if ($class eq 'Scalar') {
Branch: 3 branches
BranchTrueFalseTotal
if ($class eq 'Scalar') { } 2 56 58
elsif ($class eq 'Array') { } 19 37 56
elsif ($class eq 'Hash') { } 37 0 37
196 2 return("${msg} => $value$location");
197 } elsif ($class eq 'Array') {
198 19 ▶ unless(defined $args->{point}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{'point'}) { } 15 4 19
199 4 $msg =~ s/^( => )(.+)$/$1\@\{$2\}/;
200 4 return("$msg => $value$location");
201 }else{
202 15 return("${msg}[$args->{point}] => $value$location");
203 }
204 } elsif ($class eq 'Hash') {
205 37 ▶ return("${msg}" . (! $self->{options}->{pkg} || @msg ? "" : " => "). "{$args->{key}} => $value$location");
Branch: 1 branch
BranchTrueFalseTotal
!$self->{'options'}{'pkg'} || @msg ? : 21 16 37
Condition100%3/3
not $self->{"options"}{"pkg"} or @msg
l!l&&r!l&&!r
21916
MC/DC100%2/2
not $self->{"options"}{"pkg"} or @msg
not $self->{"options"}{"pkg"} @msg
Truth table100%3/3
not(A) or B
A= not $self->{"options"}{"pkg"}B= @msg
ABresult
1X1
011
000
206 }
207 }
208  
209 58 ▶sub _debug_message{
Subroutine _debug_message: called CC=4 SCAR=14.0
210 58 my($self, $value, $debug, $filter) = @_;
211  
212 58 ▶ if(ref $debug eq 'CODE'){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $debug eq 'CODE') { } 0 58 58
elsif (lc $debug eq 'dumper') { } 43 15 58
213 0 $value = $debug->($self, $value);
214 }elsif(lc($debug) eq 'dumper'){
215 43 $value = $self->_dumper($value);
216 43 ▶ if(defined $filter){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filter) 8 35 43
217 8 $filter->($value);
218 }
219 }
220 58 return $value;
221 }
222  
223 31 ▶sub _tieit {
Subroutine _tieit: called CC=11 SCAR=24.0
224 31 my($self, $class, %arg) = @_;
225 31 foreach (keys %OPTIONS){
226 31 ▶ $arg{$_} = $OPTIONS{$_} if not exists $arg{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $arg{$_} 30 1 31
227 }
228  
229 31 ▶ if($class =~/^Tie::Trace$/){
Branch: 1 branch
BranchTrueFalseTotal
if ($class =~ /^Tie::Trace$/) 9 22 31
230 9 my $type = lc(ref $self);
231 9 substr($type, 0, 1) = uc(substr($type, 0, 1));
232 9 $class .= '::' . $type;
233 }
234 31 my $parent = $arg{parent};
235 31 my $options;
236 31 ▶ if(defined $parent and $parent){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parent and $parent) { } 22 9 31
Condition66%2/3
defined $parent and $parent
!ll&&!rl&&r
9022
MC/DC50%1/2
defined $parent and $parent
defined $parent $parent
Truth table66%2/3
A and B
A= defined $parentB= $parent
ABresult
0X0
100
111
237 22 $options = $parent->{options};
238 }else{
239 9 $options = \%arg;
240 9 ▶ unless($options->{use}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($options->{"use"}) 9 0 9
241 9 $options->{use} = [qw/scalar array hash/];
242 }
243 9 ▶ unless(defined $options->{r}){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $options->{"r"}) 5 4 9
244 5 $options->{r} = 1;
245 }
246 9 ▶ $options->{caller} ||= 0;
Condition50%1/2
$options->{"caller"} ||= 0
l!l
09
MC/DC0%0/1
$options->{"caller"} ||= 0
$options->{"caller"}
Truth table50%1/2
A
A= $options->{"caller"}
Aresult
11
00
247 }
248 31 my $_self =
249 {
250 self => $self,
251 parent => $parent,
252 options => $options,
253 };
254 31 ▶ $_self->{__key} = delete $arg{__key} if exists $arg{__key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $arg{"__key"} 20 11 31
255 31 ▶ $_self->{__point} = delete $arg{__point} if exists $arg{__point};
Branch: 1 branch
BranchTrueFalseTotal
if exists $arg{"__point"} 2 29 31
256 31 bless $_self, $class;
257 31 return $_self;
258 }
259  
260 160 ▶sub _data_filter{
Subroutine _data_filter: called CC=18 SCAR=29.0
261 160 my($structure, $self, $parent_info) = @_;
262 160 ▶ return $structure unless $self->{options}->{r};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"options"}{"r"} 7 153 160
263 153 ▶ $parent_info ||= {};
Condition100%2/2
$parent_info ||= {}
l!l
13914
MC/DC100%1/1
$parent_info ||= {}
$parent_info
Truth table100%2/2
A
A= $parent_info
Aresult
11
00
264  
265 153 my $ref = ref $structure;
266 153 my %test = (SCALARREF() => 'SCALAR', ARRAYREF() => 'ARRAY', HASHREF() => 'HASH');
267 153 my $type = 0;
268 153 my($class, $tied);
269 153 ▶ if(defined $ref){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ref) 153 0 153
270 153 foreach my $i (keys %test){
271 445 ▶ if($ref eq $test{$i}){
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq $test{$i}) { } 22 423 445
elsif (defined $structure and $structure =~ /=$test{$i}/) { } 2 460 462
272 22 $type = $i;
273 22 last;
274 ▶ }elsif(defined $structure and $structure =~/=$test{$i}/){
Condition100%3/3
defined $structure and $structure =~ /=$test{$i}/
!ll&&!rl&&r
393822
MC/DC100%2/2
defined $structure and $structure =~ /=$test{$i}/
defined $structure $structure =~ /=$test{$i}/
Truth table100%3/3
A and B
A= defined $structureB= $structure =~ /=$test{$i}/
ABresult
0X0
100
111
275 2 ▶ $tied = tied($i == SCALARREF ? $$structure : $i == ARRAYREF ? @$structure : $structure);
Branch: 2 branches
BranchTrueFalseTotal
$i == 1 ? : 0 2 2
$i == 2 ? : 0 2 2
276 2 ▶ $type = $i | BLESSED | ($tied ? TIED : 0);
Branch: 1 branch
BranchTrueFalseTotal
$tied ? : 0 2 2
277 2 $class = $ref;
278 2 last;
279 }
280 }
281 }
282 153 ▶ unless($class or $tied){
Branch: 1 branch
BranchTrueFalseTotal
unless ($class or $tied) 151 2 153
Condition66%2/3
$class or $tied
l!l&&r!l&&!r
20151
MC/DC50%1/2
$class or $tied
$class $tied
Truth table66%2/3
A or B
A= $classB= $tied
ABresult
1X1
011
000
283 151 ▶ if(($type & 0b11001) == SCALARREF){
Branch: 3 branches
BranchTrueFalseTotal
if (($type & 25) == 1) { } 6 145 151
elsif (($type & 26) == 2) { } 5 140 145
elsif (($type & 28) == 4) { } 11 129 140
284 6 my $tmp = $$structure;
285 6 tie $$structure, "Tie::Trace::Scalar", parent => $self, %$parent_info;
286 6 $$structure = Tie::Trace::_data_filter($tmp, $self);
287 6 return $structure;
288 }elsif(($type & 0b11010) == ARRAYREF){
289 5 my @tmp = @$structure;
290 5 tie @$structure, "Tie::Trace::Array", parent => $self, %$parent_info;
291 5 foreach my $i (0 .. $#tmp){
292 23 $structure->[$i] = Tie::Trace::_data_filter($tmp[$i], $self, {__point => $i});
293 }
294 5 return $structure;
295 }elsif(($type & 0b11100) == HASHREF){
296 11 my %tmp = %$structure;
297 11 tie %$structure, "Tie::Trace::Hash", parent => $self, %$parent_info;;
298 11 ▶ while(my($k, $v) = each %tmp){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %tmp) 8 11 19
299 8 $structure->{$k} = Tie::Trace::_data_filter($v, $self, {__key => $k});
300 }
301 11 return $structure;
302 }
303 }
304 # tied variable / blessed ref / just a scalar
305 131 return $structure;
306 }
307  
308 # Hash /////////////////////////
309 package
310 Tie::Trace::Hash;
311  
312 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
313 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
314  
315 6 ▶use base qw/Tie::Trace/;
Subroutine BEGIN: called CC=1 SCAR=0.0
316  
317 52 ▶sub STORE{
Subroutine STORE: called CC=2 SCAR=6.9
318 52 my($self, $key, $value) = @_;
319 52 ▶ $self->_carpit(key => $key, value => $value) unless $QUIET;
Branch: 1 branch
BranchTrueFalseTotal
unless $Tie::Trace::QUIET 43 9 52
320 52 local $QUIET = 1;
321 52 Tie::Trace::_data_filter($value, $self, {__key => $key});
322 52 $self->{storage}->{$key} = $value;
323 };
324  
325 2 ▶sub DELETE {
Subroutine DELETE: called CC=3 SCAR=11.8
326 2 my($self, $key) = @_;
327 2 my $deleted = delete $self->{storage}->{$key};
328 $self->_carpit(key => $key,
329 value => sprintf("DELETED(%s)", $self->_dumper(defined $deleted ? $deleted : 'undef')),
330 0 ▶ filter => sub{$_[0] =~ s/^\'(.+)\'$/$1/; $_[0] =~s /\\'/'/g}
Subroutine __ANON__: not called CC=1 SCAR=6.9
331 2 ▶ ) unless $QUIET;
Branch: 2 branches
BranchTrueFalseTotal
unless $Tie::Trace::QUIET 2 0 2
defined $deleted ? : 1 1 2
332 2 return $deleted;
333 }
334  
335 0 ▶sub CLEAR{
Subroutine CLEAR: not called CC=1 SCAR=6.9
336 0 my($self) = @_;
337 0 return $self->Tie::Hash::CLEAR;
338 }
339  
340 # Array /////////////////////////
341 package
342 Tie::Trace::Array;
343  
344 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
345 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
346  
347 6 ▶use base qw/Tie::Trace/;
Subroutine BEGIN: called CC=1 SCAR=0.0
348  
349 32 ▶sub STORE{
Subroutine STORE: called CC=2 SCAR=6.9
350 32 my($self, $p, $value) = @_;
351 32 ▶ $self->_carpit(point => $p, value => $value) unless $QUIET;
Branch: 1 branch
BranchTrueFalseTotal
unless $Tie::Trace::QUIET 9 23 32
352 32 local $QUIET = 1;
353 32 Tie::Trace::_data_filter($value, $self, {__point => $p});
354 32 $self->{storage}->[$p] = $value;
355 }
356  
357 1 ▶sub DELETE{
Subroutine DELETE: called CC=3 SCAR=11.2
358 1 my($self, $p) = @_;
359 1 my $deleted = delete ${$self->{storage}}[$p];
360 $self->_carpit(point => $p,
361 value => sprintf("DELETED(%s)", $self->_dumper(defined $deleted ? $deleted : "undef")),
362 1 ▶ filter => sub{$_[0] =~ s/^\'(.*)\'$/$1/; $_[0] =~s /\\'/'/g}
Subroutine __ANON__: called CC=1 SCAR=0.0
363 1 ▶ ) unless $QUIET;
Branch: 2 branches
BranchTrueFalseTotal
unless $Tie::Trace::QUIET 1 0 1
defined $deleted ? : 0 1 1
364 1 return $deleted;
365 }
366  
367 9 ▶sub SPLICE{
Subroutine SPLICE: called CC=14 SCAR=26.4
368 9 my $self = shift;
369 9 my $sz = @{$self->{storage}};
370 9 ▶ my $off = @_ ? shift : 0;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 9 0 9
371 9 my $fetchsize = $self->FETCHSIZE;
372 9 my $caller_pkg = (caller)[0];
373 9 my $func = "";
374 9 ▶ if($caller_pkg eq "Tie::Trace::Array"){
Branch: 1 branch
BranchTrueFalseTotal
if ($caller_pkg eq "Tie::Trace::Array") 8 1 9
375 8 $func = (caller 1)[3];
376 8 $func =~s/^Tie::Trace::Array:://;
377 }
378 9 ▶ $off += $sz if $off < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $off < 0 0 9 9
379 9 ▶ my $len = @_ ? shift : $sz - $off;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 9 0 9
380 9 my $to = $off + $len -1;
381 9 ▶ my $p = $off eq $to ? $off : $off < $to ? "$off .. $to" : $off;
Branch: 2 branches
BranchTrueFalseTotal
$off eq $to ? : 1 8 9
$off < $to ? : 2 6 8
382 9 ▶ my @point = ($func and $func ne 'STORESIZE') ? () : (point => $p);
Branch: 1 branch
BranchTrueFalseTotal
$func && $func ne 'STORESIZE' ? : 4 5 9
Condition100%3/3
$func and $func ne "STORESIZE"
!ll&&!rl&&r
144
MC/DC100%2/2
$func and $func ne "STORESIZE"
$func $func ne "STORESIZE"
Truth table100%3/3
A and B
A= $funcB= $func ne "STORESIZE"
ABresult
0X0
100
111
383 9 ▶ $self->_carpit(@point, value => \@_, filter => sub {$_[0] =~ s/^\[(.*)\]$/$func\($1\)/} ) unless $QUIET;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
unless $Tie::Trace::QUIET 9 0 9
384 9 local $QUIET = 1;
385 9 ▶ if(@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 5 4 9
386 5 my $cnt = 0;
387 5 foreach(@_){
388 5 Tie::Trace::_data_filter($_, $self, {__point => $off + $cnt++});
389 }
390 }
391 9 my $ret = splice(@{$self->{storage}}, $off, $len, @_);
392 9 ▶ if(@_ != $len){
Branch: 1 branch
BranchTrueFalseTotal
if (@_ != $len) 6 3 9
393 6 my $diff = scalar @_ - $len;
394 6 local $QUIET = 1;
395 6 ▶ for(my $i = 0;$i < @{$self->{storage}}; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->{"storage"};}) 26 6 32
396 26 my $value = $self->{storage}->[$i];
397 26 Tie::Trace::_data_filter($value, $self, {__point => $i});
398 26 $self->{storage}->[$i] = $value;
399 }
400 }
401 9 return $ret;
402 }
403  
404 22 ▶sub FETCHSIZE{
Subroutine FETCHSIZE: called CC=2 SCAR=6.9
405 22 my($self) = shift;
406 22 ▶ return scalar @{$self->{storage} ||= []};
Condition100%2/2
$self->{"storage"} ||= []
l!l
211
MC/DC100%1/1
$self->{"storage"} ||= []
$self->{"storage"}
Truth table100%2/2
A
A= $self->{"storage"}
Aresult
11
00
407 }
408  
409 4 ▶sub PUSH{
Subroutine PUSH: called CC=1 SCAR=0.0
410 4 my($self, @value) = @_;
411 4 return $self->SPLICE($self->FETCHSIZE, 0, @value);
412 }
413  
414 0 ▶sub UNSHIFT{
Subroutine UNSHIFT: not called CC=1 SCAR=6.9
415 0 my($self, @value) = @_;
416 0 return $self->SPLICE(0, 0, @value);
417 }
418  
419 0 ▶sub POP{
Subroutine POP: not called CC=1 SCAR=6.9
420 0 my($self) = @_;
421 0 return $self->SPLICE(-1);
422 }
423  
424 0 ▶sub SHIFT{
Subroutine SHIFT: not called CC=1 SCAR=6.9
425 0 my($self) = @_;
426 0 return $self->SPLICE(0, 1);
427 }
428  
429 4 ▶sub STORESIZE {
Subroutine STORESIZE: called CC=1 SCAR=0.0
430 4 my ($self, $p) = @_;
431 4 $self->SPLICE($p, $self->FETCHSIZE - $p);
432 4 return undef;
433 }
434  
435 3 ▶sub CLEAR{
Subroutine CLEAR: called CC=2 SCAR=10.5
436 3 my($self) = @_;
437 3 return $self->Tie::Array::CLEAR();
438 0 $self->DELETE($_) for 0 .. $#{$self->{storage}};
439 0 return undef;
440 }
441  
442 # Scalar /////////////////////////
443 package
444 Tie::Trace::Scalar;
445  
446 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
447 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
448  
449 6 ▶use base qw/Tie::Trace/;
Subroutine BEGIN: called CC=1 SCAR=0.0
450  
451 8 ▶sub STORE{
Subroutine STORE: called CC=2 SCAR=6.9
452 8 my($self, $value) = @_;
453 8 ▶ $self->_carpit(value => $value) unless $QUIET;
Branch: 1 branch
BranchTrueFalseTotal
unless $Tie::Trace::QUIET 2 6 8
454 8 local $QUIET = 1;
455 8 Tie::Trace::_data_filter($value, $self);
456 8 ${$self->{storage}} = $value;
457 };
458  
459 =head1 NAME
460  
461 Tie::Trace - easy print debugging with tie, for watching variable
462  
463 =head1 VERSION
464  
465 Version 0.17
466  
467 =cut
468  
469 6 our $VERSION = '0.17';
470  
471 =head1 SYNOPSIS
472  
473 use Tie::Trace qw/watch/; # or qw/:all/
474
475 my %hash = (key => 'value');
476 watch %hash;
477
478 $hash{hoge} = 'hogehoge'; # warn "main:: %hash => {hoge} => hogehgoe at ..."
479
480 my @array;
481 tie @array;
482 push @array, "array"; # warn "main:: @array [0] => array at ..."
483
484 my $scalar;
485 watch $scalar;
486 $scalar = "scalar"; # warn "main:: $scalar => scalar at ..."
487  
488 =head1 DESCRIPTION
489  
490 This is useful for print debugging. Using tie mechanism,
491 you can see stored/deleted value for the specified variable.
492  
493 If the stored value is scalar/array/hash ref, this can check
494 recursively.
495  
496 for example;
497  
498 watch %hash;
499
500 $hash{foo} = {a => 1, b => 2}; # warn "main:: %hash => {foo} => {a => 1, b => 2}"
501 $hash{foo}->{a} = 2 # warn "main:: %hash => {foo}{a} => 2"
502  
503 But This ignores blessed reference and tied value.
504  
505 =head1 FUNCTION
506  
507 This provides one function C<watch> from version 0.06.
508 Then you should use only this function. Don't use C<tie> function instead.
509  
510 =over 4
511  
512 =item watch
513  
514 watch $variables;
515  
516 watch $scalar, %options;
517 watch @array, %options;
518 watch %hash, %options;
519  
520 When you C<watch> variables and value is stored/delete in the variables,
521 warn the message like as the following.
522  
523 main:: %hash => {key} => value at ...
524  
525 If the variables has values before C<watch>, it is no problem. Tie::Trace work well.
526  
527 my %hash = (key => 'value');
528 watch %hash;
529  
530 =back
531  
532 =head1 OPTIONS
533  
534 You can use C<watch> with some options.
535 If you want global options, see L<GLOBAL VARIABLES>.
536  
537 =over 4
538  
539 =item key => [values/regexs/coderef]
540  
541 watch %hash, key => [qw/foo bar/];
542  
543 It is for hash. You can specify key name/regex/coderef for checking.
544 Not specified/matched keys are ignored for warning.
545 When you give coderef, this coderef receive tied value and key as arguments,
546 it returns false, the key is ignored.
547  
548 for example;
549  
550 watch %hash, key => [qw/foo bar/, qr/x/];
551
552 $hash{foo} = 1 # warn ...
553 $hash{bar} = 1 # warn ...
554 $hash{var} = 1 # *no* warnings
555 $hash{_x_} = 1 # warn ...
556  
557 =item value => [contents/regexs/coderef]
558  
559 watch %hash, value => [qw/foo bar/];
560  
561 You can specify value's content/regex/coderef for checking.
562 Not specified/matched are ignored for warning.
563 When you give coderef, this coderef receive tied value and value as arguments,
564 it returns false, the value is ignored.
565  
566 for example;
567  
568 watch %hash, value => [qw/foo bar/, qr/\)/];
569
570 $hash{a} = 'foo' # warn ...
571 $hash{b} = 'foo1' # *no* warnings
572 $hash{c} = 'bar' # warn ...
573 $hash{d} = ':-)' # warn ...
574  
575 =item use => [qw/hash array scalar/]
576  
577 tie %hash, "Tie::Trace", use => [qw/array/];
578  
579 It specify type(scalar, array or hash) of variable for checking.
580 As default, all type will be checked.
581  
582 for example;
583  
584 watch %hash, use => [qw/array/];
585
586 $hash{foo} = 1 # *no* warnings
587 $hash{bar} = 1 # *no* warnings
588 $hash{var} = [] # *no* warnings
589 push @{$hash{var}} = 1 # warn ...
590  
591 =item debug => 'dumper'/coderef
592  
593 watch %hash, debug => 'dumper'
594 watch %hash, debug => sub{my($self, @v) = @_; return @v }
595  
596 It specify value representation. As default, "dumper" is set.
597 "dumper" makes value show with Data::Dumper::Dumper format(but ::Terse = 0 and ::Indent = 0).
598 You can use coderef instead of "dumper".
599 When you specify your coderef, its first argument is tied value and
600 second argument is value, it should modify it and return it.
601  
602 =item debug_value => [contents/regexs/coderef]
603  
604 watch %hash, debug => sub{my($s,$v) = @_; $v =~tr/op/po/;}, debug_value => [qw/foo boo/];
605  
606 You can specify debugged value's content/regex for checking.
607 Not specified/matched are ignored for warning.
608 When you give coderef, this coderef receive tied value and value as arguments,
609 it returns false, the value is ignored.
610  
611 for example;
612  
613 watch %hash, debug => sub{my($s,$v) = @_; $v =~tr/op/po/;}, debug_value => [qw/foo boo/];
614
615 $hash{a} = 'fpp' # warn ... because debugged value is foo
616 $hash{b} = 'foo' # *no* warnings because debugged value is fpp
617 $hash{c} = 'bpp' # warn ... because debugged value is boo
618  
619 =item r => 0/1
620  
621 tie %hash, "Tie::Trace", r => 0;
622  
623 If r is 0, this won't check recursively. 1 is default.
624  
625 =item caller => number/[numbers]
626  
627 watch %hash, caller => 2;
628  
629 It effects warning message.
630 default is 0. If you set grater than 0, it goes upstream to check.
631  
632 You can specify array ref.
633  
634 watch %hash, caller => [1, 2, 3];
635  
636 It display following messages.
637  
638 main %hash => {key} => 'hoge' at filename line 61.
639 at filename line 383.
640 at filename line 268.
641  
642 =back
643  
644 =head1 METHODS
645  
646 It is used in coderef which is passed for options, for example,
647 key, value and/or debug_value or as the method of the returned of tied function.
648  
649 =over 4
650  
651 =item storage
652  
653 watch %hash, debug =>
654 sub {
655 my($self, $v) = @_;
656 my $storage = $self->storage;
657 return $storage;
658 };
659  
660 This returns reference in which value(s) stored.
661  
662 =item parent
663  
664 watch %hash, debug =>
665 sub {
666 my($self, $v) = @_;
667 my $parent = $self->parent->storage;
668 return $parent;
669 };
670  
671 This method returns $self's parent tied value.
672  
673 for example;
674  
675 watch my %hash;
676 my %hash2;
677 $hash{1} = \%hash2;
678 my $tied_hash2 = tied %hash2;
679 print tied %hash eq $tied_hash2->parent; # 1
680  
681 =back
682  
683 =head1 GLOBAL VARIABLES
684  
685 =over 4
686  
687 =item %Tie::Trace::OPTIONS
688  
689 This is Global options for Tie::Trace.
690 If you don't specify any options, this option is used.
691 If you use override options, you use C<watch> with options.
692  
693 %Tie::Trace::OPTIONS = (debug => undef, ...);
694  
695 # global options will be used
696 watch my %hash;
697  
698 # your options will be used
699 watch my %hash2, debug => 'dumper', ...;
700  
701 =item $Tie::Trace::QUIET
702  
703 If this value is true, Tie::Trace warn nothing.
704  
705 watch my %hash;
706
707 $hash{1} = 1; # warn something
708
709 $Tie::Trace::QUIET = 1;
710
711 $hash{1} = 2; # no warn
712  
713 =back
714  
715 =head1 AUTHOR
716  
717 Ktat, C<< <ktat.is at gmail.com> >>
718  
719 =head1 BUGS
720  
721 Please report any bugs or feature requests to
722 C<bug-tie-debug at rt.cpan.org>, or through the web interface at
723 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Tie-Trace>.
724 I will be notified, and then you'll automatically be notified of progress on
725 your bug as I make changes.
726  
727 =head1 SUPPORT
728  
729 You can find documentation for this module with the perldoc command.
730  
731 perldoc Tie::Trace
732  
733 You can also find documentation written in Japanese(euc-jp) for this module
734 with the perldoc command.
735  
736 perldoc Tie::Trace_JP
737  
738 You can also look for information at:
739  
740 =over 4
741  
742 =item * AnnoCPAN: Annotated CPAN documentation
743  
744 L<http://annocpan.org/dist/Tie-Trace>
745  
746 =item * CPAN Ratings
747  
748 L<http://cpanratings.perl.org/d/Tie-Trace>
749  
750 =item * RT: CPAN's request tracker
751  
752 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Tie-Trace>
753  
754 =item * Search CPAN
755  
756 L<http://search.cpan.org/dist/Tie-Trace>
757  
758 =back
759  
760 =head1 ACKNOWLEDGEMENT
761  
762 JN told me the idea of new warning message(from 0.06).
763  
764 =head1 COPYRIGHT & LICENSE
765  
766 Copyright 2006-2010 Ktat, all rights reserved.
767  
768 This program is free software; you can redistribute it and/or modify it
769 under the same terms as Perl itself.
770  
771 =cut
772  
773 6 1; # End of Tie::Trace
↑ Summary