| 1 |
root |
1.2 |
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 |
root |
1.3 |
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 |
root |
1.2 |
|
| 13 |
root |
1.3 |
The "track" function can help track down some of those references back |
| 14 |
|
|
to the variables containing them. |
| 15 |
root |
1.2 |
|
| 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 |
root |
1.3 |
The output is as follows (or similar to this, in case I forget to update |
| 33 |
|
|
the manpage after some changes): |
| 34 |
root |
1.2 |
|
| 35 |
|
|
SCALAR(0x676fa0) is |
| 36 |
|
|
referenced by REF(0x676fb0), which is |
| 37 |
|
|
in the lexical '$x' in CODE(0x676370), which is |
| 38 |
|
|
not found anywhere I looked :( |
| 39 |
|
|
referenced by REF(0x676360), which is |
| 40 |
|
|
in the member 'ukukey' of HASH(0x756660), which is |
| 41 |
|
|
in the global %Test::hash. |
| 42 |
|
|
in the global $Test::var. |
| 43 |
|
|
referenced by REF(0x6760e0), which is |
| 44 |
|
|
in the member 'ukukey2' of HASH(0x676f30), which is |
| 45 |
|
|
referenced by REF(0x77bcf0), which is |
| 46 |
|
|
in the lexical '$local' in CODE(0x77bcb0), which is |
| 47 |
|
|
in the global &Test::testsub. |
| 48 |
|
|
referenced by REF(0x77bc80), which is |
| 49 |
|
|
in the global $Test::hash2. |
| 50 |
|
|
|
| 51 |
|
|
It is a bit convoluted to read, but basically it says that the value |
| 52 |
|
|
stored in $var can be found: |
| 53 |
|
|
|
| 54 |
|
|
- in some variable $x whose origin is not known (I frankly have no idea |
| 55 |
|
|
why, hints accepted). |
| 56 |
|
|
- in the hash element with key "ukukey" in the hash stored in |
| 57 |
|
|
%Test::hash. |
| 58 |
|
|
- in the global variable named $Test::var. |
| 59 |
|
|
- 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 |
| 61 |
|
|
$Test::hash2. |
| 62 |
|
|
|
| 63 |
|
|
EXPORTS |
| 64 |
root |
1.4 |
None. |
| 65 |
root |
1.2 |
|
| 66 |
|
|
FUNCTIONS |
| 67 |
root |
1.4 |
$string = Devel::FindRef::track $ref[, $depth] |
| 68 |
|
|
Track the perl value pointed to by $ref up to a depth of $depth and |
| 69 |
|
|
return a descriptive string. $ref can point at any perl value, be it |
| 70 |
|
|
anonymous sub, hash, array, scalar etc. |
| 71 |
|
|
|
| 72 |
|
|
This is the function you most often use. |
| 73 |
|
|
|
| 74 |
|
|
@references = Devel::FindRef::find $ref |
| 75 |
|
|
Return arrayrefs that contain [$message, $ref] pairs. The message |
| 76 |
|
|
describes what kind of reference was found and the $ref is the |
| 77 |
|
|
reference itself, which cna be omitted if "find" decided to end the |
| 78 |
|
|
search. |
| 79 |
|
|
|
| 80 |
|
|
The "track" function uses this to find references to the value you |
| 81 |
|
|
are interested in and recurses on the returned references. |
| 82 |
|
|
|
| 83 |
|
|
$ref = Devel::FindRef::ptr2ref $integer |
| 84 |
|
|
Sometimes you know (from debugging output) the address of a perl |
| 85 |
|
|
scalar you are interested in (e.g. "HASH(0x176ff70)"). This function |
| 86 |
|
|
can be used to turn the address into a reference to that scalar. It |
| 87 |
|
|
is quite safe to call on valid addresses, but extremely dangerous to |
| 88 |
|
|
call on invalid ones. |
| 89 |
|
|
|
| 90 |
|
|
# we know that HASH(0x176ff70) exists, so turn it into a hashref: |
| 91 |
|
|
my $ref_to_hash = Devel::FindRef::ptr2ref 0x176ff70; |
| 92 |
root |
1.2 |
|
| 93 |
|
|
AUTHOR |
| 94 |
root |
1.4 |
Marc Lehmann <pcg@goof.com>. |
| 95 |
root |
1.2 |
|
| 96 |
|
|
BUGS |
| 97 |
root |
1.4 |
Only code values, arrays, hashes, scalars and magic are being looked at. |
| 98 |
root |
1.2 |
|
| 99 |
root |
1.4 |
This is a quick hack only. |
| 100 |
root |
1.3 |
|
| 101 |
root |
1.2 |
COPYRIGHT AND LICENSE |
| 102 |
root |
1.4 |
Copyright (C) 2007 by Marc Lehmann. |
| 103 |
root |
1.2 |
|
| 104 |
root |
1.4 |
This library is free software; you can redistribute it and/or modify it |
| 105 |
|
|
under the same terms as Perl itself, either Perl version 5.8.8 or, at |
| 106 |
|
|
your option, any later version of Perl 5 you may have available. |
| 107 |
root |
1.2 |
|