1 | package Devel::FindRef; |
1 | package Devel::FindRef; |
2 | |
2 | |
3 | use strict; |
3 | use strict; |
4 | |
4 | |
5 | use XSLoader; |
5 | use XSLoader; |
|
|
6 | use Scalar::Util; |
6 | |
7 | |
7 | BEGIN { |
8 | BEGIN { |
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 | |
102 | sub find($); |
103 | sub find($); |
103 | |
104 | |
104 | sub track { |
105 | sub 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 | |
145 | Return arrayrefs that contain [$message, $ref] pairs. The message |
146 | Return arrayrefs that contain [$message, $ref] pairs. The message |
146 | describes what kind of reference was found and the C<$ref> is the |
147 | describes what kind of reference was found and the C<$ref> is the |
147 | reference itself, which cna be omitted if C<find> decided to end the |
148 | reference itself, which can be omitted if C<find> decided to end the |
148 | search. |
149 | search. The returned references are all weak references. |
149 | |
150 | |
150 | The C<track> function uses this to find references to the value you are |
151 | The C<track> function uses this to find references to the value you are |
151 | interested in and recurses on the returned references. |
152 | interested in and recurses on the returned references. |
152 | |
153 | |
153 | =cut |
154 | =cut |