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

Is it possible to test for the OS version in Perl? Now, I'm sure I could test for the OS using the appropriate OS-specific command using "system", but what I want to do is determine if I am in an MS Windows environment or a *nix environment. Is that possilble? If so, how do you do it?

Thanks.

Replies are listed 'Best First'.
Re: Test for OS version in Perl
by FunkyMonk (Bishop) on Oct 20, 2007 at 18:32 UTC
    Have a look at $^O, but make sure you know about its limitations

    From perlvar:

    $OSNAME
    $^O

    The name of the operating system under which this copy of Perl was built, as determined during the configuration process. The value is identical to $Config{'osname'} . See also Config and the -V command-line switch documented in perlrun.

    In Windows platforms, $^O is not very helpful: since it is always MSWin32 , it doesn't tell the difference between 95/98/ME/NT/2000/XP/CE/.NET. Use Win32::GetOSName() or Win32::GetOSVersion() (see Win32 and perlport) to distinguish between the variants.

Re: Test for OS version in Perl
by jasonk (Parson) on Oct 20, 2007 at 18:29 UTC

    Check out perlvar.

    if ( $^O =~ /MSWin32/ ) { print "Windows\n"; } else { print "Not windows\n"; }

    We're not surrounded, we're in a target-rich environment!
      I think I found that "perlvar", and I saw that $^O, but I didn't think it would give me what I want. After thinking about it, I guess it would work in most cases, but not in my specific case.

      I have a PC that has Cygwin on it (and Perl comes with the Cygwin), and I also have ActivePerl on it. I brought up a Cygwin window, and wrote a little script like this:

      #!/usr/bin/perl
      #!C:/Perl/bin/perl
      print "^O\n";

      When I run it like that I get: Cygwin

      If switch the two comment lines (so #!C:/Perl... is the first line), I get: MSWin32

      What I really want to know is that if I am the running the script from a Cygwin window, regardless of which of those two builds of Perl runs the script, it will tell me I am running in a Cygwin environment. If I run the script from an XP CMD window, I want it to tell me I am running in a windows environment.

        #!/usr/bin/perl #!C:/Perl/bin/perl

        The Cygwin shell will look at the 'shebang' line to determine what interpreter to use to run your script. So it will either run the cygwin perl (/usr/bin/perl) or the Activestate one (C:/perl/bin/perl) depending on which you put first. However, if you run it from an XP CMD window, I don't think it pays any attention to the shebang line. It'll be run with whatever perl you use from the command line or whatever perl is associated with it in the registry.

        Put differently -- $^O tells you what the executing perl was compiled for, not where it was launched from.

        -xdg

        Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: Test for OS version in Perl
by goibhniu (Hermit) on Oct 21, 2007 at 12:37 UTC

    On my system, the environment variable OS is set to "Windows_NT" whether in a cmd.exe window or a Cygwin Bash shell. However, in the Cygwin Bash shell, there is the additional eVar OSTYPE which is set to "Cygwin"

    does that help?


    I humbly seek wisdom.
Re: Test for OS version in Perl
by perlfan (Parson) on Oct 21, 2007 at 16:20 UTC
      In particular the original poster will want to check for 'MicrosoftWindows' or 'Unix'. The former includes both MSWin32 and Cygwin, the latter covers the whole broad church of Unix in one line of code, which is far easier than checking whether $^O is linux, or freebsd, or netbsd, or solaris, or ...