1 | NAME |
1 | NAME |
2 | Devel::FindRef - where is that reference to my variable hiding? |
2 | Devel::FindRef - where is that reference to my variable hiding? |
3 | |
3 | |
4 | SYNOPSIS |
4 | SYNOPSIS |
5 | use Devel::FindRef; |
5 | use Devel::FindRef; |
|
|
6 | |
|
|
7 | print Devel::FindRef::track \$some_variable; |
6 | |
8 | |
7 | DESCRIPTION |
9 | DESCRIPTION |
8 | Tracking down reference problems (e.g. you expect some object to be |
10 | Tracking down reference problems (e.g. you expect some object to be |
9 | destroyed, but there are still references to it that keep it alive) can |
11 | destroyed, but there are still references to it that keep it alive) can |
10 | be very hard. Fortunately, perl keeps track of all its values, so |
12 | be very hard. Fortunately, perl keeps track of all its values, so |
… | |
… | |
14 | to the variables containing them. |
16 | to the variables containing them. |
15 | |
17 | |
16 | For example, for this fragment: |
18 | For example, for this fragment: |
17 | |
19 | |
18 | package Test; |
20 | package Test; |
|
|
21 | |
|
|
22 | use Devel::FindRef; |
|
|
23 | use Scalar::Util; |
19 | |
24 | |
20 | our $var = "hi\n"; |
25 | our $var = "hi\n"; |
21 | my $x = \$var; |
26 | my $global_my = \$var; |
22 | our %hash = (ukukey => \$var); |
27 | our %global_hash = (ukukey => \$var); |
23 | our $hash2 = {ukukey2 => \$var}; |
28 | our $global_hashref = { ukukey2 => \$var }; |
24 | |
29 | |
25 | sub testsub { |
30 | sub testsub { |
26 | my $local = $hash2; |
31 | my $testsub_local = $global_hashref; |
27 | print Devel::FindRef::track \$var; |
32 | print Devel::FindRef::track \$var; |
28 | } |
33 | } |
29 | |
34 | |
|
|
35 | |
|
|
36 | my $closure = sub { |
|
|
37 | my $closure_var = \$_[0]; |
|
|
38 | Scalar::Util::weaken (my $weak_ref = \$var); |
30 | testsub; |
39 | testsub; |
|
|
40 | }; |
|
|
41 | |
|
|
42 | $closure->($var); |
31 | |
43 | |
32 | The output is as follows (or similar to this, in case I forget to update |
44 | The output is as follows (or similar to this, in case I forget to update |
33 | the manpage after some changes): |
45 | the manpage after some changes): |
34 | |
46 | |
35 | SCALAR(0x814ece8) is |
47 | SCALAR(0x7cc888) [refcount 6] is |
|
|
48 | +- referenced by REF(0x8abcc8) [refcount 1], which is |
|
|
49 | | in the lexical '$closure_var' in CODE(0x8abc50) [refcount 4], which is |
|
|
50 | | +- the closure created at tst:18. |
|
|
51 | | +- referenced by REF(0x7d3c58) [refcount 1], which is |
|
|
52 | | | in the lexical '$closure' in CODE(0x7ae530) [refcount 2], which is |
|
|
53 | | | +- the containing scope for CODE(0x8ab430) [refcount 3], which is |
|
|
54 | | | | in the global &Test::testsub. |
|
|
55 | | | +- the main body of the program. |
|
|
56 | | +- in the lexical '&' in CODE(0x7ae530) [refcount 2], which was seen before. |
|
|
57 | +- referenced by REF(0x7cc7c8) [refcount 1], which is |
|
|
58 | | in the lexical '$global_my' in CODE(0x7ae530) [refcount 2], which was seen before. |
36 | +- in the global $Test::var. |
59 | +- in the global $Test::var. |
37 | +- referenced by REF(0x814f9e4), which is |
60 | +- referenced by REF(0x7cc558) [refcount 1], which is |
38 | | in the lexical '$x' in CODE(0x814ed78), which is |
|
|
39 | | the containing scope for CODE(0x820c4b0), which is |
|
|
40 | | in the global &Test::testsub. |
|
|
41 | +- referenced by REF(0x814ed6c), which is |
|
|
42 | | in the member 'ukukey' of HASH(0x81da20c), which is |
61 | | in the member 'ukukey2' of HASH(0x7ae140) [refcount 2], which is |
|
|
62 | | +- referenced by REF(0x8abad0) [refcount 1], which is |
|
|
63 | | | in the lexical '$testsub_local' in CODE(0x8ab430) [refcount 3], which was seen before. |
|
|
64 | | +- referenced by REF(0x8ab4f0) [refcount 1], which is |
|
|
65 | | in the global $Test::global_hashref. |
|
|
66 | +- referenced by REF(0x7ae518) [refcount 1], which is |
|
|
67 | | in the member 'ukukey' of HASH(0x7d3bb0) [refcount 1], which is |
43 | | in the global %Test::hash. |
68 | | in the global %Test::global_hash. |
44 | +- referenced by REF(0x814ec28), which is |
69 | +- referenced by REF(0x7ae2f0) [refcount 1], which is |
45 | | not found anywhere I looked :( |
70 | a temporary on the stack. |
46 | +- referenced by REF(0x814eb44), which is |
|
|
47 | in the member 'ukukey2' of HASH(0x814f99c), which is |
|
|
48 | +- referenced by REF(0x820c450), which is |
|
|
49 | | in the lexical '$local' in CODE(0x820c4b0), which was seen before. |
|
|
50 | +- referenced by REF(0x820c204), which is |
|
|
51 | in the global $Test::hash2. |
|
|
52 | |
71 | |
53 | It is a bit convoluted to read, but basically it says that the value |
72 | It is a bit convoluted to read, but basically it says that the value |
54 | stored in $var can be found: |
73 | stored in $var is referenced by: |
55 | |
74 | |
56 | - in some variable $x whose origin is not known (I frankly have no idea |
75 | - in the lexical $closure_var (0x8abcc8), which is inside an |
57 | why, hints accepted). |
76 | instantiated closure, which in turn is used quite a bit. |
|
|
77 | - in the package-level lexical $global_my. |
|
|
78 | - in the global package variable named $Test::var. |
|
|
79 | - in the hash element "ukukey2", in the hash in the my variable |
|
|
80 | $testsub_local in the sub "Test::testsub" and also in the hash |
|
|
81 | "$referenced by Test::hash2". |
58 | - in the hash element with key "ukukey" in the hash stored in |
82 | - in the hash element with key "ukukey" in the hash stored in |
59 | %Test::hash. |
83 | %Test::hash. |
60 | - in the global variable named $Test::var. |
84 | - some anonymous mortalised reference on the stack (which is caused by |
61 | - in the hash element "ukukey2", in the hash in the my variable $local |
85 | calling "track" with the expression "\$var", which creates the |
62 | in the sub "Test::testsub" and also in the hash referenced by |
86 | reference). |
63 | $Test::hash2. |
87 | |
|
|
88 | And all these account for six reference counts. |
64 | |
89 | |
65 | EXPORTS |
90 | EXPORTS |
66 | None. |
91 | None. |
67 | |
92 | |
68 | FUNCTIONS |
93 | FUNCTIONS |
… | |
… | |
90 | call on invalid ones. |
115 | call on invalid ones. |
91 | |
116 | |
92 | # we know that HASH(0x176ff70) exists, so turn it into a hashref: |
117 | # we know that HASH(0x176ff70) exists, so turn it into a hashref: |
93 | my $ref_to_hash = Devel::FindRef::ptr2ref 0x176ff70; |
118 | my $ref_to_hash = Devel::FindRef::ptr2ref 0x176ff70; |
94 | |
119 | |
|
|
120 | $ref = Devel::FindRef::ref2ptr $reference |
|
|
121 | The opposite of "ptr2ref", above: returns the internal address of |
|
|
122 | the value pointed to by the passed reference. *No checks whatsoever |
|
|
123 | will be done*, so don't use this. |
|
|
124 | |
95 | ENVIRONMENT VARIABLES |
125 | ENVIRONMENT VARIABLES |
96 | You can set the environment variable "PERL_DEVEL_FINDREF_DEPTH" to an |
126 | You can set the environment variable "PERL_DEVEL_FINDREF_DEPTH" to an |
97 | integer to override the default depth in "track". If a call explicitly |
127 | integer to override the default depth in "track". If a call explicitly |
98 | specified a depth it is not overridden. |
128 | specified a depth it is not overridden. |
99 | |
129 | |
100 | AUTHOR |
130 | AUTHOR |
101 | Marc Lehmann <pcg@goof.com>. |
131 | Marc Lehmann <pcg@goof.com>. |
102 | |
132 | |
103 | BUGS |
|
|
104 | Only code values, arrays, hashes, scalars and magic are being looked at. |
|
|
105 | |
|
|
106 | This is a quick hack only. |
|
|
107 | |
|
|
108 | COPYRIGHT AND LICENSE |
133 | COPYRIGHT AND LICENSE |
109 | Copyright (C) 2007 by Marc Lehmann. |
134 | Copyright (C) 2007, 2008 by Marc Lehmann. |
110 | |
135 | |
111 | This library is free software; you can redistribute it and/or modify it |
136 | This library is free software; you can redistribute it and/or modify it |
112 | under the same terms as Perl itself, either Perl version 5.8.8 or, at |
137 | under the same terms as Perl itself, either Perl version 5.8.8 or, at |
113 | your option, any later version of Perl 5 you may have available. |
138 | your option, any later version of Perl 5 you may have available. |
114 | |
139 | |