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   30 use strict;
  4         8  
  4         162  
4 4     4   86 use warnings;
  4         18  
  4         270  
5              
6 4     4   2408 use POSIX ();
  4         40568  
  4         4702  
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             =over 2
17              
18             =item cpu
19              
20             =item cpu_type
21              
22             =item ncpu
23              
24             =item os
25              
26             =item host
27              
28             =back
29              
30             =head1 DESCRIPTION
31              
32             =head2 System::Info::Base->new()
33              
34             Return a new instance for $^O
35              
36             =cut
37              
38             sub new {
39 140     140 1 296224 my $class = shift;
40              
41 140         442 my $self = bless {}, $class;
42 140         1446 $self->prepare_sysinfo;
43              
44 140         848 $self->{_host} = $self->get_hostname;
45 140         640 $self->{_os} = $self->get_os;
46 140         575 $self->{_cpu_type} = $self->get_cpu_type;
47 140         641 $self->{_cpu} = $self->get_cpu;
48 140         485 $self->{_ncpu} = $self->get_cpu_count;
49              
50 140         922 (my $bc = $class) =~ s/.*://;
51 140   66     538 $self->{_distro} = $self->get_dist_name || ($bc eq "Base" ? "" : $bc);
52              
53             $self->{_ncore} ||= $self->{_ncpu}
54 11         96 ? (sort { $b <=> $a } ($self->{_ncpu} =~ m/(\d+)/g))[0]
55 140 100 66     2154 : $self->{_ncpu};
56              
57 140         892 return $self;
58             } # new
59              
60             =head2 $si->prepare_sysinfo
61              
62             This method should be overridden by platform specific subclasses.
63              
64             The generic information is taken from C<< POSIX::uname() >>.
65              
66             =over
67              
68             =item $self->_hostname => (POSIX::uname)[1]
69              
70             =item $self->_os => join " - " => (POSIX::uname)[0,2]
71              
72             =item $self->_osname => (POSIX::uname)[0]
73              
74             =item $self->_osvers => (POSIX::uname)[2]
75              
76             =item $self->_cpu_type => (POSIX::uname)[4]
77              
78             =item $self->_cpu => (POSIX::uname)[4]
79              
80             =item $self->_cpu_count => ""
81              
82             =back
83              
84             =cut
85              
86             sub prepare_sysinfo {
87 140     140 1 307 my $self = shift;
88 140         1702 my @uname = POSIX::uname();
89              
90 140         653 $self->{__hostname} = $uname[1];
91              
92 140         461 $self->{__osname} = $uname[0];
93 140         431 $self->{__osvers} = $uname[2];
94 140         766 my $os = join " - " => @uname[0,2];
95 140         1678 $os =~ s/(\S+)/\L$1/;
96 140         506 $self->{__os} = $os;
97              
98 140         319 $self->{__cpu_type} = $uname[4];
99 140         325 $self->{__cpu} = $uname[4];
100 140         428 $self->{__cpu_count} = "";
101              
102 140         598 return $self;
103             } # prepare_sysinfo
104              
105             =head2 $si->get_os
106              
107             Returns $self->_os
108              
109             =cut
110              
111             sub get_os {
112 140     140 1 284 my $self = shift;
113 140         589 return $self->_os;
114             } # get_os
115              
116             =head2 $si->get_hostname
117              
118             Returns $self->_hostname
119              
120             =cut
121              
122             sub get_hostname {
123 140     140 1 258 my $self = shift;
124 140         1106 return $self->_hostname;
125             } # get_hostname
126              
127             =head2 $si->get_cpu_type
128              
129             Returns $self->_cpu_type
130              
131             =cut
132              
133             sub get_cpu_type {
134 252     252 1 546 my $self = shift;
135 252         1650 return $self->_cpu_type;
136             } # get_cpu_type
137              
138             =head2 $si->get_cpu
139              
140             Returns $self->_cpu
141              
142             =cut
143              
144             sub get_cpu {
145 140     140 1 316 my $self = shift;
146 140         629 return $self->_cpu;
147             } # get_cpu
148              
149             =head2 $si->get_cpu_count
150              
151             Returns $self->_cpu_count
152              
153             =cut
154              
155             sub get_cpu_count {
156 140     140 1 243 my $self = shift;
157 140         605 return $self->_cpu_count;
158             } # get_cpu_count
159              
160             =head2 $si->get_core_count
161              
162             Returns $self->get_cpu_count as a number
163              
164             If C returns C<2 [8 cores]>, C returns C<8>
165              
166             =cut
167              
168             sub get_core_count {
169 0     0 1 0 my $self = shift;
170 0         0 return $self->{_ncore};
171             } # get_core_count
172              
173             =head2 $si->get_dist_name
174              
175             Returns the name of the distribution.
176              
177             =cut
178              
179             sub get_dist_name {
180 140     140 1 286 my $self = shift;
181 140         734 return $self->{__distro};
182             } # get_dist_name
183              
184             =head2 si_uname (@args)
185              
186             This class gathers most of the C info, make a comparable
187             version. Takes almost the same arguments:
188              
189             a for all (can be omitted)
190             n for nodename
191             s for os name and version
192             m for cpu name
193             c for cpu count
194             p for cpu_type
195              
196             =cut
197              
198             sub si_uname {
199 63     63 1 222 my $self = shift;
200 63         358 my @args = map split () => @_;
201              
202 63         241 my @sw = qw( n s m c p );
203 63         467 my %sw = (
204             n => "host",
205             s => "os",
206             m => "cpu",
207             c => "ncpu",
208             p => "cpu_type",
209             );
210              
211 63         282 @args = grep exists $sw{$_} => @args;
212 63 100       195 @args or @args = ("a");
213 63 100       299 grep m/a/ => @args and @args = @sw;
214              
215             # filter supported args but keep order of @sw!
216 63         451 my %show = map +( $_ => undef ) => grep exists $sw{$_} => @args;
217 63         351 @args = grep exists $show{$_} => @sw;
218              
219 63         125 return join " ", map { my $m = $sw{$_}; $self->$m } @args;
  137         250  
  137         697  
220             } # si_uname
221              
222             =head2 $si->old_dump
223              
224             Just a backward compatible way to dump the object (for test suite).
225              
226             =cut
227              
228             sub old_dump {
229 11     11 1 67 my $self = shift;
230             return {
231 11         73 _cpu => $self->cpu,
232             _cpu_type => $self->cpu_type,
233             _ncpu => $self->ncpu,
234             _os => $self->os,
235             _host => $self->host,
236             };
237             }
238              
239       0     sub DESTROY { }
240              
241             sub AUTOLOAD {
242 2095     2095   8659 my $self = shift;
243              
244 2095         10183 (my $attrib = our $AUTOLOAD) =~ s/.*:://;
245 2095 50       8104 if (exists $self->{"_$attrib"}) {
246             ref $self->{"_$attrib"} eq "ARRAY" and
247 2095 100       6594 return @{ $self->{"_$attrib"} };
  790         363409  
248 1305         7606 return $self->{"_$attrib"};
249             }
250             }
251              
252             1;
253              
254             __END__