mp3car-2001 has asked for the wisdom of the Perl Monks concerning the following question:

I have a little backup script, and I'd like to be able to use it for either Windows 2000 or Linux, since I develop in both. I'd like to use the same script on both OS, but obviously under windows I start my backup in D:\devel and under linux it starts in /home/joe/devel How can my perl script figure out what OS its run under so it starts in the right place?

Thanks,
    Joe

  • Comment on How do I tell what OS I'm running under?

Replies are listed 'Best First'.
(Ovid) Re: How do I tell what OS I'm running under?
by Ovid (Cardinal) on Jan 14, 2001 at 00:13 UTC
Re: How do I tell what OS I'm running under?
by Fastolfe (Vicar) on Jan 14, 2001 at 00:13 UTC
    See perldoc perlvar and note the $^O variable.
Re: How do I tell what OS I'm running under?
by Adam (Vicar) on Jan 14, 2001 at 05:55 UTC
    The quick answer, which you have recieved, is that $^O contains the "name" of the OS. This is actually imprecise. Particularly, the value of $^O is compiled in, but $^O = MSWin32 on all of: Win95, 98, me, NT, and 2k. That isn't very helpful. Worse, $^O isn't write protected, meaning that someone could stick "MSWin32" in there when you were expecting "bsdos" or "solaris".

    Of course, for the most part $^O is sufficient.

Re: How do I tell what OS I'm running under?
by turnstep (Parson) on Jan 14, 2001 at 20:23 UTC

    As someone pointed out, the $^O variable may have been set wrong. Another way to do it would be to check for the existence of certain files - it looks like from your example that you already know of two directories that will be there. You could simply test for one or the other's existence, as long as you had a guarantee that it existed on one OS and did not exist on the other. A quick and dirty fix, of course, but AWTDI. You may even want to test for the location of perl.exe - very unlikely to be in the same place on Windows and Linux. :)

      To go a bit further what turnstep explained, you may check the value of <kbd>$^X</kbd>, which is the path and name of your perl executable. If it ends in '.exe' it's likely you're running under Windows.

      <kbd>--
      PerlMonger::Paris(http => 'paris.pm.org');</kbd>
Re: How do I tell what OS I'm running under?
by mp3car-2001 (Scribe) on Jan 14, 2001 at 00:16 UTC
    Thanks a lot guys. I knew it was something dumb like that, but I searched the site and index of a couple books and couldn't find it. This site rules!!!