Info/MRO.pm untested

filter: Statementstmt 0.0% 0 / 110 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 185 CC 12 SCAR 27.7
CC
12
Cov
0%
_walk_global_data
56.0
_print_global_data
6.0
all_methods
6.0
CRAP 15.9
1 package SNMP::Info::MRO;
2  
3 use warnings;
4 use strict;
5  
6 our ($VERSION);
7 $VERSION = '3.985003';
8  
9 use PPI;
10 use Class::ISA; ## no critic
11 use Module::Info;
12 use Module::Load ();
13 # use Data::Printer;
14  
15 sub _walk_global_data {
16 my $self = shift;
17 my $class = (ref $self ? ref $self : $self);
18  
19 my $ppi = PPI::Document->new( file($class) );
20 my $name = shift or die "name (e.g. GLOBALS) required";
21 my $results = shift || {};
22 my $subref = \&_walk_global_data;
23  
24 # get the hash declaration
25 my $declaration = $ppi->find_first(sub {
26 my ($doc, $tok) = @_;
27 return ($tok->isa('PPI::Token::Symbol')
28 and $tok->symbol eq "\%$name");
29 });
30  
31 # get the hash content
32 my $content = $declaration->snext_sibling->snext_sibling;
33  
34 # get relevant tokens in the hash content
35 my @tokens = $content->find(sub {
36 my ($doc, $tok) = @_;
37 return ($tok->isa('PPI::Token::Symbol')
38 or $tok->isa('PPI::Token::Quote'));
39 });
40  
41 return $results unless scalar @tokens and ref $tokens[0] eq ref [];
42 @tokens = @{ $tokens[0] };
43  
44 # walk tokens and build final result
45 while (my $tok = splice(@tokens, 0, 1)) {
46 if ($tok->isa('PPI::Token::Quote')) {
47 my $token = $tok->string;
48 my $leaf = splice(@tokens, 0, 1);
49 my $extract = (($leaf =~ m/^&/) ? 'symbol' : 'string');
50  
51 unshift @{ $results->{$token} },
52 [ $class => $leaf->$extract ]
53 # we can sometimes see the same package twice
54 unless scalar grep { $_ eq $class }
55 map { $_->[0] }
56 @{ $results->{$token} };
57 }
58 elsif ($tok->isa('PPI::Token::Symbol')) {
59 # recurse to get the results of the mentioned package
60 (my $otherpkg = $tok->symbol) =~ s/^\%(.+)::$name$/$1/;
61 $results = $subref->($otherpkg, $name, $results);
62 }
63 }
64  
65 return $results;
66 }
67  
68 sub _print_global_data {
69 my $results = _walk_global_data(@_);
70  
71 foreach my $key (sort keys %$results) {
72 print $key, "\n";
73 my @defs = @{ $results->{$key} };
74  
75 my $first = 0;
76 while (my $classdef = splice(@defs, 0, 1)) {
77 my $class = $classdef->[0];
78 my $meth = $classdef->[1];
79  
80 if ($first) {
81 printf " %s ( %s )\n", $meth, $class;
82 }
83 else {
84 printf " `-- %s ( %s )\n", $meth, $class;
85 $first = 1;
86 }
87 }
88 }
89 }
90  
91 =head1 NAME
92  
93 SNMP::Info::MRO - Method resolution introspection for SNMP::Info
94  
95 =head1 SYNOPSIS
96  
97 use SNMP::Info::MRO;
98 use Data::Printer;
99  
100 # SNMP::Info::MRO::print_* functions
101 SNMP::Info::MRO::print_superclasses ('SNMP::Info::Layer3::Juniper');
102  
103 # print output using Data::Printer for other functions
104 my $buff = SNMP::Info::MRO::all_methods('SNMP::Info::Layer3::Juniper');
105 p $buff;
106  
107 =head1 DESCRIPTION
108  
109 This is a set of helpers to show where a given method in SNMP::Info has been
110 implemented, and which implementation is being used at runtime.
111  
112 The following distributions are I<required> to run this code:
113  
114 =over 4
115  
116 =item *
117  
118 PPI
119  
120 =item *
121  
122 Class::ISA
123  
124 =item *
125  
126 Module::Info
127  
128 =item *
129  
130 Module::Load
131  
132 =back
133  
134 =head1 FUNCTIONS
135  
136 None of the functions are exported. For all helper functions, you can pass
137 either the name of a Perl module, or an object instance of SNMP::Info.
138  
139 =over 4
140  
141 =item all_methods( $module )
142  
143 Returns the location of methods defined in C<$module> and all its ancestor
144 classes (superclasses), either as Perl subroutines or via C<%GLOBALS>
145 or C<%FUNCS> configuration. The data structure looks like:
146  
147 {
148 method_name => {
149 globals => [
150 [ Package::Name => 'mib_leaf.0' ],
151 [ Other::Package::Name => '1.3.6.1.4.1.9.2.1.58.0' ],
152 ],
153 },
154 other_method_name => [
155 subs => [
156 'Package::Name',
157 ],
158 funcs => [
159 [ Package::Name => 'mib_leaf_name' ],
160 ],
161 ],
162 }
163  
164 It should be noted that the order of method resolution in SNMP::Info is to
165 first look for a defined subroutine (this is done by Perl), then the
166 AUTOLOAD sequence will search for a definition in C<%GLOBALS> followed by
167 C<%FUNCS>.
168  
169 The defining class or module at runtime is always the first entry in the
170 list, if it exists:
171  
172 $data->{method_name}->{subs}->[0]
173 if exists $data->{method_name}->{subs};
174  
175 =cut
176  
177 sub all_methods {
178 my $self = shift;
179 my $class = (ref $self ? ref $self : $self);
180  
181 my $results = subroutines( $class );
182 $results = { map { $_ => { subs => $results->{$_} } }
183 keys %$results };
184  
185 my $globals = globals( $class );
186 foreach my $key (keys %$globals) {
187 $results->{$key}->{globals} = $globals->{$key};
188 }
189  
190 my $funcs = funcs( $class );
191 foreach my $key (keys %$funcs) {
192 $results->{$key}->{funcs} = $funcs->{$key};
193 }
194  
195 #foreach my $key (keys %$results) {
196 # $results->{$key}->{subs} ||= [];
197 # $results->{$key}->{globals} ||= [];
198 # $results->{$key}->{funcs} ||= [];
199 #}
200  
201 return $results;
202 }
203  
204 =item subroutines( $module )
205  
206 Returns the set of subroutines defined in C<$module> and all its ancestor
207 classes (superclasses). The data structure looks like:
208  
209 {
210 method_name => [
211 'Package::Name',
212 'Other::Package::Name',
213 ],
214 other_method_name => [
215 'Package::Name',
216 ],
217 }
218  
219 Should a subroutine have been defined more than once,
220 the defining classes are listed in reverse order, such that the definition
221 used at runtime is always:
222  
223 $data->{method_name}->[0];
224  
225 =cut
226  
227 sub subroutines {
228 my $self = shift;
229 my $class = (ref $self ? ref $self : $self);
230 my $results = {};
231  
232 my @super = superclasses($class);
233 foreach my $parent (reverse @super) {
234 my %sh = Module::Info->new_from_module( $parent )->subroutines;
235 my @subs = grep { $_ !~ m/^_/ }
236 map { $_ =~ s/^.+:://; $_ } ## no critic
237 keys %sh;
238  
239 foreach my $sub (@subs) {
240 unshift @{ $results->{$sub} }, $parent;
241 }
242 }
243  
244 return $results;
245 }
246  
247 =item globals( $module || $object )
248  
249 Returns a data structure showing how L<SNMP::Info> will resolve MIB Leaf
250 Nodes configured through the C<%GLOBALS> hashes in C<$module>.
251  
252 The data structure looks like:
253  
254 {
255 method_name => [
256 [ Package::Name => 'mib_leaf_name' ],
257 [ Other::Package::Name => '1.3.6.1.4.1.9.2.1.58.0' ],
258 ],
259 other_method_name => [
260 [ Package::Name => 'mib_leaf.0' ],
261 ],
262 }
263  
264 Where a method has been defined in different packages, then they are listed in
265 reverse order, such that the mapping used by SNMP::Info is always:
266  
267 $data->{method_name}->[0];
268  
269 =cut
270  
271 sub globals { _walk_global_data(shift, 'GLOBALS') }
272  
273 =item funcs( $module || $object )
274  
275 Returns a data structure showing how L<SNMP::Info> will resolve MIB Tables
276 configured through the C<%FUNCS> hashes in C<$module>.
277  
278 See L<SNMP::Info::Layer3/"GLOBALS"> for further detail.
279  
280 =cut
281  
282 sub funcs { _walk_global_data(shift, 'FUNCS') }
283  
284 =item munge( $module || $object )
285  
286 Returns a data structure showing the subroutines used for munging returned
287 values for any method defined in C<%FUNCS> or C<%GLOBALS>.
288  
289 The data structure looks like:
290  
291 {
292 method_name => [
293 [ Package::Name => '&subroutine' ],
294 [ Other::Package::Name => '&Other::Package::subroutine' ],
295 ],
296 other_method_name => [
297 [ Package::Name => '&subroutine' ],
298 ],
299 }
300  
301 Where a mapping has been defined in different packages, then they are listed
302 in reverse order, such that the munge subroutine used by SNMP::Info is always:
303  
304 $data->{method_name}->[0];
305  
306 =cut
307  
308 sub munge { _walk_global_data(shift, 'MUNGE') }
309  
310 =item file( $module )
311  
312 Returns the filename from which Perl will load the given module.
313  
314 =cut
315  
316 sub file {
317 my $self = shift;
318 my $class = (ref $self ? ref $self : $self);
319  
320 return Module::Info->new_from_module( $class )->file;
321 }
322  
323 =item superclasses( $class || $object )
324  
325 Returns the list (in order) of the names of classes Perl will search to find
326 methods for this SNMP::Info class or object instance.
327  
328 Note this B<requires> the L<Class::ISA> distribution to be installed.
329  
330 =cut
331  
332 sub superclasses {
333 my $self = shift;
334 my $class = (ref $self ? ref $self : $self);
335  
336 Module::Load::load( $class );
337 return Class::ISA::self_and_super_path( $class );
338 }
339  
340 =item print_globals( $module || $object )
341  
342 Pretty print the output of C<globals()>.
343  
344 =cut
345  
346 sub print_globals { _print_global_data(shift, 'GLOBALS') }
347  
348 =item print_funcs( $module || $object )
349  
350 Pretty print the output of C<funcs()>.
351  
352 =cut
353  
354 sub print_funcs { _print_global_data(shift, 'FUNCS') }
355  
356 =item print_munge( $module || $object )
357  
358 Pretty print the output of C<munge()>.
359  
360 =cut
361  
362 sub print_munge { _print_global_data(shift, 'MUNGE') }
363  
364 =item print_superclasses( $class || $object )
365  
366 Pretty print the output of C<superclasses()>.
367  
368 =cut
369  
370 sub print_superclasses {
371 print join ("\n", superclasses(@_)), "\n";
372 }
373  
374 =back
375  
376 =head1 AUTHOR
377  
378 Oliver Gorwits <oliver@co-ding.org>
379  
380 =head1 COPYRIGHT AND LICENSE
381  
382 This software is copyright (c) 2014 by The SNMP::Info Project.
383  
384 # Redistribution and use in source and binary forms, with or without
385 # modification, are permitted provided that the following conditions are met:
386 #
387 # * Redistributions of source code must retain the above copyright notice,
388 # this list of conditions and the following disclaimer.
389 # * Redistributions in binary form must reproduce the above copyright
390 # notice, this list of conditions and the following disclaimer in the
391 # documentation and/or other materials provided with the distribution.
392 # * Neither the name of the University of California, Santa Cruz nor the
393 # names of its contributors may be used to endorse or promote products
394 # derived from this software without specific prior written permission.
395 #
396 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
397 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
398 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
399 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
400 # LIABLE FOR # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
401 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
402 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
403 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
404 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
405 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
406 # POSSIBILITY OF SUCH DAMAGE.
407  
408 =cut
409  
410 1;