blib/lib/Devel/Carnivore.pm

filter: Statementstmt 90.0% 99 / 110 Branchbran 75.0% 21 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 86.6% 26 / 30 Podpod 0.0% 0 / 5 totaltotal 84.3% 146 / 173 CC 15 SCAR 9.3
CC
15
Cov
87%
STORE
6.1
watch
4.0
test_no_hashref
4.0
CRAP 2.5
↑ Summary
1 package Devel::Carnivore;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use 5.6.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Attribute::Handlers;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶no warnings "redefine";
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use vars qw/$OUT @EXPORT @ISA $NAME $VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use base "Exporter";
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 @EXPORT = qw(watch unwatch);
13  
14 1 $VERSION = 0.09;
15  
16 # By default print to STDERR
17 1 $OUT = \*STDERR;
18  
19 # test whether the first para is NOT a hashref
20 20 ▶sub test_no_hashref($) {
Subroutine test_no_hashref: called CC=4 SCAR=13.9
Pod undocumented
21 20 my($hashref) = @_;
22
23 20 ▶ return if ref $hashref eq "HASH"; # normal hash ref: good
Branch: 1 branch
BranchTrueFalseTotal
if ref $hashref eq "HASH" 9 11 20
24
25 11 ▶ if(ref $hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hashref) 9 2 11
26 9 local $@;
27 9 eval '%{ $hashref }';
28 9 ▶ return unless $@; # blessed hash: good
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 5 4 9
29 }
30
31 6 return 1;
32 }
33  
34 # tie $hashref to Devel::Carnivore::Tie::Hash
35 # optionally specify a name
36 # for internal use: a custom carp level may be specified. look at Carp.pm for documentation
37 9 ▶sub watch($;$) {
Subroutine watch: called CC=4 SCAR=14.0
Pod undocumented
38 9 my($hashref,$name) = @_;
39
40 # this module only works with hashrefs
41 9 ▶ croak "variable is not a hash reference" if test_no_hashref $hashref;
Branch: 1 branch
BranchTrueFalseTotal
if &test_no_hashref($hashref) 2 7 9
42
43 7 my %copy = %$hashref; # make a copy of the actual hash in hashref
44
45 7 my $calling_pkg = caller;
46
47 7 ▶ croak "can't watch a variable which is already tied" if tied %$hashref;
Branch: 1 branch
BranchTrueFalseTotal
if tied %$hashref 1 6 7
48
49 # print a comment that we start watching
50 6 print $Devel::Carnivore::OUT "# variable is now under observation\n";
51
52 6 tie %$hashref, 'Devel::Carnivore::Tie::Hash', $name;
53 # %$hashref is now empty
54
55 6 ▶ while(my($key,$value) = each %copy) { # but we restore the copy
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %copy) 0 6 6
56 0 $hashref->{$key} = $value
57 }
58 }
59  
60 # untie $hashref
61 4 ▶sub unwatch($) {
Subroutine unwatch: called CC=3 SCAR=11.8
Pod undocumented
62 4 my($hashref) = @_;
63
64 4 ▶ if(test_no_hashref $hashref) { # of course this only works if $hashref is actually a hash reference
Branch: 2 branches
BranchTrueFalseTotal
if (&test_no_hashref($hashref)) { } 0 4 4
elsif (tied(%$hashref)->isa('Devel::Carnivore::Tie::Hash')) { } 4 0 4
65 0 carp "variable is not a hash reference"
66 }
67 elsif((tied %$hashref)->isa("Devel::Carnivore::Tie::Hash")) {
68 1 ▶ no warnings; # silence "untie attempted while 1 inner references still exist" warning
Subroutine BEGIN: called CC=1 SCAR=0.0
69 # is there a better way to do this.
70 # as far as I can see this call is perfectly safe.
71 4 untie %$hashref;
72 4 print $Devel::Carnivore::OUT "# mission completed\n";
73 } else {
74 0 carp "Apparently this variable is not currently under observation."
75 }
76 }
77  
78 # install Watch as a universal attribute for hashes
79 # a name may be given as the single parameter to the attribute
80 # we then call our watch with the hashref and the name
81 1 ▶sub UNIVERSAL::Watch : ATTR(HASH) {
Subroutine Watch: called CC=1 SCAR=0.0
Pod undocumented
82 1 my ($package, $symbol, $hashref, $attr, $name, $phase) = @_;
83
84 1 watch $hashref, $name
85 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
86
87 # install Watch as a universal attribute for scalars
88 # this scalar is then tied to the special class Devel::Carnivore::Tie::Scalar
89 # ....why the f*ck? ... oh, yeah, so we can automatically tie any hashref to
90 # Devel::Carnivore::Tie::Hash as soon as it is assigned to this scalar :)
91 5 ▶sub UNIVERSAL::Watch : ATTR(SCALAR) {
Subroutine Watch: called CC=1 SCAR=0.0
Pod undocumented
92 5 my ($package, $symbol, $scalar_ref, $attr, $name, $phase) = @_;
93
94 5 tie $$scalar_ref, 'Devel::Carnivore::Tie::Scalar', $name
95 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
96  
97 # utility class used by the Watch scalar attribute
98 package Devel::Carnivore::Tie::Scalar;
99 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 1 ▶use Devel::Carnivore;
Subroutine BEGIN: called CC=1 SCAR=0.0
101  
102 # save a scalar and a name with the tied scalar
103 5 ▶sub TIESCALAR {
Subroutine TIESCALAR: called CC=1 SCAR=0.0
104 5 my($class,$name) = @_;
105
106 5 my $scalar = undef;
107
108 5 my $self = {
109 scalar => \$scalar,
110 name => $name,
111 };
112
113 5 bless $self, $class;
114 }
115  
116 # only hashrefs or object based on hashrefs may be assigned to scalars based on this class
117 # these hashrefs are then immediately tied to Devel::Carnivore::Tie::Hash
118 7 ▶sub STORE {
Subroutine STORE: called CC=2 SCAR=6.9
119 7 my($self,$value) = @_;
120
121 7 ▶ croak "You may only store hashrefs within a scalar under observation by Devel::Carnivore."
Branch: 1 branch
BranchTrueFalseTotal
if &Devel::Carnivore::test_no_hashref($value) 4 3 7
122 if Devel::Carnivore::test_no_hashref($value);
123
124 3 Devel::Carnivore::watch $value, $self->{name};
125
126 3 ${$self->{scalar}} = $value
127 }
128  
129 2 ▶sub FETCH { ${$_[0]->{scalar}} }
Subroutine FETCH: called CC=1 SCAR=0.0
130 5 ▶sub DESTROY { undef ${$_[0]->{scalar}} }
Subroutine DESTROY: called CC=1 SCAR=0.0
131  
132 package Devel::Carnivore::Tie::Hash;
133  
134 # this is where the actual output is generated
135 32 ▶sub STORE {
Subroutine STORE: called CC=6 SCAR=18.1
136 32 my($self,$key,$value) = @_;
137 32 my($package,$filename,$line) = caller;
138
139 32 my $hashref = $self->{hash};
140 32 my $name = $self->{name};
141
142 32 my $old_value = $hashref->{$key};
143  
144 32 ▶ $key = defined $key ? $key : ""; # some stuff to limit warnings
Branch: 1 branch
BranchTrueFalseTotal
defined $key ? : 32 0 32
145 32 ▶ $value = defined $value ? $value : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 32 0 32
146 32 ▶ $old_value = defined $old_value ? $old_value : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $old_value ? : 24 8 32
147
148 32 my $message = "> "; # this is what will eventually get printed out
149
150 32 ▶ $message .= qq{$name: } if defined $name; # we start out with a name if we have one
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 32 0 32
151
152 # "myHashKey" changed from "someValue" to "someOtherValue"
153 32 $message .= qq{"$key" changed from "$old_value" to "$value" };
154
155
156 # $Carp::CarpLevel may be set to influence the output.
157 # This sucks bad!!!. First, CarpLevel is deprecated but setting CarpInternal to our
158 # caller does not seem to work, second, setting CarpLevel to 1 seems to be the right
159 # thing but Perl 5.6.1 (and below???) doesnt like it.
160
161 32 local $Carp::CarpLevel;
162 32 ▶ if($] >= 5.008) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.008") 32 0 32
163 32 $Carp::CarpLevel = 1;
164 }
165
166 # we print this using a function available via the Carp module
167 # it automatically adds information about where this method was called
168
169 32 print $Devel::Carnivore::OUT Carp::shortmess($message);
170
171 # ahh, and finally we behave like a normal hash.
172 32 return $hashref->{$key} = $value;
173 }
174  
175 # make an object with a hash and a name
176 6 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=1 SCAR=0.0
177 6 my($class,$name) = @_;
178
179 6 bless {
180 hash => {},
181 name => $name,
182 }, $class
183 }
184  
185 # copied from Tie::Hash, adapted to my object scheme
186 48 ▶sub FETCH { $_[0]->{hash}{$_[1]} }
Subroutine FETCH: called CC=1 SCAR=0.0
187 0 ▶sub FIRSTKEY { my $a = scalar keys %{$_[0]->{hash}}; each %{$_[0]->{hash}} }
Subroutine FIRSTKEY: not called CC=1 SCAR=6.9
188 0 ▶sub NEXTKEY { each %{$_[0]->{hash}} }
Subroutine NEXTKEY: not called CC=1 SCAR=6.9
189 0 ▶sub EXISTS { exists $_[0]->{hash}{$_[1]} }
Subroutine EXISTS: not called CC=1 SCAR=6.9
190 0 ▶sub DELETE { delete $_[0]->{hash}{$_[1]} }
Subroutine DELETE: not called CC=1 SCAR=6.9
191 1 ▶sub CLEAR { %{$_[0]->{hash}} = () }
Subroutine CLEAR: called CC=1 SCAR=0.0
192  
193 1 q<big brother is watching you>
194  
195 __END__
↑ Summary