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.19 by root, Mon Dec 1 13:47:09 2008 UTC vs.
Revision 1.28 by root, Sat Apr 13 06:29:39 2013 UTC

1package Devel::FindRef; 1package Devel::FindRef;
2 2
3no warnings; # I hate warning nazis 3use common::sense;
4use strict;
5 4
6use XSLoader; 5use XSLoader;
7use Scalar::Util; 6use Scalar::Util;
8 7
9BEGIN { 8BEGIN {
10 our $VERSION = '1.4'; 9 our $VERSION = '1.422';
11 XSLoader::load __PACKAGE__, $VERSION; 10 XSLoader::load __PACKAGE__, $VERSION;
12} 11}
13 12
14=head1 NAME 13=head1 NAME
15 14
60The output is as follows (or similar to this, in case I forget to update 59The output is as follows (or similar to this, in case I forget to update
61the manpage after some changes): 60the manpage after some changes):
62 61
63 SCALAR(0x7cc888) [refcount 6] is 62 SCALAR(0x7cc888) [refcount 6] is
64 +- referenced by REF(0x8abcc8) [refcount 1], which is 63 +- referenced by REF(0x8abcc8) [refcount 1], which is
65 | in the lexical '$closure_var' in CODE(0x8abc50) [refcount 4], which is 64 | the lexical '$closure_var' in CODE(0x8abc50) [refcount 4], which is
66 | +- the closure created at tst:18. 65 | +- the closure created at tst:18.
67 | +- referenced by REF(0x7d3c58) [refcount 1], which is 66 | +- referenced by REF(0x7d3c58) [refcount 1], which is
68 | | in the lexical '$closure' in CODE(0x7ae530) [refcount 2], which is 67 | | the lexical '$closure' in CODE(0x7ae530) [refcount 2], which is
69 | | +- the containing scope for CODE(0x8ab430) [refcount 3], which is 68 | | +- the containing scope for CODE(0x8ab430) [refcount 3], which is
70 | | | in the global &Test::testsub. 69 | | | the global &Test::testsub.
71 | | +- the main body of the program. 70 | | +- the main body of the program.
72 | +- in the lexical '&' in CODE(0x7ae530) [refcount 2], which was seen before. 71 | +- the lexical '&' in CODE(0x7ae530) [refcount 2], which was seen before.
73 +- referenced by REF(0x7cc7c8) [refcount 1], which is 72 +- referenced by REF(0x7cc7c8) [refcount 1], which is
74 | in the lexical '$global_my' in CODE(0x7ae530) [refcount 2], which was seen before. 73 | the lexical '$global_my' in CODE(0x7ae530) [refcount 2], which was seen before.
75 +- in the global $Test::var. 74 +- the global $Test::var.
76 +- referenced by REF(0x7cc558) [refcount 1], which is 75 +- referenced by REF(0x7cc558) [refcount 1], which is
77 | in the member 'ukukey2' of HASH(0x7ae140) [refcount 2], which is 76 | the member 'ukukey2' of HASH(0x7ae140) [refcount 2], which is
78 | +- referenced by REF(0x8abad0) [refcount 1], which is 77 | +- referenced by REF(0x8abad0) [refcount 1], which is
79 | | in the lexical '$testsub_local' in CODE(0x8ab430) [refcount 3], which was seen before. 78 | | the lexical '$testsub_local' in CODE(0x8ab430) [refcount 3], which was seen before.
80 | +- referenced by REF(0x8ab4f0) [refcount 1], which is 79 | +- referenced by REF(0x8ab4f0) [refcount 1], which is
81 | in the global $Test::global_hashref. 80 | the global $Test::global_hashref.
82 +- referenced by REF(0x7ae518) [refcount 1], which is 81 +- referenced by REF(0x7ae518) [refcount 1], which is
83 | in the member 'ukukey' of HASH(0x7d3bb0) [refcount 1], which is 82 | the member 'ukukey' of HASH(0x7d3bb0) [refcount 1], which is
84 | in the global %Test::global_hash. 83 | the global %Test::global_hash.
85 +- referenced by REF(0x7ae2f0) [refcount 1], which is 84 +- referenced by REF(0x7ae2f0) [refcount 1], which is
86 a temporary on the stack. 85 a temporary on the stack.
87 86
88It is a bit convoluted to read, but basically it says that the value 87It is a bit convoluted to read, but basically it says that the value
89stored in C<$var> is referenced by: 88stored in C<$var> is referenced by:
90 89
91=over 4 90=over 4
92 91
93=item - in the lexical C<$closure_var> (0x8abcc8), which is inside an instantiated 92=item - the lexical C<$closure_var> (0x8abcc8), which is inside an instantiated
94closure, which in turn is used quite a bit. 93closure, which in turn is used quite a bit.
95 94
96=item - in the package-level lexical C<$global_my>. 95=item - the package-level lexical C<$global_my>.
97 96
98=item - in the global package variable named C<$Test::var>. 97=item - the global package variable named C<$Test::var>.
99 98
100=item - in the hash element C<ukukey2>, in the hash in the my variable 99=item - the hash element C<ukukey2>, in the hash in the my variable
101C<$testsub_local> in the sub C<Test::testsub> and also in the hash 100C<$testsub_local> in the sub C<Test::testsub> and also in the hash
102C<$referenced by Test::hash2>. 101C<$referenced by Test::hash2>.
103 102
104=item - in the hash element with key C<ukukey> in the hash stored in 103=item - the hash element with key C<ukukey> in the hash stored in
105C<%Test::hash>. 104C<%Test::hash>.
106 105
107=item - some anonymous mortalised reference on the stack (which is caused 106=item - some anonymous mortalised reference on the stack (which is caused
108by calling C<track> with the expression C<\$var>, which creates the 107by calling C<track> with the expression C<\$var>, which creates the
109reference). 108reference).
151 150
152 if ($depth) { 151 if ($depth) {
153 my (@about) = find $$refref; 152 my (@about) = find $$refref;
154 if (@about) { 153 if (@about) {
155 for my $about (@about) { 154 for my $about (@about) {
155 $about->[0] =~ s/([^\x20-\x7e])/sprintf "\\{%02x}", ord $1/ge;
156 $buf .= "$indent" . (@about > 1 ? "+- " : "") . $about->[0]; 156 $buf .= "$indent" . (@about > 1 ? "+- " : "") . $about->[0];
157 if (@$about > 1) { 157 if (@$about > 1) {
158 if ($seen{ref2ptr $about->[1]}++) { 158 if ($seen{ref2ptr $about->[1]}++) {
159 $buf .= " " . (_f $about->[1]) . ", which was seen before.\n"; 159 $buf .= " " . (_f $about->[1]) . ", which was seen before.\n";
160 } else { 160 } else {
172 $buf .= "$indent not referenced within the search depth.\n"; 172 $buf .= "$indent not referenced within the search depth.\n";
173 } 173 }
174 }; 174 };
175 175
176 $buf .= (_f $ref) . " is\n"; 176 $buf .= (_f $ref) . " is\n";
177
177 $track->(\$ref, $depth || $ENV{PERL_DEVEL_FINDREF_DEPTH} || 10, ""); 178 $track->(\$ref, $depth || $ENV{PERL_DEVEL_FINDREF_DEPTH} || 10, "");
178 $buf 179 $buf
179} 180}
180 181
181=item @references = Devel::FindRef::find $ref 182=item @references = Devel::FindRef::find $ref
191=cut 192=cut
192 193
193sub find($) { 194sub find($) {
194 my ($about, $excl) = &find_; 195 my ($about, $excl) = &find_;
195 my %excl = map +($_ => undef), @$excl; 196 my %excl = map +($_ => undef), @$excl;
196 grep !exists $excl{ref2ptr $_->[1]}, @$about 197 grep !($#$_ && exists $excl{ref2ptr $_->[1]}), @$about
197} 198}
198 199
199=item $ref = Devel::FindRef::ptr2ref $integer 200=item $ref = Devel::FindRef::ptr2ref $integer
200 201
201Sometimes you know (from debugging output) the address of a perl scalar 202Sometimes you know (from debugging output) the address of a perl value you
202you are interested in (e.g. C<HASH(0x176ff70)>). This function can be used 203are interested in (e.g. C<HASH(0x176ff70)>). This function can be used to
203to turn the address into a reference to that scalar. It is quite safe to 204turn the address into a reference to that value. It is quite safe to call
204call on valid addresses, but extremely dangerous to call on invalid ones. 205on valid addresses, but extremely dangerous to call on invalid ones. I<No
206checks whatsoever will be done>, so don't use this unless you really know
207the value is the address of a valid perl value.
205 208
206 # we know that HASH(0x176ff70) exists, so turn it into a hashref: 209 # we know that HASH(0x176ff70) exists, so turn it into a hashref:
207 my $ref_to_hash = Devel::FindRef::ptr2ref 0x176ff70; 210 my $ref_to_hash = Devel::FindRef::ptr2ref 0x176ff70;
208 211
209=item $ref = Devel::FindRef::ref2ptr $reference 212=item $ptr = Devel::FindRef::ref2ptr $reference
210 213
211The opposite of C<ptr2ref>, above: returns the internal address of the 214The opposite of C<ptr2ref>, above: returns the internal address of the
212value pointed to by the passed reference. I<No checks whatsoever will be 215value pointed to by the passed reference. This function is safe to call on
213done>, so don't use this. 216anything, and returns the same value taht a normal reference would if used
217in a numeric context.
214 218
215=back 219=back
216 220
217=head1 ENVIRONMENT VARIABLES 221=head1 ENVIRONMENT VARIABLES
218 222

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines