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