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   36 use strict;
  4         9  
  4         112  
4 4     4   18 use warnings;
  4         8  
  4         92  
5              
6 4     4   18 use POSIX ();
  4         8  
  4         3861  
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 123     123 1 4075 my $class = shift;
36              
37 123         314 my $self = bless {}, $class;
38 123         414 $self->prepare_sysinfo;
39              
40 123         514 $self->{_host} = $self->get_hostname;
41 123         401 $self->{_os} = $self->get_os;
42 123         311 $self->{_cpu_type} = $self->get_cpu_type;
43 123         339 $self->{_cpu} = $self->get_cpu;
44 123         299 $self->{_ncpu} = $self->get_cpu_count;
45              
46 123         628 (my $bc = $class) =~ s/.*://;
47 123   66     402 $self->{_distro} = $self->get_dist_name || ($bc eq "Base" ? "" : $bc);
48              
49             $self->{_ncore} ||= $self->{_ncpu}
50 6         77 ? (sort { $b <=> $a } ($self->{_ncpu} =~ m/(\d+)/g))[0]
51 123 100 66     1485 : $self->{_ncpu};
52              
53 123         497 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 123     123 1 198 my $self = shift;
84 123         1297 my @uname = POSIX::uname();
85              
86 123         496 $self->{__hostname} = $uname[1];
87              
88 123         249 $self->{__osname} = $uname[0];
89 123         213 $self->{__osvers} = $uname[2];
90 123         437 my $os = join " - " => @uname[0,2];
91 123         1073 $os =~ s/(\S+)/\L$1/;
92 123         306 $self->{__os} = $os;
93              
94 123         213 $self->{__cpu_type} = $uname[4];
95 123         196 $self->{__cpu} = $uname[4];
96 123         219 $self->{__cpu_count} = "";
97              
98 123         369 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 123     123 1 192 my $self = shift;
109 123         410 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 123     123 1 253 my $self = shift;
120 123         686 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 219     219 1 427 my $self = shift;
131 219         1400 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 123     123 1 194 my $self = shift;
142 123         397 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 123     123 1 177 my $self = shift;
153 123         381 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 123     123 1 179 my $self = shift;
177 123         608 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 211 my $self = shift;
196 62         287 my @args = map split () => @_;
197              
198 62         186 my @sw = qw( n s m c p );
199 62         250 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       166 @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         247 my %show = map +( $_ => undef ) => grep exists $sw{$_} => @args;
213 62         196 @args = grep exists $show{$_} => @sw;
214              
215 62         113 return join " ", map { my $m = $sw{$_}; $self->$m } @args;
  134         217  
  134         526  
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 75 my $self = shift;
226             return {
227 11         55 _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 1863     1863   6961 my $self = shift;
239              
240 1863         7283 (my $attrib = our $AUTOLOAD) =~ s/.*:://;
241 1863 50       5950 if (exists $self->{"_$attrib"}) {
242             ref $self->{"_$attrib"} eq "ARRAY" and
243 1863 100       4573 return @{ $self->{"_$attrib"} };
  694         198801  
244 1169         4895 return $self->{"_$attrib"};
245             }
246             }
247              
248             1;
249              
250             __END__