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

In my script I would like to detect whether it's running on Windows or not. I guess best method is to check $^O against predefined set of values (am I right?). I know ActiveState perl uses $^O = 'MSWin32'; What other possible values it can have? On 64-bit windows? On Cygwin Perl? On MS's port of perl to SFU? Thanks!
  • Comment on how to detect whether script is being run on Windows at runtime?

Replies are listed 'Best First'.
Re: how to detect whether script is being run on Windows at runtime?
by Fletch (Bishop) on May 10, 2004 at 18:32 UTC

    perldoc perlport lists what $^O will be on different platforms.

      $^O

      $^O is determined when perl is compiled. It's the platform perl was compiled on, and that isn't necessarily the platform it runs on.

      Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

        True, but perlport also gives a snippet using Win32::GetOSVersion() to distinguish between different flavours thereof.

Re: how to detect whether script is being run on Windows at runtime?
by jacques (Priest) on May 10, 2004 at 18:45 UTC
    In my script I would like to detect whether it's running on Windows or not.

    Sys::Hostname

      I think all Sys::Hostname does is tell you the hostname of the machine. Doesn't tell you what OS the machine is running.

      -- vek --
        Whoops! My mistake. Try this from usenet:

        use Config; $ostype = $Config{"osname"};
Re: how to detect whether script is being run on Windows at runtime?
by cLive ;-) (Prior) on May 10, 2004 at 21:41 UTC
    lazy hack?
    if (-d "C:/windows/system") { # is windows }
    Not tested - I'm not on windows - but would that cover ambiguity on $^O?

    cLive ;-)

      Except of course for every operating system based on winnt, which uses c:/winnt/
        Could you just check for the existence of "C:\"? How many OS's start with a letter, specifically the thrid letter in the alphabet (this is a real question)?

        Cheers,

        John

        Update:

        Apparrently you get '--'ed around here for asking what can be preceived as stupid questions. From this and BUU's comments I'll take the answer to my first question as no.

        Update 2:

        I was talking about the second question at work today with a friend. He said that OS/2 and one of the mainframe operating systems also used letters to denote drives.