1 |
NAME |
2 |
Devel::FindRef - where is that reference to my scalar hiding? |
3 |
|
4 |
SYNOPSIS |
5 |
use Devel::FindRef; |
6 |
|
7 |
DESCRIPTION |
8 |
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 |
10 |
be very hard. Fortunately, perl keeps track of all its values, so |
11 |
tracking references "backwards" is usually possible. |
12 |
|
13 |
The "track" function can help track down some of those references back |
14 |
to the variables containing them. |
15 |
|
16 |
For example, for this fragment: |
17 |
|
18 |
package Test; |
19 |
|
20 |
our $var = "hi\n"; |
21 |
my $x = \$var; |
22 |
our %hash = (ukukey => \$var); |
23 |
our $hash2 = {ukukey2 => \$var}; |
24 |
|
25 |
sub testsub { |
26 |
my $local = $hash2; |
27 |
print Devel::FindRef::track \$var; |
28 |
} |
29 |
|
30 |
testsub; |
31 |
|
32 |
The output is as follows (or similar to this, in case I forget to update |
33 |
the manpage after some changes): |
34 |
|
35 |
SCALAR(0x7bd2d0) is |
36 |
in the global $Test::var. |
37 |
referenced by REF(0x7bd240), which is |
38 |
in the member 'ukukey2' of HASH(0x7bd228), which is |
39 |
referenced by REF(0x81dae8), which is |
40 |
in the lexical '$local' in CODE(0x81da88), which is |
41 |
in the global &Test::testsub. |
42 |
referenced by REF(0x81da40), which is |
43 |
in the global $Test::hash2. |
44 |
referenced by REF(0x79f3f8), which is |
45 |
in the lexical '$x' in CODE(0x79f518), which is |
46 |
the containing scope for CODE(0x81da88), which is |
47 |
in the global &Test::testsub. |
48 |
referenced by REF(0x79f2f0), which is |
49 |
not found anywhere I looked :( |
50 |
referenced by REF(0x79f140), which is |
51 |
in the member 'ukukey' of HASH(0x81d698), which is |
52 |
in the global %Test::hash. |
53 |
|
54 |
It is a bit convoluted to read, but basically it says that the value |
55 |
stored in $var can be found: |
56 |
|
57 |
- in some variable $x whose origin is not known (I frankly have no idea |
58 |
why, hints accepted). |
59 |
- in the hash element with key "ukukey" in the hash stored in |
60 |
%Test::hash. |
61 |
- in the global variable named $Test::var. |
62 |
- in the hash element "ukukey2", in the hash in the my variable $local |
63 |
in the sub "Test::testsub" and also in the hash referenced by |
64 |
$Test::hash2. |
65 |
|
66 |
EXPORTS |
67 |
None. |
68 |
|
69 |
FUNCTIONS |
70 |
$string = Devel::FindRef::track $ref[, $depth] |
71 |
Track the perl value pointed to by $ref up to a depth of $depth and |
72 |
return a descriptive string. $ref can point at any perl value, be it |
73 |
anonymous sub, hash, array, scalar etc. |
74 |
|
75 |
This is the function you most often use. |
76 |
|
77 |
@references = Devel::FindRef::find $ref |
78 |
Return arrayrefs that contain [$message, $ref] pairs. The message |
79 |
describes what kind of reference was found and the $ref is the |
80 |
reference itself, which can be omitted if "find" decided to end the |
81 |
search. The returned references are all weak references. |
82 |
|
83 |
The "track" function uses this to find references to the value you |
84 |
are interested in and recurses on the returned references. |
85 |
|
86 |
$ref = Devel::FindRef::ptr2ref $integer |
87 |
Sometimes you know (from debugging output) the address of a perl |
88 |
scalar you are interested in (e.g. "HASH(0x176ff70)"). This function |
89 |
can be used to turn the address into a reference to that scalar. It |
90 |
is quite safe to call on valid addresses, but extremely dangerous to |
91 |
call on invalid ones. |
92 |
|
93 |
# we know that HASH(0x176ff70) exists, so turn it into a hashref: |
94 |
my $ref_to_hash = Devel::FindRef::ptr2ref 0x176ff70; |
95 |
|
96 |
AUTHOR |
97 |
Marc Lehmann <pcg@goof.com>. |
98 |
|
99 |
BUGS |
100 |
Only code values, arrays, hashes, scalars and magic are being looked at. |
101 |
|
102 |
This is a quick hack only. |
103 |
|
104 |
COPYRIGHT AND LICENSE |
105 |
Copyright (C) 2007 by Marc Lehmann. |
106 |
|
107 |
This library is free software; you can redistribute it and/or modify it |
108 |
under the same terms as Perl itself, either Perl version 5.8.8 or, at |
109 |
your option, any later version of Perl 5 you may have available. |
110 |
|