1 | package Devel::FindRef; |
1 | package Devel::FindRef; |
2 | |
2 | |
3 | no warnings; # I hate warning nazis |
3 | use common::sense; |
4 | use strict; |
|
|
5 | |
4 | |
6 | use XSLoader; |
5 | use XSLoader; |
7 | use Scalar::Util; |
6 | use Scalar::Util; |
8 | |
7 | |
9 | BEGIN { |
8 | BEGIN { |
10 | our $VERSION = '1.31'; |
9 | our $VERSION = 1.46; |
11 | XSLoader::load __PACKAGE__, $VERSION; |
10 | XSLoader::load __PACKAGE__, $VERSION; |
12 | } |
11 | } |
13 | |
12 | |
14 | =head1 NAME |
13 | =head1 NAME |
15 | |
14 | |
… | |
… | |
31 | The C<track> function can help track down some of those references back to |
30 | The C<track> function can help track down some of those references back to |
32 | the variables containing them. |
31 | the variables containing them. |
33 | |
32 | |
34 | For example, for this fragment: |
33 | For example, for this fragment: |
35 | |
34 | |
36 | package Test; |
35 | package Test; |
|
|
36 | |
|
|
37 | use Devel::FindRef; |
|
|
38 | use Scalar::Util; |
37 | |
39 | |
38 | our $var = "hi\n"; |
40 | our $var = "hi\n"; |
39 | my $x = \$var; |
41 | my $global_my = \$var; |
40 | our %hash = (ukukey => \$var); |
42 | our %global_hash = (ukukey => \$var); |
41 | our $hash2 = {ukukey2 => \$var}; |
43 | our $global_hashref = { ukukey2 => \$var }; |
42 | |
44 | |
43 | sub testsub { |
45 | sub testsub { |
44 | my $local = $hash2; |
46 | my $testsub_local = $global_hashref; |
45 | print Devel::FindRef::track \$var; |
47 | print Devel::FindRef::track \$var; |
46 | } |
48 | } |
47 | |
49 | |
48 | testsub; |
50 | my $closure = sub { |
|
|
51 | my $closure_var = \$_[0]; |
|
|
52 | Scalar::Util::weaken (my $weak_ref = \$var); |
|
|
53 | testsub; |
|
|
54 | }; |
|
|
55 | |
|
|
56 | $closure->($var); |
49 | |
57 | |
50 | The output is as follows (or similar to this, in case I forget to update |
58 | The output is as follows (or similar to this, in case I forget to update |
51 | the manpage after some changes): |
59 | the manpage after some changes): |
52 | |
60 | |
53 | SCALAR(0x814ece8) is |
61 | SCALAR(0x7cc888) [refcount 6] is |
|
|
62 | +- referenced by REF(0x8abcc8) [refcount 1], which is |
|
|
63 | | the lexical '$closure_var' in CODE(0x8abc50) [refcount 4], which is |
|
|
64 | | +- the closure created at tst:18. |
|
|
65 | | +- referenced by REF(0x7d3c58) [refcount 1], which is |
|
|
66 | | | the lexical '$closure' in CODE(0x7ae530) [refcount 2], which is |
|
|
67 | | | +- the containing scope for CODE(0x8ab430) [refcount 3], which is |
|
|
68 | | | | the global &Test::testsub. |
|
|
69 | | | +- the main body of the program. |
|
|
70 | | +- the lexical '&' in CODE(0x7ae530) [refcount 2], which was seen before. |
|
|
71 | +- referenced by REF(0x7cc7c8) [refcount 1], which is |
|
|
72 | | the lexical '$global_my' in CODE(0x7ae530) [refcount 2], which was seen before. |
54 | +- in the global $Test::var. |
73 | +- the global $Test::var. |
55 | +- referenced by REF(0x814f9e4), which is |
74 | +- referenced by REF(0x7cc558) [refcount 1], which is |
56 | | in the lexical '$x' in CODE(0x814ed78), which is |
75 | | the member 'ukukey2' of HASH(0x7ae140) [refcount 2], which is |
57 | | the containing scope for CODE(0x820c4b0), which is |
76 | | +- referenced by REF(0x8abad0) [refcount 1], which is |
58 | | in the global &Test::testsub. |
77 | | | the lexical '$testsub_local' in CODE(0x8ab430) [refcount 3], which was seen before. |
|
|
78 | | +- referenced by REF(0x8ab4f0) [refcount 1], which is |
|
|
79 | | the global $Test::global_hashref. |
59 | +- referenced by REF(0x814ed6c), which is |
80 | +- referenced by REF(0x7ae518) [refcount 1], which is |
60 | | in the member 'ukukey' of HASH(0x81da20c), which is |
81 | | the member 'ukukey' of HASH(0x7d3bb0) [refcount 1], which is |
61 | | in the global %Test::hash. |
82 | | the global %Test::global_hash. |
62 | +- referenced by REF(0x814ec28), which is |
83 | +- referenced by REF(0x7ae2f0) [refcount 1], which is |
63 | | not found anywhere I looked :( |
84 | a temporary on the stack. |
64 | +- referenced by REF(0x814eb44), which is |
|
|
65 | in the member 'ukukey2' of HASH(0x814f99c), which is |
|
|
66 | +- referenced by REF(0x820c450), which is |
|
|
67 | | in the lexical '$local' in CODE(0x820c4b0), which was seen before. |
|
|
68 | +- referenced by REF(0x820c204), which is |
|
|
69 | in the global $Test::hash2. |
|
|
70 | |
85 | |
71 | It is a bit convoluted to read, but basically it says that the value |
86 | It is a bit convoluted to read, but basically it says that the value |
72 | stored in C<$var> can be found: |
87 | stored in C<$var> is referenced by: |
73 | |
88 | |
74 | =over 4 |
89 | =over 4 |
75 | |
90 | |
76 | =item - in some variable C<$x> whose origin is not known (I frankly have no |
91 | =item - the lexical C<$closure_var> (0x8abcc8), which is inside an instantiated |
77 | idea why, hints accepted). |
92 | closure, which in turn is used quite a bit. |
78 | |
93 | |
79 | =item - in the hash element with key C<ukukey> in the hash stored in C<%Test::hash>. |
94 | =item - the package-level lexical C<$global_my>. |
80 | |
95 | |
81 | =item - in the global variable named C<$Test::var>. |
96 | =item - the global package variable named C<$Test::var>. |
82 | |
97 | |
83 | =item - in the hash element C<ukukey2>, in the hash in the my variable |
98 | =item - the hash element C<ukukey2>, in the hash in the my variable |
84 | C<$local> in the sub C<Test::testsub> and also in the hash referenced by |
99 | C<$testsub_local> in the sub C<Test::testsub> and also in the hash |
|
|
100 | C<$referenced by Test::hash2>. |
|
|
101 | |
|
|
102 | =item - the hash element with key C<ukukey> in the hash stored in |
85 | C<$Test::hash2>. |
103 | C<%Test::hash>. |
|
|
104 | |
|
|
105 | =item - some anonymous mortalised reference on the stack (which is caused |
|
|
106 | by calling C<track> with the expression C<\$var>, which creates the |
|
|
107 | reference). |
86 | |
108 | |
87 | =back |
109 | =back |
|
|
110 | |
|
|
111 | And all these account for six reference counts. |
88 | |
112 | |
89 | =head1 EXPORTS |
113 | =head1 EXPORTS |
90 | |
114 | |
91 | None. |
115 | None. |
92 | |
116 | |
… | |
… | |
98 | |
122 | |
99 | Track the perl value pointed to by C<$ref> up to a depth of C<$depth> and |
123 | Track the perl value pointed to by C<$ref> up to a depth of C<$depth> and |
100 | return a descriptive string. C<$ref> can point at any perl value, be it |
124 | return a descriptive string. C<$ref> can point at any perl value, be it |
101 | anonymous sub, hash, array, scalar etc. |
125 | anonymous sub, hash, array, scalar etc. |
102 | |
126 | |
103 | This is the function you most often use. |
127 | This is the function you most likely want to use when tracking down |
|
|
128 | references. |
104 | |
129 | |
105 | =cut |
130 | =cut |
106 | |
131 | |
107 | sub find($); |
132 | sub find($); |
108 | |
133 | |
… | |
… | |
124 | |
149 | |
125 | if ($depth) { |
150 | if ($depth) { |
126 | my (@about) = find $$refref; |
151 | my (@about) = find $$refref; |
127 | if (@about) { |
152 | if (@about) { |
128 | for my $about (@about) { |
153 | for my $about (@about) { |
|
|
154 | $about->[0] =~ s/([^\x20-\x7e])/sprintf "\\{%02x}", ord $1/ge; |
129 | $buf .= "$indent" . (@about > 1 ? "+- " : " ") . $about->[0]; |
155 | $buf .= "$indent" . (@about > 1 ? "+- " : "") . $about->[0]; |
130 | if (@$about > 1) { |
156 | if (@$about > 1) { |
131 | if ($seen{ref2ptr $about->[1]}++) { |
157 | if ($seen{ref2ptr $about->[1]}++) { |
132 | $buf .= " " . (_f $about->[1]) . ", which was seen before.\n"; |
158 | $buf .= " " . (_f $about->[1]) . ", which was seen before.\n"; |
133 | } else { |
159 | } else { |
134 | $buf .= " " . (_f $about->[1]) . ", which is\n"; |
160 | $buf .= " " . (_f $about->[1]) . ", which is\n"; |
… | |
… | |
145 | $buf .= "$indent not referenced within the search depth.\n"; |
171 | $buf .= "$indent not referenced within the search depth.\n"; |
146 | } |
172 | } |
147 | }; |
173 | }; |
148 | |
174 | |
149 | $buf .= (_f $ref) . " is\n"; |
175 | $buf .= (_f $ref) . " is\n"; |
|
|
176 | |
150 | $track->(\$ref, $depth || $ENV{PERL_DEVEL_FINDREF_DEPTH} || 10, ""); |
177 | $track->(\$ref, $depth || $ENV{PERL_DEVEL_FINDREF_DEPTH} || 10, ""); |
151 | $buf |
178 | $buf |
152 | } |
179 | } |
153 | |
180 | |
154 | =item @references = Devel::FindRef::find $ref |
181 | =item @references = Devel::FindRef::find $ref |
… | |
… | |
164 | =cut |
191 | =cut |
165 | |
192 | |
166 | sub find($) { |
193 | sub find($) { |
167 | my ($about, $excl) = &find_; |
194 | my ($about, $excl) = &find_; |
168 | my %excl = map +($_ => undef), @$excl; |
195 | my %excl = map +($_ => undef), @$excl; |
169 | grep !exists $excl{ref2ptr $_->[1]}, @$about |
196 | grep !($#$_ && exists $excl{ref2ptr $_->[1]}), @$about |
170 | } |
197 | } |
171 | |
198 | |
172 | =item $ref = Devel::FindRef::ptr2ref $integer |
199 | =item $ref = Devel::FindRef::ptr2ref $integer |
173 | |
200 | |
174 | Sometimes you know (from debugging output) the address of a perl scalar |
201 | Sometimes you know (from debugging output) the address of a perl value you |
175 | you are interested in (e.g. C<HASH(0x176ff70)>). This function can be used |
202 | are interested in (e.g. C<HASH(0x176ff70)>). This function can be used to |
176 | to turn the address into a reference to that scalar. It is quite safe to |
203 | turn the address into a reference to that value. It is quite safe to call |
177 | call on valid addresses, but extremely dangerous to call on invalid ones. |
204 | on valid addresses, but extremely dangerous to call on invalid ones. I<No |
|
|
205 | checks whatsoever will be done>, so don't use this unless you really know |
|
|
206 | the value is the address of a valid perl value. |
178 | |
207 | |
179 | # we know that HASH(0x176ff70) exists, so turn it into a hashref: |
208 | # we know that HASH(0x176ff70) exists, so turn it into a hashref: |
180 | my $ref_to_hash = Devel::FindRef::ptr2ref 0x176ff70; |
209 | my $ref_to_hash = Devel::FindRef::ptr2ref 0x176ff70; |
181 | |
210 | |
182 | =item $ref = Devel::FindRef::ref2ptr $reference |
211 | =item $ptr = Devel::FindRef::ref2ptr $reference |
183 | |
212 | |
184 | The opposite of C<ptr2ref>, above: returns the internal address of the |
213 | The opposite of C<ptr2ref>, above: returns the internal address of the |
185 | value pointed to by the passed reference. I<No checks whatsoever will be |
214 | value pointed to by the passed reference. This function is safe to call on |
186 | done>, so don't use this. |
215 | anything, and returns the same value that a normal reference would if used |
|
|
216 | in a numeric context. |
187 | |
217 | |
188 | =back |
218 | =back |
189 | |
219 | |
190 | =head1 ENVIRONMENT VARIABLES |
220 | =head1 ENVIRONMENT VARIABLES |
191 | |
221 | |
192 | You can set the environment variable C<PERL_DEVEL_FINDREF_DEPTH> to an |
222 | You can set the environment variable C<PERL_DEVEL_FINDREF_DEPTH> to an |
193 | integer to override the default depth in C<track>. If a call explicitly |
223 | integer to override the default depth in C<track>. If a call explicitly |
194 | specified a depth it is not overridden. |
224 | specifies a depth, it is not overridden. |
195 | |
225 | |
196 | =head1 AUTHOR |
226 | =head1 AUTHOR |
197 | |
227 | |
198 | Marc Lehmann <pcg@goof.com>. |
228 | Marc Lehmann <pcg@goof.com>. |
199 | |
229 | |
200 | =head1 COPYRIGHT AND LICENSE |
230 | =head1 COPYRIGHT AND LICENSE |
201 | |
231 | |
202 | Copyright (C) 2007, 2008 by Marc Lehmann. |
232 | Copyright (C) 2007, 2008, 2009, 2013 by Marc Lehmann. |
203 | |
233 | |
204 | This library is free software; you can redistribute it and/or modify |
234 | This library is free software; you can redistribute it and/or modify |
205 | it under the same terms as Perl itself, either Perl version 5.8.8 or, |
235 | it under the same terms as Perl itself, either Perl version 5.8.8 or, |
206 | at your option, any later version of Perl 5 you may have available. |
236 | at your option, any later version of Perl 5 you may have available. |
207 | |
237 | |