1 | NAME |
1 | NAME |
2 | Devel::FindRef - where is that reference to my scalar 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 | |
6 | |
|
|
7 | print Devel::FindRef::track \$some_variable; |
|
|
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, although perl keeps track of all values. |
12 | be very hard. Fortunately, perl keeps track of all its values, so |
|
|
13 | tracking references "backwards" is usually possible. |
11 | |
14 | |
12 | The "track" function can hlep track down some of those refernces back to |
15 | The "track" function can help track down some of those references back |
13 | the variables containing them. |
16 | to the variables containing them. |
14 | |
17 | |
15 | For example, for this fragment: |
18 | For example, for this fragment: |
16 | |
19 | |
17 | package Test; |
20 | package Test; |
|
|
21 | |
|
|
22 | use Devel::FindRef; |
|
|
23 | use Scalar::Util; |
18 | |
24 | |
19 | our $var = "hi\n"; |
25 | our $var = "hi\n"; |
20 | my $x = \$var; |
26 | my $global_my = \$var; |
21 | our %hash = (ukukey => \$var); |
27 | our %global_hash = (ukukey => \$var); |
22 | our $hash2 = {ukukey2 => \$var}; |
28 | our $global_hashref = { ukukey2 => \$var }; |
23 | |
29 | |
24 | sub testsub { |
30 | sub testsub { |
25 | my $local = $hash2; |
31 | my $testsub_local = $global_hashref; |
26 | print Devel::FindRef::track \$var; |
32 | print Devel::FindRef::track \$var; |
27 | } |
33 | } |
28 | |
|
|
29 | testsub; |
|
|
30 | |
34 | |
31 | The output is as follows (or similar to htis, in case I forget to update |
35 | my $closure = sub { |
32 | the manpage afetr some changes): |
36 | my $closure_var = \$_[0]; |
|
|
37 | Scalar::Util::weaken (my $weak_ref = \$var); |
|
|
38 | testsub; |
|
|
39 | }; |
33 | |
40 | |
34 | SCALAR(0x676fa0) is |
41 | $closure->($var); |
35 | referenced by REF(0x676fb0), which is |
42 | |
36 | in the lexical '$x' in CODE(0x676370), which is |
43 | The output is as follows (or similar to this, in case I forget to update |
37 | not found anywhere I looked :( |
44 | the manpage after some changes): |
38 | referenced by REF(0x676360), which is |
45 | |
39 | in the member 'ukukey' of HASH(0x756660), which is |
46 | SCALAR(0x7cc888) [refcount 6] is |
40 | in the global %Test::hash. |
47 | +- referenced by REF(0x8abcc8) [refcount 1], which is |
|
|
48 | | the lexical '$closure_var' in CODE(0x8abc50) [refcount 4], which is |
|
|
49 | | +- the closure created at tst:18. |
|
|
50 | | +- referenced by REF(0x7d3c58) [refcount 1], which is |
|
|
51 | | | the lexical '$closure' in CODE(0x7ae530) [refcount 2], which is |
|
|
52 | | | +- the containing scope for CODE(0x8ab430) [refcount 3], which is |
|
|
53 | | | | the global &Test::testsub. |
|
|
54 | | | +- the main body of the program. |
|
|
55 | | +- the lexical '&' in CODE(0x7ae530) [refcount 2], which was seen before. |
|
|
56 | +- referenced by REF(0x7cc7c8) [refcount 1], which is |
|
|
57 | | the lexical '$global_my' in CODE(0x7ae530) [refcount 2], which was seen before. |
41 | in the global $Test::var. |
58 | +- the global $Test::var. |
|
|
59 | +- referenced by REF(0x7cc558) [refcount 1], which is |
|
|
60 | | the member 'ukukey2' of HASH(0x7ae140) [refcount 2], which is |
|
|
61 | | +- referenced by REF(0x8abad0) [refcount 1], which is |
|
|
62 | | | the lexical '$testsub_local' in CODE(0x8ab430) [refcount 3], which was seen before. |
|
|
63 | | +- referenced by REF(0x8ab4f0) [refcount 1], which is |
|
|
64 | | the global $Test::global_hashref. |
|
|
65 | +- referenced by REF(0x7ae518) [refcount 1], which is |
|
|
66 | | the member 'ukukey' of HASH(0x7d3bb0) [refcount 1], which is |
|
|
67 | | the global %Test::global_hash. |
42 | referenced by REF(0x6760e0), which is |
68 | +- referenced by REF(0x7ae2f0) [refcount 1], which is |
43 | in the member 'ukukey2' of HASH(0x676f30), which is |
69 | a temporary on the stack. |
44 | referenced by REF(0x77bcf0), which is |
|
|
45 | in the lexical '$local' in CODE(0x77bcb0), which is |
|
|
46 | in the global &Test::testsub. |
|
|
47 | referenced by REF(0x77bc80), which is |
|
|
48 | in the global $Test::hash2. |
|
|
49 | |
70 | |
50 | It is a bit convoluted to read, but basically it says that the value |
71 | It is a bit convoluted to read, but basically it says that the value |
51 | stored in $var can be found: |
72 | stored in $var is referenced by: |
52 | |
73 | |
53 | - in some variable $x whose origin is not known (I frankly have no idea |
74 | - the lexical $closure_var (0x8abcc8), which is inside an instantiated |
54 | why, hints accepted). |
75 | closure, which in turn is used quite a bit. |
55 | - in the hash element with key "ukukey" in the hash stored in |
76 | - the package-level lexical $global_my. |
56 | %Test::hash. |
|
|
57 | - in the global variable named $Test::var. |
77 | - the global package variable named $Test::var. |
58 | - in the hash element "ukukey2", in the hash in the my variable $local |
78 | - the hash element "ukukey2", in the hash in the my variable |
59 | in the sub "Test::testsub" and also in the hash referenced by |
79 | $testsub_local in the sub "Test::testsub" and also in the hash |
60 | $Test::hash2. |
80 | "$referenced by Test::hash2". |
|
|
81 | - the hash element with key "ukukey" in the hash stored in %Test::hash. |
|
|
82 | - some anonymous mortalised reference on the stack (which is caused by |
|
|
83 | calling "track" with the expression "\$var", which creates the |
|
|
84 | reference). |
|
|
85 | |
|
|
86 | And all these account for six reference counts. |
61 | |
87 | |
62 | EXPORTS |
88 | EXPORTS |
63 | None. |
89 | None. |
64 | |
90 | |
65 | FUNCTIONS |
91 | FUNCTIONS |
66 | $string = Devel::FindRef::track $ref[, $depth] |
92 | $string = Devel::FindRef::track $ref[, $depth] |
67 | Track the perl value pointed to by $ref up to a depth of $depth |
93 | Track the perl value pointed to by $ref up to a depth of $depth and |
68 | and return a descriptive string. $ref can point at any perl |
94 | return a descriptive string. $ref can point at any perl value, be it |
69 | value, be it anonymous sub, hash, array, scalar etc. |
95 | anonymous sub, hash, array, scalar etc. |
70 | |
96 | |
71 | This is the function you most often use. |
97 | This is the function you most likely want to use when tracking down |
|
|
98 | references. |
72 | |
99 | |
73 | @references = Devel::FindRef::find $ref |
100 | @references = Devel::FindRef::find $ref |
74 | Return arrayrefs that contain [$message, $ref] pairs. The |
101 | Return arrayrefs that contain [$message, $ref] pairs. The message |
75 | message describes what kind of reference was found and the $ref |
102 | describes what kind of reference was found and the $ref is the |
76 | is the reference itself, which cna be omitted if "find" decided |
103 | reference itself, which can be omitted if "find" decided to end the |
77 | to end the search. |
104 | search. The returned references are all weak references. |
78 | |
105 | |
79 | The "track" function uses this to find references to the value |
106 | The "track" function uses this to find references to the value you |
80 | you are interested in and recurses on the returned references. |
107 | are interested in and recurses on the returned references. |
81 | |
108 | |
82 | $ref = Devel::FindRef::ref2ptr $ptr |
109 | $ref = Devel::FindRef::ptr2ref $integer |
83 | Sometimes you know (from debugging output) the address of a perl |
110 | Sometimes you know (from debugging output) the address of a perl |
84 | scalar you are interested in. This function can be used to turn |
111 | value you are interested in (e.g. "HASH(0x176ff70)"). This function |
85 | the address into a reference to that scalar. It is quite safe to |
112 | can be used to turn the address into a reference to that value. It |
86 | call on valid addresses, but extremely dangerous to call on |
113 | is quite safe to call on valid addresses, but extremely dangerous to |
87 | invalid ones. |
114 | call on invalid ones. *No checks whatsoever will be done*, so don't |
|
|
115 | use this unless you really know the value is the address of a valid |
|
|
116 | perl value. |
|
|
117 | |
|
|
118 | # we know that HASH(0x176ff70) exists, so turn it into a hashref: |
|
|
119 | my $ref_to_hash = Devel::FindRef::ptr2ref 0x176ff70; |
|
|
120 | |
|
|
121 | $ptr = Devel::FindRef::ref2ptr $reference |
|
|
122 | The opposite of "ptr2ref", above: returns the internal address of |
|
|
123 | the value pointed to by the passed reference. This function is safe |
|
|
124 | to call on anything, and returns the same value taht a normal |
|
|
125 | reference would if used in a numeric context. |
|
|
126 | |
|
|
127 | ENVIRONMENT VARIABLES |
|
|
128 | You can set the environment variable "PERL_DEVEL_FINDREF_DEPTH" to an |
|
|
129 | integer to override the default depth in "track". If a call explicitly |
|
|
130 | specifies a depth, it is not overridden. |
88 | |
131 | |
89 | AUTHOR |
132 | AUTHOR |
90 | Marc Lehmann <pcg@goof.com>. |
133 | Marc Lehmann <pcg@goof.com>. |
91 | |
|
|
92 | BUGS |
|
|
93 | Only code values, arrays, hashes, scalars and magic are being looked |
|
|
94 | at. |
|
|
95 | |
134 | |
96 | COPYRIGHT AND LICENSE |
135 | COPYRIGHT AND LICENSE |
97 | Copyright (C) 2007 by Marc Lehmann. |
136 | Copyright (C) 2007, 2008, 2009, 2013 by Marc Lehmann. |
98 | |
137 | |
99 | This library is free software; you can redistribute it and/or modify |
138 | This library is free software; you can redistribute it and/or modify it |
100 | it under the same terms as Perl itself, either Perl version 5.8.8 |
139 | under the same terms as Perl itself, either Perl version 5.8.8 or, at |
101 | or, at your option, any later version of Perl 5 you may have |
140 | your option, any later version of Perl 5 you may have available. |
102 | available. |
|
|
103 | |
141 | |