1 | package Devel::FindRef; |
1 | package Devel::FindRef; |
2 | |
2 | |
|
|
3 | no warnings; # I hate warning nazis |
3 | use strict; |
4 | use strict; |
4 | |
5 | |
5 | use XSLoader; |
6 | use XSLoader; |
6 | use Scalar::Util; |
7 | use Scalar::Util; |
7 | |
8 | |
8 | BEGIN { |
9 | BEGIN { |
9 | our $VERSION = '1.3'; |
10 | our $VERSION = '1.31'; |
10 | XSLoader::load __PACKAGE__, $VERSION; |
11 | XSLoader::load __PACKAGE__, $VERSION; |
11 | } |
12 | } |
12 | |
13 | |
13 | =head1 NAME |
14 | =head1 NAME |
14 | |
15 | |
15 | Devel::FindRef - where is that reference to my variable hiding? |
16 | Devel::FindRef - where is that reference to my variable hiding? |
16 | |
17 | |
17 | =head1 SYNOPSIS |
18 | =head1 SYNOPSIS |
18 | |
19 | |
19 | use Devel::FindRef; |
20 | use Devel::FindRef; |
|
|
21 | |
|
|
22 | print Devel::FindRef::track \$some_variable; |
20 | |
23 | |
21 | =head1 DESCRIPTION |
24 | =head1 DESCRIPTION |
22 | |
25 | |
23 | Tracking down reference problems (e.g. you expect some object to be |
26 | 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 |
27 | destroyed, but there are still references to it that keep it alive) can be |
… | |
… | |
101 | |
104 | |
102 | =cut |
105 | =cut |
103 | |
106 | |
104 | sub find($); |
107 | sub find($); |
105 | |
108 | |
|
|
109 | sub _f($) { |
|
|
110 | "$_[0] [refcount " . (_refcnt $_[0]) . "]" |
|
|
111 | } |
|
|
112 | |
106 | sub track { |
113 | sub track { |
107 | my ($ref, $depth) = @_; |
114 | my ($ref, $depth) = @_; |
108 | @_ = (); |
115 | @_ = (); |
109 | |
116 | |
110 | my $buf = ""; |
117 | my $buf = ""; |
… | |
… | |
120 | if (@about) { |
127 | if (@about) { |
121 | for my $about (@about) { |
128 | for my $about (@about) { |
122 | $buf .= "$indent" . (@about > 1 ? "+- " : " ") . $about->[0]; |
129 | $buf .= "$indent" . (@about > 1 ? "+- " : " ") . $about->[0]; |
123 | if (@$about > 1) { |
130 | if (@$about > 1) { |
124 | if ($seen{ref2ptr $about->[1]}++) { |
131 | if ($seen{ref2ptr $about->[1]}++) { |
125 | $buf .= " $about->[1], which was seen before.\n"; |
132 | $buf .= " " . (_f $about->[1]) . ", which was seen before.\n"; |
126 | } else { |
133 | } else { |
127 | $buf .= " $about->[1], which is\n"; |
134 | $buf .= " " . (_f $about->[1]) . ", which is\n"; |
128 | $track->(\$about->[1], $depth - 1, $about == $about[-1] ? "$indent " : "$indent| "); |
135 | $track->(\$about->[1], $depth - 1, $about == $about[-1] ? "$indent " : "$indent| "); |
129 | } |
136 | } |
130 | } else { |
137 | } else { |
131 | $buf .= ".\n"; |
138 | $buf .= ".\n"; |
132 | } |
139 | } |
… | |
… | |
137 | } else { |
144 | } else { |
138 | $buf .= "$indent not referenced within the search depth.\n"; |
145 | $buf .= "$indent not referenced within the search depth.\n"; |
139 | } |
146 | } |
140 | }; |
147 | }; |
141 | |
148 | |
142 | $buf .= "$ref is\n"; |
149 | $buf .= (_f $ref) . " is\n"; |
143 | $track->(\$ref, $depth || $ENV{PERL_DEVEL_FINDREF_DEPTH} || 10, ""); |
150 | $track->(\$ref, $depth || $ENV{PERL_DEVEL_FINDREF_DEPTH} || 10, ""); |
144 | $buf |
151 | $buf |
145 | } |
152 | } |
146 | |
153 | |
147 | =item @references = Devel::FindRef::find $ref |
154 | =item @references = Devel::FindRef::find $ref |
… | |
… | |
188 | |
195 | |
189 | =head1 AUTHOR |
196 | =head1 AUTHOR |
190 | |
197 | |
191 | Marc Lehmann <pcg@goof.com>. |
198 | Marc Lehmann <pcg@goof.com>. |
192 | |
199 | |
193 | =head1 BUGS |
|
|
194 | |
|
|
195 | Only code values, arrays, hashes, scalars and magic are being looked at. |
|
|
196 | |
|
|
197 | This is a quick hack only. |
|
|
198 | |
|
|
199 | =head1 COPYRIGHT AND LICENSE |
200 | =head1 COPYRIGHT AND LICENSE |
200 | |
201 | |
201 | Copyright (C) 2007 by Marc Lehmann. |
202 | Copyright (C) 2007, 2008 by Marc Lehmann. |
202 | |
203 | |
203 | This library is free software; you can redistribute it and/or modify |
204 | This library is free software; you can redistribute it and/or modify |
204 | it under the same terms as Perl itself, either Perl version 5.8.8 or, |
205 | it under the same terms as Perl itself, either Perl version 5.8.8 or, |
205 | at your option, any later version of Perl 5 you may have available. |
206 | at your option, any later version of Perl 5 you may have available. |
206 | |
207 | |