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