Devel::FindRef - where is that reference to my scalar hiding?
use Devel::FindRef;
Tracking down reference problems (e.g. you expect some object to be destroyed, but there are still references to it that keep it alive) can be very hard. Fortunately, perl keeps track of all its values, so tracking references "backwards" is usually possible.
The track function can help track down some of those references back to
the variables containing them.
For example, for this fragment:
package Test; our $var = "hi\n"; my $x = \$var; our %hash = (ukukey => \$var); our $hash2 = {ukukey2 => \$var}; sub testsub { my $local = $hash2; print Devel::FindRef::track \$var; } testsub;
The output is as follows (or similar to this, in case I forget to update the manpage after some changes):
SCALAR(0x676fa0) is
referenced by REF(0x676fb0), which is
in the lexical '$x' in CODE(0x676370), which is
not found anywhere I looked :(
referenced by REF(0x676360), which is
in the member 'ukukey' of HASH(0x756660), which is
in the global %Test::hash.
in the global $Test::var.
referenced by REF(0x6760e0), which is
in the member 'ukukey2' of HASH(0x676f30), which is
referenced by REF(0x77bcf0), which is
in the lexical '$local' in CODE(0x77bcb0), which is
in the global &Test::testsub.
referenced by REF(0x77bc80), which is
in the global $Test::hash2.
It is a bit convoluted to read, but basically it says that the value
stored in $var can be found:
$x whose origin is not known (I frankly have no
idea why, hints accepted).
ukukey in the hash stored in %Test::hash.
$Test::var.
ukukey2, in the hash in the my variable
$local in the sub Test::testsub and also in the hash referenced by
$Test::hash2.
None.
Track the perl value pointed to by $ref up to a depth of $depth and
return a descriptive string. $ref can point at any perl value, be it
anonymous sub, hash, array, scalar etc.
This is the function you most often use.
Return arrayrefs that contain [$message, $ref] pairs. The message
describes what kind of reference was found and the $ref is the
reference itself, which can be omitted if find decided to end the
search. The returned references are all weak references.
The track function uses this to find references to the value you are
interested in and recurses on the returned references.
Sometimes you know (from debugging output) the address of a perl scalar
you are interested in (e.g. HASH(0x176ff70)). This function can be used
to turn the address into a reference to that scalar. It is quite safe to
call on valid addresses, but extremely dangerous to call on invalid ones.
# we know that HASH(0x176ff70) exists, so turn it into a hashref: my $ref_to_hash = Devel::FindRef::ptr2ref 0x176ff70;
Marc Lehmann <pcg@goof.com>.
Only code values, arrays, hashes, scalars and magic are being looked at.
This is a quick hack only.
Copyright (C) 2007 by Marc Lehmann.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.