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 | |
6 | use Scalar::Util; |
7 | |
7 | |
8 | BEGIN { |
8 | BEGIN { |
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 | |
103 | sub find($); |
103 | sub find($); |
104 | |
104 | |
105 | sub track { |
105 | sub 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 | |
146 | Return arrayrefs that contain [$message, $ref] pairs. The message |
146 | Return arrayrefs that contain [$message, $ref] pairs. The message |
147 | 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 |
148 | 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 |
149 | search. |
149 | search. The returned references are all weak references. |
150 | |
150 | |
151 | 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 |
152 | interested in and recurses on the returned references. |
152 | interested 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 | |
164 | Sometimes you know (from debugging output) the address of a perl scalar |
164 | Sometimes you know (from debugging output) the address of a perl scalar |
165 | you are interested in. This function can be used to turn the address into |
165 | you are interested in (e.g. C<HASH(0x176ff70)>). This function can be used |
166 | a reference to that scalar. It is quite safe to call on valid addresses, |
166 | to turn the address into a reference to that scalar. It is quite safe to |
167 | but extremely dangerous to call on invalid ones. |
167 | call 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 | |