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

In reply to Re: Multiprocessor (SMP) system detection by grinder
in thread Multiprocessor (SMP) system detection by PetaMem

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.