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.8 by root, Wed Nov 28 12:20:33 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;
6use Scalar::Util;
6 7
7BEGIN { 8BEGIN {
8 our $VERSION = '1.0'; 9 our $VERSION = '1.1';
9 XSLoader::load __PACKAGE__, $VERSION; 10 XSLoader::load __PACKAGE__, $VERSION;
10} 11}
11 12
12=head1 NAME 13=head1 NAME
13 14
100=cut 101=cut
101 102
102sub find($); 103sub find($);
103 104
104sub track { 105sub track {
106 my ($ref, $depth) = @_;
107 @_ = ();
108
105 my $buf = ""; 109 my $buf = "";
106 my %ignore; 110
111 Scalar::Util::weaken $ref;
107 112
108 my $track; $track = sub { 113 my $track; $track = sub {
109 my ($target, $depth, $indent) = @_; 114 my ($refref, $depth, $indent) = @_;
110 @_ = ();
111 local $ignore{$target+0} = undef;
112 115
113 if ($depth) { 116 if ($depth) {
114 my (@about) = grep !exists $ignore{$_->[1]}, find $target; 117 my (@about) = find $$refref;
115 if (@about) { 118 if (@about) {
116 local @ignore{map $_->[1]+0, @about} = ();
117 for my $about (@about) { 119 for my $about (@about) {
118 local $ignore{$about+0} = undef;
119 $buf .= (" ") x $indent; 120 $buf .= (" ") x $indent;
120 $buf .= $about->[0]; 121 $buf .= $about->[0];
121 if (@$about > 1) { 122 if (@$about > 1) {
122 $buf .= " $about->[1], which is\n"; 123 $buf .= " $about->[1], which is\n";
123 $track->($about->[1], $depth - 1, $indent + 1); 124 $track->(\$about->[1], $depth - 1, $indent + 1);
124 } else { 125 } else {
125 $buf .= ".\n"; 126 $buf .= ".\n";
126 } 127 }
127 } 128 }
128 } else { 129 } else {
133 $buf .= (" ") x $indent; 134 $buf .= (" ") x $indent;
134 $buf .= "not referenced within the search depth.\n"; 135 $buf .= "not referenced within the search depth.\n";
135 } 136 }
136 }; 137 };
137 138
138 $buf .= "$_[0] is\n"; 139 $buf .= "$ref is\n";
139 $track->($_[0], $_[1] || 10, 1); 140 $track->(\$ref, $depth || 10, 1);
140 $buf 141 $buf
141} 142}
142 143
143=item @references = Devel::FindRef::find $ref 144=item @references = Devel::FindRef::find $ref
144 145
145Return arrayrefs that contain [$message, $ref] pairs. The message 146Return arrayrefs that contain [$message, $ref] pairs. The message
146describes 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
147reference 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
148search. 149search. The returned references are all weak references.
149 150
150The 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
151interested in and recurses on the returned references. 152interested in and recurses on the returned references.
152 153
153=cut 154=cut

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines