biochris has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, Anyway I can check the OS in my script, if it's Windows or Linux so I can run the appropriate commands? i.e. If (Windows){run some code} elsif (Linux){run some other code specific to Linux} else {I don't recognize this OS} etc. Help from the Monks really appreciated!

Edited by Arunbear: Changed title from 'Windows vs. Linux', as per Monastery guidelines

  • Comment on How can my code discover the operating system name?

Replies are listed 'Best First'.
Re: How can my code discover the operating system name?
by Transient (Hermit) on May 06, 2005 at 19:02 UTC
Re: How can my code discover the operating system name?
by Tanktalus (Canon) on May 06, 2005 at 23:21 UTC

    This may not be completely relevant to your question, but it's related. In most cases, the difference between Windows and Windows is immaterial. Or Linux and Linux. But sometimes it matters.

    If you're trying to figure out the difference between Windows ME, Windows 2003, and Windows XP, you have to first use $^O to find out you're on Windows, then load the appropriate Win32 module and find out the version of the OS. Or, if you're trying to figure out the difference between Windows/x86, Windows/x86-64 (AMD64 or EM64T), and Windows/ia64, you need to check %ENV. You're looking for either PROCESSOR_ARCHITEW6432 or PROCESSOR_ARCHITECTURE - which one depends on the machine. I actually use my $proc = $ENV{PROCESSOR_ARCHITEW6432} || $ENV{PROCESSOR_ARCHITECTURE};.

    If you're trying to figure out the difference between Linux/x86, Linux/x86-64, Linux/ia64, Linux/ppc, Linux/390, Linux/390x, etc., you need to check the uname - $Config::Config{'archname'} is often good enough.

    Again, it usually doesn't matter. It does for me, though, so I've got a routine to explicitly figure out precisely which platform I'm on, and then I load a data file that describes the platform ("unix" vs "win"; "linux" vs "aix" vs "sun" vs "hp" vs "dynix" vs "sco" vs...; "SystemV"; etc.) and then I deal with that data object to find out whether or not to do certain things, and, if I want to do something, how to do it.

      I've got a routine to explicitly figure out precisely which platform I'm on

      Maybe something for the Code Snippets or perhaps even a full-blown module on CPAN?

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law