1 |
root |
1.1 |
package Devel::FindRef; |
2 |
|
|
|
3 |
|
|
use strict; |
4 |
|
|
|
5 |
|
|
use XSLoader; |
6 |
root |
1.9 |
use Scalar::Util; |
7 |
root |
1.1 |
|
8 |
|
|
BEGIN { |
9 |
root |
1.13 |
our $VERSION = '1.3'; |
10 |
root |
1.1 |
XSLoader::load __PACKAGE__, $VERSION; |
11 |
|
|
} |
12 |
|
|
|
13 |
|
|
=head1 NAME |
14 |
|
|
|
15 |
root |
1.13 |
Devel::FindRef - where is that reference to my variable hiding? |
16 |
root |
1.1 |
|
17 |
|
|
=head1 SYNOPSIS |
18 |
|
|
|
19 |
|
|
use Devel::FindRef; |
20 |
|
|
|
21 |
|
|
=head1 DESCRIPTION |
22 |
|
|
|
23 |
|
|
Tracking down reference problems (e.g. you expect some object to be |
24 |
root |
1.4 |
destroyed, but there are still references to it that keep it alive) can be |
25 |
|
|
very hard. Fortunately, perl keeps track of all its values, so tracking |
26 |
|
|
references "backwards" is usually possible. |
27 |
root |
1.1 |
|
28 |
root |
1.4 |
The C<track> function can help track down some of those references back to |
29 |
root |
1.1 |
the variables containing them. |
30 |
|
|
|
31 |
|
|
For example, for this fragment: |
32 |
|
|
|
33 |
|
|
package Test; |
34 |
|
|
|
35 |
|
|
our $var = "hi\n"; |
36 |
|
|
my $x = \$var; |
37 |
|
|
our %hash = (ukukey => \$var); |
38 |
|
|
our $hash2 = {ukukey2 => \$var}; |
39 |
|
|
|
40 |
|
|
sub testsub { |
41 |
|
|
my $local = $hash2; |
42 |
|
|
print Devel::FindRef::track \$var; |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
testsub; |
46 |
|
|
|
47 |
root |
1.4 |
The output is as follows (or similar to this, in case I forget to update |
48 |
root |
1.3 |
the manpage after some changes): |
49 |
root |
1.1 |
|
50 |
root |
1.13 |
SCALAR(0x814ece8) is |
51 |
|
|
+- in the global $Test::var. |
52 |
|
|
+- referenced by REF(0x814f9e4), which is |
53 |
|
|
| in the lexical '$x' in CODE(0x814ed78), which is |
54 |
|
|
| the containing scope for CODE(0x820c4b0), which is |
55 |
|
|
| in the global &Test::testsub. |
56 |
|
|
+- referenced by REF(0x814ed6c), which is |
57 |
|
|
| in the member 'ukukey' of HASH(0x81da20c), which is |
58 |
|
|
| in the global %Test::hash. |
59 |
|
|
+- referenced by REF(0x814ec28), which is |
60 |
|
|
| not found anywhere I looked :( |
61 |
|
|
+- referenced by REF(0x814eb44), which is |
62 |
|
|
in the member 'ukukey2' of HASH(0x814f99c), which is |
63 |
|
|
+- referenced by REF(0x820c450), which is |
64 |
|
|
| in the lexical '$local' in CODE(0x820c4b0), which was seen before. |
65 |
|
|
+- referenced by REF(0x820c204), which is |
66 |
root |
1.1 |
in the global $Test::hash2. |
67 |
|
|
|
68 |
root |
1.4 |
It is a bit convoluted to read, but basically it says that the value |
69 |
|
|
stored in C<$var> can be found: |
70 |
root |
1.1 |
|
71 |
|
|
=over 4 |
72 |
|
|
|
73 |
|
|
=item - in some variable C<$x> whose origin is not known (I frankly have no |
74 |
|
|
idea why, hints accepted). |
75 |
|
|
|
76 |
|
|
=item - in the hash element with key C<ukukey> in the hash stored in C<%Test::hash>. |
77 |
|
|
|
78 |
|
|
=item - in the global variable named C<$Test::var>. |
79 |
|
|
|
80 |
|
|
=item - in the hash element C<ukukey2>, in the hash in the my variable |
81 |
|
|
C<$local> in the sub C<Test::testsub> and also in the hash referenced by |
82 |
|
|
C<$Test::hash2>. |
83 |
|
|
|
84 |
root |
1.6 |
=back |
85 |
|
|
|
86 |
root |
1.1 |
=head1 EXPORTS |
87 |
|
|
|
88 |
|
|
None. |
89 |
|
|
|
90 |
|
|
=head1 FUNCTIONS |
91 |
|
|
|
92 |
|
|
=over 4 |
93 |
|
|
|
94 |
|
|
=item $string = Devel::FindRef::track $ref[, $depth] |
95 |
|
|
|
96 |
|
|
Track the perl value pointed to by C<$ref> up to a depth of C<$depth> and |
97 |
|
|
return a descriptive string. C<$ref> can point at any perl value, be it |
98 |
|
|
anonymous sub, hash, array, scalar etc. |
99 |
|
|
|
100 |
|
|
This is the function you most often use. |
101 |
|
|
|
102 |
|
|
=cut |
103 |
|
|
|
104 |
|
|
sub find($); |
105 |
|
|
|
106 |
|
|
sub track { |
107 |
root |
1.9 |
my ($ref, $depth) = @_; |
108 |
|
|
@_ = (); |
109 |
|
|
|
110 |
root |
1.1 |
my $buf = ""; |
111 |
root |
1.11 |
my %seen; |
112 |
root |
1.9 |
|
113 |
|
|
Scalar::Util::weaken $ref; |
114 |
root |
1.1 |
|
115 |
|
|
my $track; $track = sub { |
116 |
root |
1.9 |
my ($refref, $depth, $indent) = @_; |
117 |
root |
1.1 |
|
118 |
|
|
if ($depth) { |
119 |
root |
1.9 |
my (@about) = find $$refref; |
120 |
root |
1.1 |
if (@about) { |
121 |
|
|
for my $about (@about) { |
122 |
root |
1.12 |
$buf .= "$indent" . (@about > 1 ? "+- " : " ") . $about->[0]; |
123 |
root |
1.1 |
if (@$about > 1) { |
124 |
root |
1.11 |
if ($seen{$about->[1]+0}++) { |
125 |
|
|
$buf .= " $about->[1], which was seen before.\n"; |
126 |
|
|
} else { |
127 |
|
|
$buf .= " $about->[1], which is\n"; |
128 |
root |
1.12 |
$track->(\$about->[1], $depth - 1, $about == $about[-1] ? "$indent " : "$indent| "); |
129 |
root |
1.11 |
} |
130 |
root |
1.1 |
} else { |
131 |
|
|
$buf .= ".\n"; |
132 |
|
|
} |
133 |
|
|
} |
134 |
|
|
} else { |
135 |
root |
1.12 |
$buf .= "$indent not found anywhere I looked :(\n"; |
136 |
root |
1.1 |
} |
137 |
|
|
} else { |
138 |
root |
1.12 |
$buf .= "$indent not referenced within the search depth.\n"; |
139 |
root |
1.1 |
} |
140 |
|
|
}; |
141 |
|
|
|
142 |
root |
1.9 |
$buf .= "$ref is\n"; |
143 |
root |
1.12 |
$track->(\$ref, $depth || $ENV{PERL_DEVEL_FINDREF_DEPTH} || 10, ""); |
144 |
root |
1.1 |
$buf |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
=item @references = Devel::FindRef::find $ref |
148 |
|
|
|
149 |
|
|
Return arrayrefs that contain [$message, $ref] pairs. The message |
150 |
|
|
describes what kind of reference was found and the C<$ref> is the |
151 |
root |
1.9 |
reference itself, which can be omitted if C<find> decided to end the |
152 |
|
|
search. The returned references are all weak references. |
153 |
root |
1.1 |
|
154 |
|
|
The C<track> function uses this to find references to the value you are |
155 |
|
|
interested in and recurses on the returned references. |
156 |
|
|
|
157 |
|
|
=cut |
158 |
|
|
|
159 |
|
|
sub find($) { |
160 |
|
|
my ($about, $excl) = &find_; |
161 |
root |
1.6 |
my %excl = map +($_ => undef), @$excl; |
162 |
|
|
grep !exists $excl{$_->[1] + 0}, @$about |
163 |
root |
1.1 |
} |
164 |
|
|
|
165 |
root |
1.7 |
=item $ref = Devel::FindRef::ptr2ref $integer |
166 |
root |
1.1 |
|
167 |
|
|
Sometimes you know (from debugging output) the address of a perl scalar |
168 |
root |
1.7 |
you are interested in (e.g. C<HASH(0x176ff70)>). This function can be used |
169 |
|
|
to turn the address into a reference to that scalar. It is quite safe to |
170 |
|
|
call on valid addresses, but extremely dangerous to call on invalid ones. |
171 |
|
|
|
172 |
|
|
# we know that HASH(0x176ff70) exists, so turn it into a hashref: |
173 |
|
|
my $ref_to_hash = Devel::FindRef::ptr2ref 0x176ff70; |
174 |
root |
1.1 |
|
175 |
|
|
=back |
176 |
|
|
|
177 |
root |
1.12 |
=head1 ENVIRONMENT VARIABLES |
178 |
|
|
|
179 |
|
|
You can set the environment variable C<PERL_DEVEL_FINDREF_DEPTH> to an |
180 |
|
|
integer to override the default depth in C<track>. If a call explicitly |
181 |
|
|
specified a depth it is not overridden. |
182 |
|
|
|
183 |
root |
1.1 |
=head1 AUTHOR |
184 |
|
|
|
185 |
|
|
Marc Lehmann <pcg@goof.com>. |
186 |
|
|
|
187 |
|
|
=head1 BUGS |
188 |
|
|
|
189 |
root |
1.2 |
Only code values, arrays, hashes, scalars and magic are being looked at. |
190 |
root |
1.1 |
|
191 |
root |
1.4 |
This is a quick hack only. |
192 |
|
|
|
193 |
root |
1.1 |
=head1 COPYRIGHT AND LICENSE |
194 |
|
|
|
195 |
|
|
Copyright (C) 2007 by Marc Lehmann. |
196 |
|
|
|
197 |
|
|
This library is free software; you can redistribute it and/or modify |
198 |
|
|
it under the same terms as Perl itself, either Perl version 5.8.8 or, |
199 |
|
|
at your option, any later version of Perl 5 you may have available. |
200 |
|
|
|
201 |
|
|
=cut |
202 |
|
|
|
203 |
|
|
1 |
204 |
|
|
|