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.6 by root, Sat Mar 24 19:18:18 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.2'; 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
101=cut 101=cut
102 102
103sub find($); 103sub find($);
104 104
105sub track { 105sub track {
106 my ($ref, $depth) = @_;
107 @_ = ();
108
106 my $buf = ""; 109 my $buf = "";
107 my %ignore; 110
111 Scalar::Util::weaken $ref;
108 112
109 my $track; $track = sub { 113 my $track; $track = sub {
110 my ($target, $depth, $indent) = @_; 114 my ($refref, $depth, $indent) = @_;
111 @_ = ();
112 local $ignore{$target+0} = undef;
113 115
114 if ($depth) { 116 if ($depth) {
115 my (@about) = grep !exists $ignore{$_->[1]}, find $target; 117 my (@about) = find $$refref;
116 if (@about) { 118 if (@about) {
117 local @ignore{map $_->[1]+0, @about} = ();
118 for my $about (@about) { 119 for my $about (@about) {
119 local $ignore{$about+0} = undef;
120 $buf .= (" ") x $indent; 120 $buf .= (" ") x $indent;
121 $buf .= $about->[0]; 121 $buf .= $about->[0];
122 if (@$about > 1) { 122 if (@$about > 1) {
123 $buf .= " $about->[1], which is\n"; 123 $buf .= " $about->[1], which is\n";
124 $track->($about->[1], $depth - 1, $indent + 1); 124 $track->(\$about->[1], $depth - 1, $indent + 1);
125 } else { 125 } else {
126 $buf .= ".\n"; 126 $buf .= ".\n";
127 } 127 }
128 } 128 }
129 } else { 129 } else {
134 $buf .= (" ") x $indent; 134 $buf .= (" ") x $indent;
135 $buf .= "not referenced within the search depth.\n"; 135 $buf .= "not referenced within the search depth.\n";
136 } 136 }
137 }; 137 };
138 138
139 $buf .= "$_[0] is\n"; 139 $buf .= "$ref is\n";
140 $track->($_[0], $_[1] || 10, 1); 140 $track->(\$ref, $depth || 10, 1);
141 $buf 141 $buf
142} 142}
143 143
144=item @references = Devel::FindRef::find $ref 144=item @references = Devel::FindRef::find $ref
145 145
146Return arrayrefs that contain [$message, $ref] pairs. The message 146Return arrayrefs that contain [$message, $ref] pairs. The message
147describes 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
148reference 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
149search. 149search. The returned references are all weak references.
150 150
151The 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
152interested in and recurses on the returned references. 152interested in and recurses on the returned references.
153 153
154=cut 154=cut
157 my ($about, $excl) = &find_; 157 my ($about, $excl) = &find_;
158 my %excl = map +($_ => undef), @$excl; 158 my %excl = map +($_ => undef), @$excl;
159 grep !exists $excl{$_->[1] + 0}, @$about 159 grep !exists $excl{$_->[1] + 0}, @$about
160} 160}
161 161
162=item $ref = Devel::FindRef::ref2ptr $ptr 162=item $ref = Devel::FindRef::ptr2ref $integer
163 163
164Sometimes you know (from debugging output) the address of a perl scalar 164Sometimes you know (from debugging output) the address of a perl scalar
165you 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
166a 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
167but 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;
168 171
169=back 172=back
170 173
171=head1 AUTHOR 174=head1 AUTHOR
172 175

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines