File Coverage

blib/lib/System/Info/Base.pm
Criterion Covered Total %
statement 66 68 97.0
branch 9 10 90.0
condition 4 6 66.6
subroutine 14 16 87.5
pod 11 11 100.0
total 104 111 93.6


line stmt bran cond sub pod time code
1             package System::Info::Base;
2              
3 4     4   42 use strict;
  4         8  
  4         108  
4 4     4   20 use warnings;
  4         6  
  4         93  
5              
6 4     4   18 use POSIX ();
  4         7  
  4         3966  
7              
8             our $VERSION = "0.050";
9              
10             =head1 NAME
11              
12             System::Info::Base - Baseclass for system information.
13              
14             =head1 ATTRIBUTES
15              
16             =head2 cpu
17              
18             =head2 cpu_type
19              
20             =head2 ncpu
21              
22             =head2 os
23              
24             =head2 host
25              
26             =head1 DESCRIPTION
27              
28             =head2 System::Info::Base->new()
29              
30             Return a new instance for $^O
31              
32             =cut
33              
34             sub new {
35 119     119 1 5843 my $class = shift;
36              
37 119         281 my $self = bless {}, $class;
38 119         379 $self->prepare_sysinfo;
39              
40 119         505 $self->{_host} = $self->get_hostname;
41 119         353 $self->{_os} = $self->get_os;
42 119         291 $self->{_cpu_type} = $self->get_cpu_type;
43 119         433 $self->{_cpu} = $self->get_cpu;
44 119         315 $self->{_ncpu} = $self->get_cpu_count;
45              
46 119         603 (my $bc = $class) =~ s/.*://;
47 119   66     431 $self->{_distro} = $self->get_dist_name || ($bc eq "Base" ? "" : $bc);
48              
49             $self->{_ncore} ||= $self->{_ncpu}
50 6         82 ? (sort { $b <=> $a } ($self->{_ncpu} =~ m/(\d+)/g))[0]
51 119 100 66     1343 : $self->{_ncpu};
52              
53 119         496 return $self;
54             } # new
55              
56             =head2 $si->prepare_sysinfo
57              
58             This method should be overridden by platform specific subclasses.
59              
60             The generic information is taken from C<< POSIX::uname() >>.
61              
62             =over
63              
64             =item $self->_hostname => (POSIX::uname)[1]
65              
66             =item $self->_os => join " - " => (POSIX::uname)[0,2]
67              
68             =item $self->_osname => (POSIX::uname)[0]
69              
70             =item $self->_osvers => (POSIX::uname)[2]
71              
72             =item $self->_cpu_type => (POSIX::uname)[4]
73              
74             =item $self->_cpu => (POSIX::uname)[4]
75              
76             =item $self->_cpu_count => ""
77              
78             =back
79              
80             =cut
81              
82             sub prepare_sysinfo {
83 119     119 1 189 my $self = shift;
84 119         1318 my @uname = POSIX::uname();
85              
86 119         493 $self->{__hostname} = $uname[1];
87              
88 119         239 $self->{__osname} = $uname[0];
89 119         216 $self->{__osvers} = $uname[2];
90 119         438 my $os = join " - " => @uname[0,2];
91 119         1009 $os =~ s/(\S+)/\L$1/;
92 119         322 $self->{__os} = $os;
93              
94 119         206 $self->{__cpu_type} = $uname[4];
95 119         184 $self->{__cpu} = $uname[4];
96 119         186 $self->{__cpu_count} = "";
97              
98 119         379 return $self;
99             } # prepare_sysinfo
100              
101             =head2 $si->get_os
102              
103             Returns $self->_os
104              
105             =cut
106              
107             sub get_os {
108 119     119 1 228 my $self = shift;
109 119         446 return $self->_os;
110             } # get_os
111              
112             =head2 $si->get_hostname
113              
114             Returns $self->_hostname
115              
116             =cut
117              
118             sub get_hostname {
119 119     119 1 227 my $self = shift;
120 119         676 return $self->_hostname;
121             } # get_hostname
122              
123             =head2 $si->get_cpu_type
124              
125             Returns $self->_cpu_type
126              
127             =cut
128              
129             sub get_cpu_type {
130 211     211 1 374 my $self = shift;
131 211         995 return $self->_cpu_type;
132             } # get_cpu_type
133              
134             =head2 $si->get_cpu
135              
136             Returns $self->_cpu
137              
138             =cut
139              
140             sub get_cpu {
141 119     119 1 204 my $self = shift;
142 119         378 return $self->_cpu;
143             } # get_cpu
144              
145             =head2 $si->get_cpu_count
146              
147             Returns $self->_cpu_count
148              
149             =cut
150              
151             sub get_cpu_count {
152 119     119 1 231 my $self = shift;
153 119         403 return $self->_cpu_count;
154             } # get_cpu_count
155              
156             =head2 $si->get_core_count
157              
158             Returns $self->get_cpu_count as a number
159              
160             If C returns C<2 [8 cores]>, C returns C<8>
161              
162             =cut
163              
164             sub get_core_count {
165 0     0 1 0 my $self = shift;
166 0         0 return $self->{_ncore};
167             } # get_core_count
168              
169             =head2 $si->get_dist_name
170              
171             Returns the name of the distribution.
172              
173             =cut
174              
175             sub get_dist_name {
176 119     119 1 181 my $self = shift;
177 119         551 return $self->{__distro};
178             } # get_dist_name
179              
180             =head2 si_uname (@args)
181              
182             This class gathers most of the C info, make a comparable
183             version. Takes almost the same arguments:
184              
185             a for all (can be omitted)
186             n for nodename
187             s for os name and version
188             m for cpu name
189             c for cpu count
190             p for cpu_type
191              
192             =cut
193              
194             sub si_uname {
195 62     62 1 198 my $self = shift;
196 62         278 my @args = map split () => @_;
197              
198 62         187 my @sw = qw( n s m c p );
199 62         256 my %sw = (
200             n => "host",
201             s => "os",
202             m => "cpu",
203             c => "ncpu",
204             p => "cpu_type",
205             );
206              
207 62         201 @args = grep exists $sw{$_} => @args;
208 62 100       153 @args or @args = ("a");
209 62 100       230 grep m/a/ => @args and @args = @sw;
210              
211             # filter supported args but keep order of @sw!
212 62         261 my %show = map +( $_ => undef ) => grep exists $sw{$_} => @args;
213 62         202 @args = grep exists $show{$_} => @sw;
214              
215 62         101 return join " ", map { my $m = $sw{$_}; $self->$m } @args;
  134         199  
  134         507  
216             } # si_uname
217              
218             =head2 $si->old_dump
219              
220             Just a backward compatible way to dump the object (for test suite).
221              
222             =cut
223              
224             sub old_dump {
225 11     11 1 131 my $self = shift;
226             return {
227 11         96 _cpu => $self->cpu,
228             _cpu_type => $self->cpu_type,
229             _ncpu => $self->ncpu,
230             _os => $self->os,
231             _host => $self->host,
232             };
233             }
234              
235       0     sub DESTROY { }
236              
237             sub AUTOLOAD {
238 1807     1807   6539 my $self = shift;
239              
240 1807         7120 (my $attrib = our $AUTOLOAD) =~ s/.*:://;
241 1807 50       5658 if (exists $self->{"_$attrib"}) {
242             ref $self->{"_$attrib"} eq "ARRAY" and
243 1807 100       4560 return @{ $self->{"_$attrib"} };
  670         193264  
244 1137         4779 return $self->{"_$attrib"};
245             }
246             }
247              
248             1;
249              
250             __END__