File Coverage

blib/lib/System/Info.pm
Criterion Covered Total %
statement 65 67 97.0
branch 14 26 53.8
condition 1 3 33.3
subroutine 18 19 94.7
pod 4 4 100.0
total 102 119 85.7


line stmt bran cond sub pod time code
1             package System::Info;
2              
3 2     2   298211 use strict;
  2         5  
  2         120  
4 2     2   23 use warnings;
  2         3  
  2         194  
5              
6             our $VERSION = "0.067";
7              
8 2     2   16 use base "Exporter";
  2         3  
  2         421  
9             our @EXPORT_OK = qw( &sysinfo &sysinfo_hash &si_uname );
10              
11 2     2   1198 use System::Info::AIX;
  2         8  
  2         118  
12 2     2   1184 use System::Info::BSD;
  2         8  
  2         105  
13 2     2   1155 use System::Info::Cygwin;
  2         9  
  2         95  
14 2     2   1286 use System::Info::Darwin;
  2         8  
  2         131  
15 2     2   1104 use System::Info::Generic;
  2         8  
  2         74  
16 2     2   1058 use System::Info::Haiku;
  2         8  
  2         77  
17 2     2   1215 use System::Info::HPUX;
  2         9  
  2         90  
18 2     2   1169 use System::Info::Irix;
  2         8  
  2         117  
19 2     2   17 use System::Info::Linux;
  2         3  
  2         71  
20 2     2   1203 use System::Info::Solaris;
  2         9  
  2         86  
21 2     2   1208 use System::Info::VMS;
  2         8  
  2         82  
22 2     2   1182 use System::Info::Windows;
  2         8  
  2         1857  
23              
24             =head1 NAME
25              
26             System::Info - Factory for system specific information objects
27              
28             =head1 SYNOPSIS
29              
30             use System::Info;
31              
32             my $si = System::Info->new;
33              
34             printf "Hostname: %s\n", $si->host;
35             printf "Number of CPU's: %s\n", $si->ncpu;
36             printf "Processor type: %s\n", $si->cpu_type; # short
37             printf "Processor description: %s\n", $si->cpu; # long
38             printf "OS and version: %s\n", $si->os;
39              
40             or
41              
42             use System::Info qw( sysinfo );
43             printf "[%s]\n", sysinfo ();
44              
45             or
46              
47             $ perl -MSystem::Info=si_uname -le print+si_uname
48              
49             =head1 DESCRIPTION
50              
51             System::Info tries to present system-related information, like number of CPU's,
52             architecture, OS and release related information in a system-independent way.
53             This releases the user of this module of the need to know if the information
54             comes from Windows, Linux, HP-UX, AIX, Solaris, Irix, or VMS, and if the
55             architecture is i386, x64, pa-risc2, or arm.
56              
57             =head1 METHODS
58              
59             =head2 System::Info->new
60              
61             Factory method, with fallback to the information in C<< POSIX::uname () >>.
62              
63             =cut
64              
65             sub new {
66 124     124 1 574457 my $factory = shift;
67              
68 124 50       984 $^O =~ m/aix/i and return System::Info::AIX->new;
69 124 50       880 $^O =~ m/bsd|dragonfly/i and return System::Info::BSD->new;
70 124 50       451 $^O =~ m/cygwin/i and return System::Info::Cygwin->new;
71 124 50       473 $^O =~ m/darwin/i and return System::Info::Darwin->new;
72 124 50       566 $^O =~ m/haiku/ and return System::Info::Haiku->new;
73 124 50       408 $^O =~ m/hp-?ux/i and return System::Info::HPUX->new;
74 124 50       476 $^O =~ m/irix/i and return System::Info::Irix->new;
75 124 100       1865 $^O =~ m/linux|android/i and return System::Info::Linux->new;
76 1 50       7 $^O =~ m/solaris|sunos|osf/i and return System::Info::Solaris->new;
77 1 50       5 $^O =~ m/VMS/ and return System::Info::VMS->new;
78 1 50       5 $^O =~ m/mswin32|windows/i and return System::Info::Windows->new;
79              
80 1         19 return System::Info::Generic->new;
81             }
82              
83             =head2 sysinfo
84              
85             C returns a string with C, C and C.
86              
87             =cut
88              
89             sub sysinfo {
90 1     1 1 497 my $si = System::Info->new;
91 1 50       24 my @fields = $_[0]
92             ? qw( host os cpu ncpu cpu_type )
93             : qw( host os cpu_type );
94 1         7 return join " ", @{ $si }{ map "_$_" => @fields };
  1         97  
95             } # sysinfo
96              
97             =head2 sysinfo_hash
98              
99             C returns a hash reference with basic system information, like:
100              
101             { cpu => 'Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz (GenuineIntel 2700MHz)',
102             cpu_count => '1 [8 cores]',
103             cpu_cores => 8,
104             cpu_type => 'x86_64',
105             distro => 'openSUSE Tumbleweed 20171030',
106             hostname => 'foobar',
107             os => 'linux - 4.13.10-1-default [openSUSE Tumbleweed 20171030]',
108             osname => 'Linux',
109             osvers => '4.13.10-1-default'
110             }
111              
112             The C count refers to logical cores. On MacOS there is also a
113             C count, which will be the same as C for Apple Silicon,
114             but not for an Intel Mac with SMT enabled:
115              
116             { cpu => 'Quad-Core Intel Core i7 (2 GHz)',
117             cpu_count => '1 [4 cores]',
118             cpu_cores => '8',
119             physical_cores => '4',
120             cpu_type => 'x86_64',
121             distro => 'Darwin 11.7.10',
122             hostname => '192.168.1.6',
123             os => 'darwin - 20.6.0 (Mac OS X - macOS 11.7.10 (20G1427))',
124             osname => 'Darwin',
125             osvers => '11.7.10'
126             }
127              
128             =cut
129              
130             sub sysinfo_hash {
131 1     1 1 19 my $si = System::Info->new;
132 1 50       12 my %phys = $si->{_phys_core} ? (physical_cores => $si->{_phys_core}) : ();
133             return {
134             %phys,
135             hostname => $si->{_host},
136             cpu => $si->{_cpu},
137             cpu_type => $si->{_cpu_type},
138             cpu_count => $si->{_ncpu},
139             cpu_cores => $si->{_ncore},
140             os => $si->{_os},
141             osname => $si->{__osname},
142             osvers => $si->{__osvers},
143             distro => $si->{__distro}
144             || join " " => $si->{__osname}, $si->{__osvers},
145 1   33     137 };
146             } # sysinfo
147              
148             =head2 si_uname (@args)
149              
150             This class gathers most of the C info, make a comparable
151             version. Takes almost the same arguments:
152              
153             a for all (can be omitted)
154             n for nodename
155             s for os name and version
156             m for cpu name
157             c for cpu count
158             p for cpu_type
159              
160             =cut
161              
162             sub si_uname {
163 0     0 1   my $si = System::Info->new;
164 0           return $si->si_uname (@_);
165             }
166              
167             1;
168              
169             __END__