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.7 by root, Mon Apr 30 21:08:14 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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines