File Coverage

blib/lib/SNMP/ToolBox.pm
Criterion Covered Total %
statement 39 40 97.5
branch 15 16 93.7
condition 15 19 78.9
subroutine 8 9 88.8
pod 2 2 100.0
total 79 86 91.8


line stmt bran cond sub pod time code
1             package SNMP::ToolBox;
2 5     5   5651 use strict;
  5         14  
  5         198  
3 5     5   27 use warnings;
  5         9  
  5         228  
4              
5 5     5   4295 use parent qw< Exporter >;
  5         1674  
  5         37  
6              
7 5     5   265 use Carp;
  5         11  
  5         389  
8              
9              
10             {
11 5     5   26 no strict "vars";
  5         13  
  5         2491  
12             $VERSION = '0.03';
13             @EXPORT = qw< by_oid find_next_oid oid_encode >;
14              
15             if ($] < 5.008) {
16             *by_oid = \&_by_oid_vstring;
17             }
18             else {
19             *by_oid = \&_by_oid_classical;
20             }
21             }
22              
23              
24             #
25             # _by_oid_classical()
26             # -----------------
27             # by Sebastien Aperghis-Tramoni
28             #
29             sub _by_oid_classical ($$) {
30 5     5   2920 my (undef, @a) = split /\./, $_[0];
31 5         23 my (undef, @b) = split /\./, $_[1];
32 5 50       14 my $n = $#a > $#b ? $#a : $#b;
33 5         7 my $v = 0;
34 5   50     220 $v ||= ($a[$_]||0) <=> ($b[$_]||0), $v && return $v for 0 .. $n;
      50        
      66        
      100        
35 1         5 return $v
36             }
37              
38              
39             #
40             # _by_oid_vstring()
41             # ---------------
42             # by Vincent Pit
43             #
44             sub _by_oid_vstring ($$) {
45 0     0   0 eval($_[0]) cmp eval ($_[1])
46             }
47              
48              
49             #
50             # find_next_oid()
51             # -------------
52             sub find_next_oid {
53 15     15 1 11213 my ($oid_list, $req_oid, $walk_base) = @_;
54              
55 15 100       239 croak "error: first argument must be an arrayref"
56             unless ref $oid_list eq "ARRAY";
57              
58 14   100     36 $req_oid ||= "";
59 14   100     55 $walk_base ||= "";
60              
61 14         16 my ($first_idx, $next_oid_idx);
62              
63 14         18 for my $i (0 .. $#{$oid_list}) {
  14         44  
64             # check if we are still within the given context, if given any
65 135 100 100     533 next if $walk_base and index($oid_list->[$i], $walk_base) != 0;
66              
67             # keep track of the first entry within the given context
68 69 100       143 $first_idx = $i if not defined $first_idx;
69              
70             # exact match of the requested entry
71 69 100       260 if ($oid_list->[$i] eq $req_oid) {
    100          
72 6         9 $next_oid_idx = $i + 1;
73             last
74 6         10 }
75             # prefix match of the requested entry
76             elsif (index($oid_list->[$i], $req_oid) == 0) {
77 5         7 $next_oid_idx = $i;
78             last
79 5         10 }
80             }
81              
82             # get the entry following the requested one
83 14 100 66     48 my $next_oid = (defined $next_oid_idx and $next_oid_idx <= $#{$oid_list})
84             ? $oid_list->[$next_oid_idx]
85             : "NONE";
86              
87             # check that the resulting OID is still within context
88 14 100       42 $next_oid = "NONE" if index($next_oid, $walk_base) != 0;
89              
90 14         51 return $next_oid
91             }
92              
93              
94             #
95             # oid_encode()
96             # ----------
97             sub oid_encode {
98 24     24 1 29007 return join ".", length($_[0]), unpack "c*", $_[0];
99             }
100              
101              
102             __PACKAGE__
103              
104             __END__