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   34 use strict;
  4         9  
  4         117  
4 4     4   20 use warnings;
  4         8  
  4         116  
5              
6 4     4   37 use POSIX ();
  4         9  
  4         4079  
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 112     112 1 4967 my $class = shift;
36              
37 112         295 my $self = bless {}, $class;
38 112         386 $self->prepare_sysinfo;
39              
40 112         600 $self->{_host} = $self->get_hostname;
41 112         322 $self->{_os} = $self->get_os;
42 112         297 $self->{_cpu_type} = $self->get_cpu_type;
43 112         359 $self->{_cpu} = $self->get_cpu;
44 112         267 $self->{_ncpu} = $self->get_cpu_count;
45              
46 112         550 (my $bc = $class) =~ s/.*://;
47 112   66     342 $self->{_distro} = $self->get_dist_name || ($bc eq "Base" ? "" : $bc);
48              
49             $self->{_ncore} ||= $self->{_ncpu}
50 6         78 ? (sort { $b <=> $a } ($self->{_ncpu} =~ m/(\d+)/g))[0]
51 112 100 66     1417 : $self->{_ncpu};
52              
53 112         556 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 112     112 1 170 my $self = shift;
84 112         1203 my @uname = POSIX::uname();
85              
86 112         523 $self->{__hostname} = $uname[1];
87              
88 112         231 $self->{__osname} = $uname[0];
89 112         223 $self->{__osvers} = $uname[2];
90 112         424 my $os = join " - " => @uname[0,2];
91 112         1018 $os =~ s/(\S+)/\L$1/;
92 112         288 $self->{__os} = $os;
93              
94 112         232 $self->{__cpu_type} = $uname[4];
95 112         178 $self->{__cpu} = $uname[4];
96 112         219 $self->{__cpu_count} = "";
97              
98 112         377 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 112     112 1 194 my $self = shift;
109 112         382 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 112     112 1 237 my $self = shift;
120 112         570 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 197     197 1 340 my $self = shift;
131 197         964 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 112     112 1 184 my $self = shift;
142 112         428 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 112     112 1 258 my $self = shift;
153 112         397 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 112     112 1 167 my $self = shift;
177 112         570 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 197 my $self = shift;
196 62         298 my @args = map split () => @_;
197              
198 62         186 my @sw = qw( n s m c p );
199 62         246 my %sw = (
200             n => "host",
201             s => "os",
202             m => "cpu",
203             c => "ncpu",
204             p => "cpu_type",
205             );
206              
207 62         191 @args = grep exists $sw{$_} => @args;
208 62 100       151 @args or @args = ("a");
209 62 100       231 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         201 @args = grep exists $show{$_} => @sw;
214              
215 62         109 return join " ", map { my $m = $sw{$_}; $self->$m } @args;
  134         206  
  134         539  
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 166 my $self = shift;
226             return {
227 11         62 _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 1709     1709   6968 my $self = shift;
239              
240 1709         6610 (my $attrib = our $AUTOLOAD) =~ s/.*:://;
241 1709 50       5594 if (exists $self->{"_$attrib"}) {
242             ref $self->{"_$attrib"} eq "ARRAY" and
243 1709 100       4327 return @{ $self->{"_$attrib"} };
  628         180727  
244 1081         4655 return $self->{"_$attrib"};
245             }
246             }
247              
248             1;
249              
250             __END__