ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Devel-FindRef/README
Revision: 1.12
Committed: Tue Jun 3 18:40:50 2014 UTC (9 years, 11 months ago) by root
Branch: MAIN
CVS Tags: rel-1_46, rel-1_44, rel-1_45, HEAD
Changes since 1.11: +1 -1 lines
Log Message:
1.44

File Contents

# User Rev Content
1 root 1.2 NAME
2 root 1.7 Devel::FindRef - where is that reference to my variable hiding?
3 root 1.2
4     SYNOPSIS
5     use Devel::FindRef;
6    
7 root 1.9 print Devel::FindRef::track \$some_variable;
8    
9 root 1.2 DESCRIPTION
10     Tracking down reference problems (e.g. you expect some object to be
11 root 1.3 destroyed, but there are still references to it that keep it alive) can
12     be very hard. Fortunately, perl keeps track of all its values, so
13     tracking references "backwards" is usually possible.
14 root 1.2
15 root 1.3 The "track" function can help track down some of those references back
16     to the variables containing them.
17 root 1.2
18     For example, for this fragment:
19    
20 root 1.11 package Test;
21 root 1.9
22     use Devel::FindRef;
23     use Scalar::Util;
24 root 1.10
25     our $var = "hi\n";
26 root 1.9 my $global_my = \$var;
27     our %global_hash = (ukukey => \$var);
28     our $global_hashref = { ukukey2 => \$var };
29 root 1.10
30 root 1.11 sub testsub {
31 root 1.9 my $testsub_local = $global_hashref;
32 root 1.2 print Devel::FindRef::track \$var;
33 root 1.11 }
34 root 1.9
35     my $closure = sub {
36     my $closure_var = \$_[0];
37     Scalar::Util::weaken (my $weak_ref = \$var);
38     testsub;
39     };
40    
41     $closure->($var);
42 root 1.2
43 root 1.3 The output is as follows (or similar to this, in case I forget to update
44     the manpage after some changes):
45 root 1.2
46 root 1.9 SCALAR(0x7cc888) [refcount 6] is
47     +- referenced by REF(0x8abcc8) [refcount 1], which is
48 root 1.11 | the lexical '$closure_var' in CODE(0x8abc50) [refcount 4], which is
49 root 1.9 | +- the closure created at tst:18.
50     | +- referenced by REF(0x7d3c58) [refcount 1], which is
51 root 1.11 | | the lexical '$closure' in CODE(0x7ae530) [refcount 2], which is
52 root 1.9 | | +- the containing scope for CODE(0x8ab430) [refcount 3], which is
53 root 1.11 | | | the global &Test::testsub.
54 root 1.9 | | +- the main body of the program.
55 root 1.11 | +- the lexical '&' in CODE(0x7ae530) [refcount 2], which was seen before.
56 root 1.9 +- referenced by REF(0x7cc7c8) [refcount 1], which is
57 root 1.11 | the lexical '$global_my' in CODE(0x7ae530) [refcount 2], which was seen before.
58     +- the global $Test::var.
59 root 1.9 +- referenced by REF(0x7cc558) [refcount 1], which is
60 root 1.11 | the member 'ukukey2' of HASH(0x7ae140) [refcount 2], which is
61 root 1.9 | +- referenced by REF(0x8abad0) [refcount 1], which is
62 root 1.11 | | the lexical '$testsub_local' in CODE(0x8ab430) [refcount 3], which was seen before.
63 root 1.9 | +- referenced by REF(0x8ab4f0) [refcount 1], which is
64 root 1.11 | the global $Test::global_hashref.
65 root 1.9 +- referenced by REF(0x7ae518) [refcount 1], which is
66 root 1.11 | the member 'ukukey' of HASH(0x7d3bb0) [refcount 1], which is
67     | the global %Test::global_hash.
68 root 1.9 +- referenced by REF(0x7ae2f0) [refcount 1], which is
69     a temporary on the stack.
70 root 1.2
71     It is a bit convoluted to read, but basically it says that the value
72 root 1.9 stored in $var is referenced by:
73 root 1.2
74 root 1.10 - the lexical $closure_var (0x8abcc8), which is inside an instantiated
75     closure, which in turn is used quite a bit.
76     - the package-level lexical $global_my.
77     - the global package variable named $Test::var.
78     - the hash element "ukukey2", in the hash in the my variable
79 root 1.9 $testsub_local in the sub "Test::testsub" and also in the hash
80     "$referenced by Test::hash2".
81 root 1.10 - the hash element with key "ukukey" in the hash stored in %Test::hash.
82 root 1.9 - 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.
87 root 1.2
88     EXPORTS
89 root 1.4 None.
90 root 1.2
91     FUNCTIONS
92 root 1.4 $string = Devel::FindRef::track $ref[, $depth]
93     Track the perl value pointed to by $ref up to a depth of $depth and
94     return a descriptive string. $ref can point at any perl value, be it
95     anonymous sub, hash, array, scalar etc.
96    
97 root 1.11 This is the function you most likely want to use when tracking down
98     references.
99 root 1.4
100     @references = Devel::FindRef::find $ref
101     Return arrayrefs that contain [$message, $ref] pairs. The message
102     describes what kind of reference was found and the $ref is the
103 root 1.5 reference itself, which can be omitted if "find" decided to end the
104     search. The returned references are all weak references.
105 root 1.4
106     The "track" function uses this to find references to the value you
107     are interested in and recurses on the returned references.
108    
109     $ref = Devel::FindRef::ptr2ref $integer
110     Sometimes you know (from debugging output) the address of a perl
111 root 1.11 value you are interested in (e.g. "HASH(0x176ff70)"). This function
112     can be used to turn the address into a reference to that value. It
113 root 1.4 is quite safe to call on valid addresses, but extremely dangerous to
114 root 1.11 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 root 1.4
118     # we know that HASH(0x176ff70) exists, so turn it into a hashref:
119     my $ref_to_hash = Devel::FindRef::ptr2ref 0x176ff70;
120 root 1.2
121 root 1.11 $ptr = Devel::FindRef::ref2ptr $reference
122 root 1.8 The opposite of "ptr2ref", above: returns the internal address of
123 root 1.11 the value pointed to by the passed reference. This function is safe
124 root 1.12 to call on anything, and returns the same value that a normal
125 root 1.11 reference would if used in a numeric context.
126 root 1.8
127 root 1.7 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 root 1.11 specifies a depth, it is not overridden.
131 root 1.7
132 root 1.2 AUTHOR
133 root 1.4 Marc Lehmann <pcg@goof.com>.
134 root 1.2
135     COPYRIGHT AND LICENSE
136 root 1.11 Copyright (C) 2007, 2008, 2009, 2013 by Marc Lehmann.
137 root 1.2
138 root 1.4 This library is free software; you can redistribute it and/or modify it
139     under the same terms as Perl itself, either Perl version 5.8.8 or, at
140     your option, any later version of Perl 5 you may have available.
141 root 1.2