ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Devel-FindRef/FindRef.pm
Revision: 1.5
Committed: Wed Feb 7 21:34:02 2007 UTC (17 years, 3 months ago) by root
Branch: MAIN
CVS Tags: rel-0_2
Changes since 1.4: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 package Devel::FindRef;
2
3 use strict;
4
5 use XSLoader;
6
7
8 BEGIN {
9 our $VERSION = '0.2';
10 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 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
28 The C<track> function can help track down some of those references back to
29 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 The output is as follows (or similar to this, in case I forget to update
48 the manpage after some changes):
49
50 SCALAR(0x676fa0) is
51 referenced by REF(0x676fb0), which is
52 in the lexical '$x' in CODE(0x676370), which is
53 not found anywhere I looked :(
54 referenced by REF(0x676360), which is
55 in the member 'ukukey' of HASH(0x756660), which is
56 in the global %Test::hash.
57 in the global $Test::var.
58 referenced by REF(0x6760e0), which is
59 in the member 'ukukey2' of HASH(0x676f30), which is
60 referenced by REF(0x77bcf0), which is
61 in the lexical '$local' in CODE(0x77bcb0), which is
62 in the global &Test::testsub.
63 referenced by REF(0x77bc80), which is
64 in the global $Test::hash2.
65
66
67 It is a bit convoluted to read, but basically it says that the value
68 stored in C<$var> can be found:
69
70 =over 4
71
72 =item - in some variable C<$x> whose origin is not known (I frankly have no
73 idea why, hints accepted).
74
75 =item - in the hash element with key C<ukukey> in the hash stored in C<%Test::hash>.
76
77 =item - in the global variable named C<$Test::var>.
78
79 =item - in the hash element C<ukukey2>, in the hash in the my variable
80 C<$local> in the sub C<Test::testsub> and also in the hash referenced by
81 C<$Test::hash2>.
82
83 =head1 EXPORTS
84
85 None.
86
87 =head1 FUNCTIONS
88
89 =over 4
90
91 =item $string = Devel::FindRef::track $ref[, $depth]
92
93 Track the perl value pointed to by C<$ref> up to a depth of C<$depth> and
94 return a descriptive string. C<$ref> can point at any perl value, be it
95 anonymous sub, hash, array, scalar etc.
96
97 This is the function you most often use.
98
99 =cut
100
101 sub find($);
102
103 sub track {
104 my $buf = "";
105
106 my $track; $track = sub {
107 my (undef, $depth, $indent) = @_;
108
109 if ($depth) {
110 my (@about) = find $_[0];
111 if (@about) {
112 for my $about (@about) {
113 $buf .= (" ") x $indent;
114 $buf .= $about->[0];
115 if (@$about > 1) {
116 $buf .= " $about->[1], which is\n";
117 $track->($about->[1], $depth - 1, $indent + 1);
118 } else {
119 $buf .= ".\n";
120 }
121 }
122 } else {
123 $buf .= (" ") x $indent;
124 $buf .= "not found anywhere I looked :(\n";
125 }
126 } else {
127 $buf .= (" ") x $indent;
128 $buf .= "not referenced within the search depth.\n";
129 }
130 };
131
132 $buf .= "$_[0] is\n";
133 $track->($_[0], $_[1] || 10, 1);
134 $buf
135 }
136
137 =item @references = Devel::FindRef::find $ref
138
139 Return arrayrefs that contain [$message, $ref] pairs. The message
140 describes what kind of reference was found and the C<$ref> is the
141 reference itself, which cna be omitted if C<find> decided to end the
142 search.
143
144 The C<track> function uses this to find references to the value you are
145 interested in and recurses on the returned references.
146
147 =cut
148
149 sub find($) {
150 my ($about, $excl) = &find_;
151 my %excl = map +($_ => 1), @$excl;
152 grep !$excl{$_->[1] + 0}, @$about
153 }
154
155 =item $ref = Devel::FindRef::ref2ptr $ptr
156
157 Sometimes you know (from debugging output) the address of a perl scalar
158 you are interested in. This function can be used to turn the address into
159 a reference to that scalar. It is quite safe to call on valid addresses,
160 but extremely dangerous to call on invalid ones.
161
162 =back
163
164 =head1 AUTHOR
165
166 Marc Lehmann <pcg@goof.com>.
167
168 =head1 BUGS
169
170 Only code values, arrays, hashes, scalars and magic are being looked at.
171
172 This is a quick hack only.
173
174 =head1 COPYRIGHT AND LICENSE
175
176 Copyright (C) 2007 by Marc Lehmann.
177
178 This library is free software; you can redistribute it and/or modify
179 it under the same terms as Perl itself, either Perl version 5.8.8 or,
180 at your option, any later version of Perl 5 you may have available.
181
182 =cut
183
184 1
185