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.4 by root, Fri Jan 12 10:26:55 2007 UTC vs.
Revision 1.9 by root, Sat Dec 29 21:04:46 2007 UTC

1package Devel::FindRef; 1package Devel::FindRef;
2 2
3use strict; 3use strict;
4 4
5use XSLoader; 5use XSLoader;
6 6use Scalar::Util;
7 7
8BEGIN { 8BEGIN {
9 our $VERSION = '0.1'; 9 our $VERSION = '1.1';
10 XSLoader::load __PACKAGE__, $VERSION; 10 XSLoader::load __PACKAGE__, $VERSION;
11} 11}
12 12
13=head1 NAME 13=head1 NAME
14 14
78 78
79=item - in the hash element C<ukukey2>, in the hash in the my variable 79=item - in the hash element C<ukukey2>, in the hash in the my variable
80C<$local> in the sub C<Test::testsub> and also in the hash referenced by 80C<$local> in the sub C<Test::testsub> and also in the hash referenced by
81C<$Test::hash2>. 81C<$Test::hash2>.
82 82
83=back
84
83=head1 EXPORTS 85=head1 EXPORTS
84 86
85None. 87None.
86 88
87=head1 FUNCTIONS 89=head1 FUNCTIONS
99=cut 101=cut
100 102
101sub find($); 103sub find($);
102 104
103sub track { 105sub track {
106 my ($ref, $depth) = @_;
107 @_ = ();
108
104 my $buf = ""; 109 my $buf = "";
105 110
111 Scalar::Util::weaken $ref;
112
106 my $track; $track = sub { 113 my $track; $track = sub {
107 my (undef, $depth, $indent) = @_; 114 my ($refref, $depth, $indent) = @_;
108 115
109 if ($depth) { 116 if ($depth) {
110 my (@about) = find $_[0]; 117 my (@about) = find $$refref;
111 if (@about) { 118 if (@about) {
112 for my $about (@about) { 119 for my $about (@about) {
113 $buf .= (" ") x $indent; 120 $buf .= (" ") x $indent;
114 $buf .= $about->[0]; 121 $buf .= $about->[0];
115 if (@$about > 1) { 122 if (@$about > 1) {
116 $buf .= " $about->[1], which is\n"; 123 $buf .= " $about->[1], which is\n";
117 $track->($about->[1], $depth - 1, $indent + 1); 124 $track->(\$about->[1], $depth - 1, $indent + 1);
118 } else { 125 } else {
119 $buf .= ".\n"; 126 $buf .= ".\n";
120 } 127 }
121 } 128 }
122 } else { 129 } else {
127 $buf .= (" ") x $indent; 134 $buf .= (" ") x $indent;
128 $buf .= "not referenced within the search depth.\n"; 135 $buf .= "not referenced within the search depth.\n";
129 } 136 }
130 }; 137 };
131 138
132 $buf .= "$_[0] is\n"; 139 $buf .= "$ref is\n";
133 $track->($_[0], $_[1] || 10, 1); 140 $track->(\$ref, $depth || 10, 1);
134 $buf 141 $buf
135} 142}
136 143
137=item @references = Devel::FindRef::find $ref 144=item @references = Devel::FindRef::find $ref
138 145
139Return arrayrefs that contain [$message, $ref] pairs. The message 146Return arrayrefs that contain [$message, $ref] pairs. The message
140describes what kind of reference was found and the C<$ref> is the 147describes what kind of reference was found and the C<$ref> is the
141reference itself, which cna be omitted if C<find> decided to end the 148reference itself, which can be omitted if C<find> decided to end the
142search. 149search. The returned references are all weak references.
143 150
144The C<track> function uses this to find references to the value you are 151The C<track> function uses this to find references to the value you are
145interested in and recurses on the returned references. 152interested in and recurses on the returned references.
146 153
147=cut 154=cut
148 155
149sub find($) { 156sub find($) {
150 my ($about, $excl) = &find_; 157 my ($about, $excl) = &find_;
151 my %excl = map +($_ => 1), @$excl; 158 my %excl = map +($_ => undef), @$excl;
152 grep !$excl{$_->[1] + 0}, @$about 159 grep !exists $excl{$_->[1] + 0}, @$about
153} 160}
154 161
155=item $ref = Devel::FindRef::ref2ptr $ptr 162=item $ref = Devel::FindRef::ptr2ref $integer
156 163
157Sometimes you know (from debugging output) the address of a perl scalar 164Sometimes you know (from debugging output) the address of a perl scalar
158you are interested in. This function can be used to turn the address into 165you are interested in (e.g. C<HASH(0x176ff70)>). This function can be used
159a reference to that scalar. It is quite safe to call on valid addresses, 166to turn the address into a reference to that scalar. It is quite safe to
160but extremely dangerous to call on invalid ones. 167call on valid addresses, but extremely dangerous to call on invalid ones.
168
169 # we know that HASH(0x176ff70) exists, so turn it into a hashref:
170 my $ref_to_hash = Devel::FindRef::ptr2ref 0x176ff70;
161 171
162=back 172=back
163 173
164=head1 AUTHOR 174=head1 AUTHOR
165 175

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines