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 | DESCRIPTION |
7 | DESCRIPTION |
… | |
… | |
14 | to the variables containing them. |
14 | to the variables containing them. |
15 | |
15 | |
16 | For example, for this fragment: |
16 | For example, for this fragment: |
17 | |
17 | |
18 | package Test; |
18 | package Test; |
19 | |
19 | |
20 | our $var = "hi\n"; |
20 | our $var = "hi\n"; |
21 | my $x = \$var; |
21 | my $x = \$var; |
22 | our %hash = (ukukey => \$var); |
22 | our %hash = (ukukey => \$var); |
23 | our $hash2 = {ukukey2 => \$var}; |
23 | our $hash2 = {ukukey2 => \$var}; |
24 | |
24 | |
25 | sub testsub { |
25 | sub testsub { |
26 | my $local = $hash2; |
26 | my $local = $hash2; |
27 | print Devel::FindRef::track \$var; |
27 | print Devel::FindRef::track \$var; |
28 | } |
28 | } |
29 | |
29 | |
30 | testsub; |
30 | testsub; |
31 | |
31 | |
32 | The output is as follows (or similar to this, in case I forget to update |
32 | The output is as follows (or similar to this, in case I forget to update |
33 | the manpage after some changes): |
33 | the manpage after some changes): |
34 | |
34 | |
35 | SCALAR(0x676fa0) is |
35 | SCALAR(0x814ece8) is |
|
|
36 | +- in the global $Test::var. |
36 | referenced by REF(0x676fb0), which is |
37 | +- referenced by REF(0x814f9e4), which is |
37 | in the lexical '$x' in CODE(0x676370), which is |
38 | | in the lexical '$x' in CODE(0x814ed78), which is |
38 | not found anywhere I looked :( |
39 | | the containing scope for CODE(0x820c4b0), which is |
|
|
40 | | in the global &Test::testsub. |
39 | referenced by REF(0x676360), which is |
41 | +- referenced by REF(0x814ed6c), which is |
40 | in the member 'ukukey' of HASH(0x756660), which is |
42 | | in the member 'ukukey' of HASH(0x81da20c), which is |
41 | in the global %Test::hash. |
43 | | in the global %Test::hash. |
42 | in the global $Test::var. |
|
|
43 | referenced by REF(0x6760e0), which is |
44 | +- referenced by REF(0x814ec28), which is |
|
|
45 | | not found anywhere I looked :( |
|
|
46 | +- referenced by REF(0x814eb44), which is |
44 | in the member 'ukukey2' of HASH(0x676f30), which is |
47 | in the member 'ukukey2' of HASH(0x814f99c), which is |
45 | referenced by REF(0x77bcf0), which is |
48 | +- referenced by REF(0x820c450), which is |
46 | in the lexical '$local' in CODE(0x77bcb0), which is |
49 | | in the lexical '$local' in CODE(0x820c4b0), which was seen before. |
47 | in the global &Test::testsub. |
|
|
48 | referenced by REF(0x77bc80), which is |
50 | +- referenced by REF(0x820c204), which is |
49 | in the global $Test::hash2. |
51 | in the global $Test::hash2. |
50 | |
52 | |
51 | It is a bit convoluted to read, but basically it says that the value |
53 | It is a bit convoluted to read, but basically it says that the value |
52 | stored in $var can be found: |
54 | stored in $var can be found: |
53 | |
55 | |
… | |
… | |
59 | - in the hash element "ukukey2", in the hash in the my variable $local |
61 | - in the hash element "ukukey2", in the hash in the my variable $local |
60 | in the sub "Test::testsub" and also in the hash referenced by |
62 | in the sub "Test::testsub" and also in the hash referenced by |
61 | $Test::hash2. |
63 | $Test::hash2. |
62 | |
64 | |
63 | EXPORTS |
65 | EXPORTS |
64 | None. |
66 | None. |
65 | |
67 | |
66 | FUNCTIONS |
68 | FUNCTIONS |
67 | $string = Devel::FindRef::track $ref[, $depth] |
69 | $string = Devel::FindRef::track $ref[, $depth] |
68 | Track the perl value pointed to by $ref up to a depth of $depth |
70 | Track the perl value pointed to by $ref up to a depth of $depth and |
69 | and return a descriptive string. $ref can point at any perl |
71 | return a descriptive string. $ref can point at any perl value, be it |
70 | value, be it anonymous sub, hash, array, scalar etc. |
72 | anonymous sub, hash, array, scalar etc. |
71 | |
73 | |
72 | This is the function you most often use. |
74 | This is the function you most often use. |
73 | |
75 | |
74 | @references = Devel::FindRef::find $ref |
76 | @references = Devel::FindRef::find $ref |
75 | Return arrayrefs that contain [$message, $ref] pairs. The |
77 | Return arrayrefs that contain [$message, $ref] pairs. The message |
76 | message describes what kind of reference was found and the $ref |
78 | describes what kind of reference was found and the $ref is the |
77 | is the reference itself, which cna be omitted if "find" decided |
79 | reference itself, which can be omitted if "find" decided to end the |
78 | to end the search. |
80 | search. The returned references are all weak references. |
79 | |
81 | |
80 | The "track" function uses this to find references to the value |
82 | The "track" function uses this to find references to the value you |
81 | you are interested in and recurses on the returned references. |
83 | are interested in and recurses on the returned references. |
82 | |
84 | |
83 | $ref = Devel::FindRef::ref2ptr $ptr |
85 | $ref = Devel::FindRef::ptr2ref $integer |
84 | Sometimes you know (from debugging output) the address of a perl |
86 | Sometimes you know (from debugging output) the address of a perl |
85 | scalar you are interested in. This function can be used to turn |
87 | scalar you are interested in (e.g. "HASH(0x176ff70)"). This function |
86 | the address into a reference to that scalar. It is quite safe to |
88 | can be used to turn the address into a reference to that scalar. It |
87 | call on valid addresses, but extremely dangerous to call on |
89 | is quite safe to call on valid addresses, but extremely dangerous to |
88 | invalid ones. |
90 | call on invalid ones. |
|
|
91 | |
|
|
92 | # we know that HASH(0x176ff70) exists, so turn it into a hashref: |
|
|
93 | my $ref_to_hash = Devel::FindRef::ptr2ref 0x176ff70; |
|
|
94 | |
|
|
95 | $ref = Devel::FindRef::ref2ptr $reference |
|
|
96 | The opposite of "ptr2ref", above: returns the internal address of |
|
|
97 | the value pointed to by the passed reference. *No checks whatsoever |
|
|
98 | will be done*, so don't use this. |
|
|
99 | |
|
|
100 | ENVIRONMENT VARIABLES |
|
|
101 | You can set the environment variable "PERL_DEVEL_FINDREF_DEPTH" to an |
|
|
102 | integer to override the default depth in "track". If a call explicitly |
|
|
103 | specified a depth it is not overridden. |
89 | |
104 | |
90 | AUTHOR |
105 | AUTHOR |
91 | Marc Lehmann <pcg@goof.com>. |
106 | Marc Lehmann <pcg@goof.com>. |
92 | |
107 | |
93 | BUGS |
108 | BUGS |
94 | Only code values, arrays, hashes, scalars and magic are being looked |
109 | Only code values, arrays, hashes, scalars and magic are being looked at. |
95 | at. |
|
|
96 | |
110 | |
97 | This is a quick hack only. |
111 | This is a quick hack only. |
98 | |
112 | |
99 | COPYRIGHT AND LICENSE |
113 | COPYRIGHT AND LICENSE |
100 | Copyright (C) 2007 by Marc Lehmann. |
114 | Copyright (C) 2007 by Marc Lehmann. |
101 | |
115 | |
102 | This library is free software; you can redistribute it and/or modify |
116 | This library is free software; you can redistribute it and/or modify it |
103 | it under the same terms as Perl itself, either Perl version 5.8.8 |
117 | under the same terms as Perl itself, either Perl version 5.8.8 or, at |
104 | or, at your option, any later version of Perl 5 you may have |
118 | your option, any later version of Perl 5 you may have available. |
105 | available. |
|
|
106 | |
119 | |