… | |
… | |
4 | |
4 | |
5 | use XSLoader; |
5 | use XSLoader; |
6 | |
6 | |
7 | |
7 | |
8 | BEGIN { |
8 | BEGIN { |
9 | our $VERSION = '0.1'; |
9 | our $VERSION = '0.2'; |
10 | XSLoader::load __PACKAGE__, $VERSION; |
10 | XSLoader::load __PACKAGE__, $VERSION; |
11 | } |
11 | } |
12 | |
12 | |
13 | =head1 NAME |
13 | =head1 NAME |
14 | |
14 | |
… | |
… | |
19 | use Devel::FindRef; |
19 | use Devel::FindRef; |
20 | |
20 | |
21 | =head1 DESCRIPTION |
21 | =head1 DESCRIPTION |
22 | |
22 | |
23 | Tracking down reference problems (e.g. you expect some object to be |
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 |
24 | destroyed, but there are still references to it that keep it alive) can be |
25 | be very hard, although perl keeps track of all values. |
25 | very hard. Fortunately, perl keeps track of all its values, so tracking |
|
|
26 | references "backwards" is usually possible. |
26 | |
27 | |
27 | The C<track> function can hlep track down some of those refernces back to |
28 | The C<track> function can help track down some of those references back to |
28 | the variables containing them. |
29 | the variables containing them. |
29 | |
30 | |
30 | For example, for this fragment: |
31 | For example, for this fragment: |
31 | |
32 | |
32 | package Test; |
33 | package Test; |
… | |
… | |
41 | print Devel::FindRef::track \$var; |
42 | print Devel::FindRef::track \$var; |
42 | } |
43 | } |
43 | |
44 | |
44 | testsub; |
45 | testsub; |
45 | |
46 | |
46 | The output is as follows (or similar to htis, in case I forget to update |
47 | The output is as follows (or similar to this, in case I forget to update |
47 | the manpage after some changes): |
48 | the manpage after some changes): |
48 | |
49 | |
49 | SCALAR(0x676fa0) is |
50 | SCALAR(0x676fa0) is |
50 | referenced by REF(0x676fb0), which is |
51 | referenced by REF(0x676fb0), which is |
51 | in the lexical '$x' in CODE(0x676370), which is |
52 | in the lexical '$x' in CODE(0x676370), which is |
… | |
… | |
61 | in the global &Test::testsub. |
62 | in the global &Test::testsub. |
62 | referenced by REF(0x77bc80), which is |
63 | referenced by REF(0x77bc80), which is |
63 | in the global $Test::hash2. |
64 | in the global $Test::hash2. |
64 | |
65 | |
65 | |
66 | |
66 | It is a bit convoluted to read, but basically it says that the value stored in C<$var> |
67 | It is a bit convoluted to read, but basically it says that the value |
67 | can be found: |
68 | stored in C<$var> can be found: |
68 | |
69 | |
69 | =over 4 |
70 | =over 4 |
70 | |
71 | |
71 | =item - in some variable C<$x> whose origin is not known (I frankly have no |
72 | =item - in some variable C<$x> whose origin is not known (I frankly have no |
72 | idea why, hints accepted). |
73 | idea why, hints accepted). |
… | |
… | |
76 | =item - in the global variable named C<$Test::var>. |
77 | =item - in the global variable named C<$Test::var>. |
77 | |
78 | |
78 | =item - in the hash element C<ukukey2>, in the hash in the my variable |
79 | =item - in the hash element C<ukukey2>, in the hash in the my variable |
79 | C<$local> in the sub C<Test::testsub> and also in the hash referenced by |
80 | C<$local> in the sub C<Test::testsub> and also in the hash referenced by |
80 | C<$Test::hash2>. |
81 | C<$Test::hash2>. |
|
|
82 | |
|
|
83 | =back |
81 | |
84 | |
82 | =head1 EXPORTS |
85 | =head1 EXPORTS |
83 | |
86 | |
84 | None. |
87 | None. |
85 | |
88 | |
… | |
… | |
99 | |
102 | |
100 | sub find($); |
103 | sub find($); |
101 | |
104 | |
102 | sub track { |
105 | sub track { |
103 | my $buf = ""; |
106 | my $buf = ""; |
|
|
107 | my %ignore; |
104 | |
108 | |
105 | my $track; $track = sub { |
109 | my $track; $track = sub { |
106 | my (undef, $depth, $indent) = @_; |
110 | my ($target, $depth, $indent) = @_; |
|
|
111 | @_ = (); |
|
|
112 | local $ignore{$target+0} = undef; |
107 | |
113 | |
108 | if ($depth) { |
114 | if ($depth) { |
109 | my (@about) = find $_[0]; |
115 | my (@about) = grep !exists $ignore{$_->[1]}, find $target; |
110 | if (@about) { |
116 | if (@about) { |
|
|
117 | local @ignore{map $_->[1]+0, @about} = (); |
111 | for my $about (@about) { |
118 | for my $about (@about) { |
|
|
119 | local $ignore{$about+0} = undef; |
112 | $buf .= (" ") x $indent; |
120 | $buf .= (" ") x $indent; |
113 | $buf .= $about->[0]; |
121 | $buf .= $about->[0]; |
114 | if (@$about > 1) { |
122 | if (@$about > 1) { |
115 | $buf .= " $about->[1], which is\n"; |
123 | $buf .= " $about->[1], which is\n"; |
116 | $track->($about->[1], $depth - 1, $indent + 1); |
124 | $track->($about->[1], $depth - 1, $indent + 1); |
… | |
… | |
145 | |
153 | |
146 | =cut |
154 | =cut |
147 | |
155 | |
148 | sub find($) { |
156 | sub find($) { |
149 | my ($about, $excl) = &find_; |
157 | my ($about, $excl) = &find_; |
150 | my %excl = map +($_ => 1), @$excl; |
158 | my %excl = map +($_ => undef), @$excl; |
151 | grep !$excl{$_->[1] + 0}, @$about |
159 | grep !exists $excl{$_->[1] + 0}, @$about |
152 | } |
160 | } |
153 | |
161 | |
154 | =item $ref = Devel::FindRef::ref2ptr $ptr |
162 | =item $ref = Devel::FindRef::ref2ptr $ptr |
155 | |
163 | |
156 | Sometimes you know (from debugging output) the address of a perl scalar |
164 | Sometimes you know (from debugging output) the address of a perl scalar |
… | |
… | |
166 | |
174 | |
167 | =head1 BUGS |
175 | =head1 BUGS |
168 | |
176 | |
169 | Only code values, arrays, hashes, scalars and magic are being looked at. |
177 | Only code values, arrays, hashes, scalars and magic are being looked at. |
170 | |
178 | |
|
|
179 | This is a quick hack only. |
|
|
180 | |
171 | =head1 COPYRIGHT AND LICENSE |
181 | =head1 COPYRIGHT AND LICENSE |
172 | |
182 | |
173 | Copyright (C) 2007 by Marc Lehmann. |
183 | Copyright (C) 2007 by Marc Lehmann. |
174 | |
184 | |
175 | This library is free software; you can redistribute it and/or modify |
185 | This library is free software; you can redistribute it and/or modify |