File Coverage

lib/Rex/Inventory/SMBios/Section.pm
Criterion Covered Total %
statement 43 88 48.8
branch 3 12 25.0
condition 0 5 0.0
subroutine 11 19 57.8
pod 0 8 0.0
total 57 132 43.1


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             package Rex::Inventory::SMBios::Section;
6              
7 38     38   523 use v5.12.5;
  38         171  
8 38     38   244 use warnings;
  38         87  
  38         2184  
9              
10             our $VERSION = '1.14.2.3'; # TRIAL VERSION
11              
12             require Exporter;
13 38     38   276 use base qw(Exporter);
  38         127  
  38         3203  
14 38     38   282 use vars qw($SECTION @EXPORT);
  38         158  
  38         9073  
15              
16             @EXPORT = qw(section);
17             $SECTION = {};
18              
19             sub new {
20 0     0 0 0 my $that = shift;
21 0   0     0 my $proto = ref($that) || $that;
22 0         0 my $self = {@_};
23              
24 0         0 bless( $self, $proto );
25              
26 0         0 return $self;
27             }
28              
29             sub section {
30 228     228 0 992 my ( $class, $section ) = @_;
31 228         805 $SECTION->{$class} = $section;
32             }
33              
34 0           sub has {
35 228     228 0 536 my ( $class, $item, $is_array ) = @_;
36              
37 228 50       832 unless ( ref($item) eq "ARRAY" ) {
38 0         0 my $_tmp = $item;
39 0         0 $item = [$_tmp];
40             }
41              
42 38     38   408 no strict 'refs';
  38         118  
  38         8759  
43              
44 228         350 for my $_itm ( @{$item} ) {
  228         589  
45 1026         1555 my ( $itm, $from );
46              
47 1026 100       1997 if ( ref($_itm) eq "HASH" ) {
48 380         678 $itm = $_itm->{key};
49 380         576 $from = $_itm->{from};
50             }
51             else {
52 646         985 $itm = $_itm;
53 646         878 $from = $_itm;
54             }
55 1026         3605 $itm =~ s/[^a-zA-Z0-9_]+/_/g;
56 1026         5778 *{"${class}::get_\L$itm"} = sub {
57 0     0   0 my $self = shift;
58 0         0 return $self->get( $from, $is_array );
59 1026         3278 };
60              
61 1026         1669 push( @{"${class}::items"}, "\L$itm" );
  1026         3747  
62             }
63              
64 38     38   306 use strict;
  38         87  
  38         6105  
65             }
66              
67             sub dmi {
68              
69 0     0 0   my ($self) = @_;
70 0           return $self->{"dmi"};
71              
72             }
73              
74             sub get {
75              
76 0     0 0   my ( $self, $key, $is_array ) = @_;
77 0           return $self->_search_for( $key, $is_array );
78              
79             }
80              
81             sub get_all {
82              
83 0     0 0   my ($self) = @_;
84              
85 38     38   282 use Data::Dumper;
  38         97  
  38         2434  
86 0           my $r = ref($self);
87              
88 38     38   244 no strict 'refs';
  38         102  
  38         2523  
89 0           my @items = @{"${r}::items"};
  0            
90 38     38   264 use strict;
  38         108  
  38         14868  
91              
92 0           my $ret = {};
93 0           for my $itm (@items) {
94 0           my $f = "get_$itm";
95 0           $ret->{$itm} = $self->$f();
96             }
97              
98 0           return $ret;
99              
100             }
101              
102             sub dump {
103              
104 0     0 0   my ($self) = @_;
105              
106 0           require Data::Dumper;
107             print Data::Dumper::Dumper(
108 0           $self->dmi->get_tree( $SECTION->{ ref($self) } ) );
109              
110             }
111              
112             sub _search_for {
113 0     0     my ( $self, $key, $is_array ) = @_;
114              
115 0 0         unless ( $self->dmi->get_tree( $SECTION->{ ref($self) } ) ) {
116              
117             #die $SECTION->{ref($self)} . " not supported";
118 0           return;
119             }
120              
121 0           my $idx = 0;
122 0           for my $entry ( @{ $self->dmi->get_tree( $SECTION->{ ref($self) } ) } ) {
  0            
123 0           my ($_key) = keys %{$entry};
  0            
124 0 0         if ($is_array) {
125 0 0         if ( $idx != $self->get_index() ) {
126 0           ++$idx;
127 0           next;
128             }
129             }
130              
131 0 0         if ( exists $entry->{$key} ) {
132 0           return $entry->{$key};
133             }
134             else {
135 0           return "";
136             }
137 0           ++$idx;
138             }
139              
140 0           return "";
141             }
142              
143             sub get_index {
144              
145 0     0 0   my ($self) = @_;
146 0   0       return $self->{"index"} || 0;
147              
148             }
149              
150             1;