That's probably about the best you can do.
What you are trying to do is inherently non-portable, and
one of Perl's strongest features is to be as portable as possible. In your case, you shall have to run
perl -le 'print $^O'
on as many systems as possible, and work out how to detect the number of CPU's in a platform-dependent way. All of this could be bundled in up in a module, e.g. System::CPU (there's probably a better category that already exists on CPAN to tell the truth). The code could look like:
package System::CPU;
use strict;
sub nr_cpus {
$^O eq 'linux' and return nr_cpus_linux();
$^O eq 'solaris' and return nr_cpus_solaris();
$^O eq 'palm' and return 1;
1; # default if we don't know any better
}
sub nr_cpus_linux {
# your code here
}
sub nr_cpus_solaris {
require Solaris::Funky::Stuff;
Solaris::Funky::Stuff->import();
}
1;
You could then get the number of CPUs by saying
my $cpus = System::CPU::nr_cpus;
You will have to ask yourself the question of what to
do on a platform unknown to the script. In this particular case it probably makes sense to return 1, but in other contexts it may be wiser to die or croak (which could always be trapped by the caller in an eval.
update: changed the example Solaris code a bit
to show how you can include platform-dependent code at
run-time.
update: Thinking about your code over lunch I
recalled that at least as recently as the late 2.0.x linux
kernels, there was a problem with architecture-dependent /proc/cpuinfo files. You had to have two different ways of parsing the file depending on whether you were running on Intel or Alpha. Not sure if this is still true, and as I no longer have access to Alphas running Linux I cannot check this out. YMMV.
-- g r i n d e r
|