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.1'; |
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 | |
… | |
… | |
78 | |
78 | |
79 | =item - in the hash element C<ukukey2>, in the hash in the my variable |
79 | =item - in the hash element C<ukukey2>, in the hash in the my variable |
80 | C<$local> in the sub C<Test::testsub> and also in the hash referenced by |
80 | C<$local> in the sub C<Test::testsub> and also in the hash referenced by |
81 | C<$Test::hash2>. |
81 | C<$Test::hash2>. |
82 | |
82 | |
|
|
83 | =back |
|
|
84 | |
83 | =head1 EXPORTS |
85 | =head1 EXPORTS |
84 | |
86 | |
85 | None. |
87 | None. |
86 | |
88 | |
87 | =head1 FUNCTIONS |
89 | =head1 FUNCTIONS |
… | |
… | |
99 | =cut |
101 | =cut |
100 | |
102 | |
101 | sub find($); |
103 | sub find($); |
102 | |
104 | |
103 | sub track { |
105 | sub track { |
|
|
106 | my ($ref, $depth) = @_; |
|
|
107 | @_ = (); |
|
|
108 | |
104 | my $buf = ""; |
109 | my $buf = ""; |
105 | |
110 | |
|
|
111 | Scalar::Util::weaken $ref; |
|
|
112 | |
106 | my $track; $track = sub { |
113 | my $track; $track = sub { |
107 | my (undef, $depth, $indent) = @_; |
114 | my ($refref, $depth, $indent) = @_; |
108 | |
115 | |
109 | if ($depth) { |
116 | if ($depth) { |
110 | my (@about) = find $_[0]; |
117 | my (@about) = find $$refref; |
111 | if (@about) { |
118 | if (@about) { |
112 | for my $about (@about) { |
119 | for my $about (@about) { |
113 | $buf .= (" ") x $indent; |
120 | $buf .= (" ") x $indent; |
114 | $buf .= $about->[0]; |
121 | $buf .= $about->[0]; |
115 | if (@$about > 1) { |
122 | if (@$about > 1) { |
116 | $buf .= " $about->[1], which is\n"; |
123 | $buf .= " $about->[1], which is\n"; |
117 | $track->($about->[1], $depth - 1, $indent + 1); |
124 | $track->(\$about->[1], $depth - 1, $indent + 1); |
118 | } else { |
125 | } else { |
119 | $buf .= ".\n"; |
126 | $buf .= ".\n"; |
120 | } |
127 | } |
121 | } |
128 | } |
122 | } else { |
129 | } else { |
… | |
… | |
127 | $buf .= (" ") x $indent; |
134 | $buf .= (" ") x $indent; |
128 | $buf .= "not referenced within the search depth.\n"; |
135 | $buf .= "not referenced within the search depth.\n"; |
129 | } |
136 | } |
130 | }; |
137 | }; |
131 | |
138 | |
132 | $buf .= "$_[0] is\n"; |
139 | $buf .= "$ref is\n"; |
133 | $track->($_[0], $_[1] || 10, 1); |
140 | $track->(\$ref, $depth || 10, 1); |
134 | $buf |
141 | $buf |
135 | } |
142 | } |
136 | |
143 | |
137 | =item @references = Devel::FindRef::find $ref |
144 | =item @references = Devel::FindRef::find $ref |
138 | |
145 | |
139 | Return arrayrefs that contain [$message, $ref] pairs. The message |
146 | Return arrayrefs that contain [$message, $ref] pairs. The message |
140 | 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 |
141 | 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 |
142 | search. |
149 | search. The returned references are all weak references. |
143 | |
150 | |
144 | 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 |
145 | interested in and recurses on the returned references. |
152 | interested in and recurses on the returned references. |
146 | |
153 | |
147 | =cut |
154 | =cut |
148 | |
155 | |
149 | sub find($) { |
156 | sub find($) { |
150 | my ($about, $excl) = &find_; |
157 | my ($about, $excl) = &find_; |
151 | my %excl = map +($_ => 1), @$excl; |
158 | my %excl = map +($_ => undef), @$excl; |
152 | grep !$excl{$_->[1] + 0}, @$about |
159 | grep !exists $excl{$_->[1] + 0}, @$about |
153 | } |
160 | } |
154 | |
161 | |
155 | =item $ref = Devel::FindRef::ref2ptr $ptr |
162 | =item $ref = Devel::FindRef::ptr2ref $integer |
156 | |
163 | |
157 | 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 |
158 | 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 |
159 | 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 |
160 | 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; |
161 | |
171 | |
162 | =back |
172 | =back |
163 | |
173 | |
164 | =head1 AUTHOR |
174 | =head1 AUTHOR |
165 | |
175 | |