ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Devel-FindRef/FindRef.pm
(Generate patch)

Comparing Devel-FindRef/FindRef.pm (file contents):
Revision 1.13 by root, Fri Jul 11 22:18:10 2008 UTC vs.
Revision 1.18 by root, Mon Dec 1 13:22:43 2008 UTC

1package Devel::FindRef; 1package Devel::FindRef;
2 2
3no warnings; # I hate warning nazis
3use strict; 4use strict;
4 5
5use XSLoader; 6use XSLoader;
6use Scalar::Util; 7use Scalar::Util;
7 8
8BEGIN { 9BEGIN {
9 our $VERSION = '1.3'; 10 our $VERSION = '1.31';
10 XSLoader::load __PACKAGE__, $VERSION; 11 XSLoader::load __PACKAGE__, $VERSION;
11} 12}
12 13
13=head1 NAME 14=head1 NAME
14 15
15Devel::FindRef - where is that reference to my variable hiding? 16Devel::FindRef - where is that reference to my variable hiding?
16 17
17=head1 SYNOPSIS 18=head1 SYNOPSIS
18 19
19 use Devel::FindRef; 20 use Devel::FindRef;
21
22 print Devel::FindRef::track \$some_variable;
20 23
21=head1 DESCRIPTION 24=head1 DESCRIPTION
22 25
23Tracking down reference problems (e.g. you expect some object to be 26Tracking down reference problems (e.g. you expect some object to be
24destroyed, but there are still references to it that keep it alive) can be 27destroyed, but there are still references to it that keep it alive) can be
101 104
102=cut 105=cut
103 106
104sub find($); 107sub find($);
105 108
109sub _f($) {
110 "$_[0] [refcount " . (_refcnt $_[0]) . "]"
111}
112
106sub track { 113sub track {
107 my ($ref, $depth) = @_; 114 my ($ref, $depth) = @_;
108 @_ = (); 115 @_ = ();
109 116
110 my $buf = ""; 117 my $buf = "";
119 my (@about) = find $$refref; 126 my (@about) = find $$refref;
120 if (@about) { 127 if (@about) {
121 for my $about (@about) { 128 for my $about (@about) {
122 $buf .= "$indent" . (@about > 1 ? "+- " : " ") . $about->[0]; 129 $buf .= "$indent" . (@about > 1 ? "+- " : " ") . $about->[0];
123 if (@$about > 1) { 130 if (@$about > 1) {
124 if ($seen{$about->[1]+0}++) { 131 if ($seen{ref2ptr $about->[1]}++) {
125 $buf .= " $about->[1], which was seen before.\n"; 132 $buf .= " " . (_f $about->[1]) . ", which was seen before.\n";
126 } else { 133 } else {
127 $buf .= " $about->[1], which is\n"; 134 $buf .= " " . (_f $about->[1]) . ", which is\n";
128 $track->(\$about->[1], $depth - 1, $about == $about[-1] ? "$indent " : "$indent| "); 135 $track->(\$about->[1], $depth - 1, $about == $about[-1] ? "$indent " : "$indent| ");
129 } 136 }
130 } else { 137 } else {
131 $buf .= ".\n"; 138 $buf .= ".\n";
132 } 139 }
137 } else { 144 } else {
138 $buf .= "$indent not referenced within the search depth.\n"; 145 $buf .= "$indent not referenced within the search depth.\n";
139 } 146 }
140 }; 147 };
141 148
142 $buf .= "$ref is\n"; 149 $buf .= (_f $ref) . " is\n";
143 $track->(\$ref, $depth || $ENV{PERL_DEVEL_FINDREF_DEPTH} || 10, ""); 150 $track->(\$ref, $depth || $ENV{PERL_DEVEL_FINDREF_DEPTH} || 10, "");
144 $buf 151 $buf
145} 152}
146 153
147=item @references = Devel::FindRef::find $ref 154=item @references = Devel::FindRef::find $ref
157=cut 164=cut
158 165
159sub find($) { 166sub find($) {
160 my ($about, $excl) = &find_; 167 my ($about, $excl) = &find_;
161 my %excl = map +($_ => undef), @$excl; 168 my %excl = map +($_ => undef), @$excl;
162 grep !exists $excl{$_->[1] + 0}, @$about 169 grep !exists $excl{ref2ptr $_->[1]}, @$about
163} 170}
164 171
165=item $ref = Devel::FindRef::ptr2ref $integer 172=item $ref = Devel::FindRef::ptr2ref $integer
166 173
167Sometimes you know (from debugging output) the address of a perl scalar 174Sometimes you know (from debugging output) the address of a perl scalar
170call on valid addresses, but extremely dangerous to call on invalid ones. 177call on valid addresses, but extremely dangerous to call on invalid ones.
171 178
172 # we know that HASH(0x176ff70) exists, so turn it into a hashref: 179 # we know that HASH(0x176ff70) exists, so turn it into a hashref:
173 my $ref_to_hash = Devel::FindRef::ptr2ref 0x176ff70; 180 my $ref_to_hash = Devel::FindRef::ptr2ref 0x176ff70;
174 181
182=item $ref = Devel::FindRef::ref2ptr $reference
183
184The opposite of C<ptr2ref>, above: returns the internal address of the
185value pointed to by the passed reference. I<No checks whatsoever will be
186done>, so don't use this.
187
175=back 188=back
176 189
177=head1 ENVIRONMENT VARIABLES 190=head1 ENVIRONMENT VARIABLES
178 191
179You can set the environment variable C<PERL_DEVEL_FINDREF_DEPTH> to an 192You can set the environment variable C<PERL_DEVEL_FINDREF_DEPTH> to an
182 195
183=head1 AUTHOR 196=head1 AUTHOR
184 197
185Marc Lehmann <pcg@goof.com>. 198Marc Lehmann <pcg@goof.com>.
186 199
187=head1 BUGS
188
189Only code values, arrays, hashes, scalars and magic are being looked at.
190
191This is a quick hack only.
192
193=head1 COPYRIGHT AND LICENSE 200=head1 COPYRIGHT AND LICENSE
194 201
195Copyright (C) 2007 by Marc Lehmann. 202Copyright (C) 2007, 2008 by Marc Lehmann.
196 203
197This library is free software; you can redistribute it and/or modify 204This library is free software; you can redistribute it and/or modify
198it under the same terms as Perl itself, either Perl version 5.8.8 or, 205it under the same terms as Perl itself, either Perl version 5.8.8 or,
199at your option, any later version of Perl 5 you may have available. 206at your option, any later version of Perl 5 you may have available.
200 207

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines