How can I detect the operating system in Perl?

I have Perl on Mac, Windows and Ubuntu. How can I tell from within the script which one is which? Thanks in advance.

Edit: I was asked what I am doing. It is a script, part of our cross-platform build system. The script recurses directories and figures out what files to build. Some files are platform-specific, and thus, on Linux I don't want to build the files ending with _win.cpp, etc.

Examine the $^O variable which will contain the name of the operating system:

print "$^O\n";

Which prints linux on Linux and MSWin32 on Windows.

You can also refer to this variable by the name $OSNAME if you use the English module:

use English qw' -no_match_vars ';
print "$OSNAME\n";

According to perlport, $^O will be darwin on Mac OS X.


You can also use the Config core module, which can provide the same information (and a lot more):

use Config;

print "$Config{osname}\n";
print "$Config{archname}\n";

Which on my Ubuntu machine prints:

linux
i486-linux-gnu-thread-multi

Note that this information is based on the system that Perl was built, which is not necessarily the system Perl is currently running on (the same is true for $^O and $OSNAME); the OS won't likely be different but some information, like the architecture name, may very well be.

There are many special, read-only Perl variables accessible from anywhere in any Perl script. One of them tells you the operating system the script is running in,​  Here's a quick reference on how to find the OS the local machine is running from Perl. The $^O variable ($OSTYPE if you use English) contains the operating system that your perl binary was built for.

If you need more specific information on Windows this may help.

my $osname = $^O;


if( $osname eq 'MSWin32' ){{
  eval { require Win32; } or last;
  $osname = Win32::GetOSName();

  # work around for historical reasons
  $osname = 'WinXP' if $osname =~ /^WinXP/;
}}

Derived from sysinfo.t, which I wrote the original version.

If you need more detailed information:

my ( $osvername, $major, $minor, $id ) = Win32::GetOSVersion();

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  How do I tell which operating system my Perl program is running in? Solution: Use a special variable. There are many special, read-only Perl variables accessible from anywhere in any Perl script. One of them tells you the operating system the script is running in, as the following code demonstrates:

Sys::Info::OS looks like a relatively clean potential solution, but currently doesn't seem to support Mac. It shouldn't be too much work to add that though.

Look inside the source for File::Spec to see how it loads the right delegate based on the operating system. :) File::Spec has a separate Perl module file for each  Just a very simple command to determine the operating system in which the script is running #!/usr/bin/perl print "The OS in which the script is runnin is:- ";

Look inside the source for File::Spec to see how it loads the right delegate based on the operating system. :)

File::Spec has a separate Perl module file for each OS. File::Spec::Win32, File::Spec::OS2, etc...

It checks the operating system and will load the appropriate .pm file at runtime based on OS.

# From the source code of File::Spec
my %module = (
      MSWin32 => 'Win32',
      os2     => 'OS2',
      VMS     => 'VMS',
      NetWare => 'Win32', # Yes, File::Spec::Win32 works on NetWare.
      symbian => 'Win32', # Yes, File::Spec::Win32 works on symbian.
      dos     => 'OS2',   # Yes, File::Spec::OS2 works on DJGPP.
      cygwin  => 'Cygwin',
      amigaos => 'AmigaOS');


my $module = $module{$^O} || 'Unix';

require "File/Spec/$module.pm";
our @ISA = ("File::Spec::$module");

Perl is built with a $^O variable that indicates the operating system it was built on. OpenVMS, VMS, To determine the architecture that you are running on  This variable contains the operating system name (e.g. sunos, solaris, hpux, etc.). It can be useful later on for setting defaults. Any spaces are replaced with underscores. It is set to a null string if we can't figure it out. osvers. This variable contains the operating system version (e.g. 4.1.3, 5.2, etc.).

The variable $^O (that's a capital 'O', not a zero) holds the name of the operating system.

Depending on what you want, it may or may not give the answer you want - on my system it gives 'linux' without saying which distro. I'm not so sure about what it says on Windows or MacOS.

Modules that provide OS-specific behaviors often need to know if the current operating system matches a more generic type of operating systems. For example  I am creating a small perl script on Windows. I want to determine the version of Windows i.e. XP, Vista, 7, 8, etc. How can I get this information?

new; os; device; perl; perl_build; perl_long; httpd printf "Perl version is %s\n" , $​info ->perl; printf "CPU: %s\n" , scalar $cpu ->identify;  This is my own attempt to document the Perl system() function as well as provide some usage examples and misexamples. The system() Basics. The system() function executes an operating system command by forking a child process and waiting for the child process to complete, returning some sort of exit status for the child that you can test.

Just a very simple command to determine the operating system in which the script is running #!/usr/bin/perl print "The OS in which the script is  The above command will put the files into a single string, demarcated by the newline character. Perl system () function spawns a child process and goes to the sleep mode, while the command is being executed. $^O is a string which contains the name of the current operating system on which the Perl script is run.

This page explains how to find Perl version from a command prompt or using a Find Out Perl Version From a Linux / Unix / BSD / OS X Shell Prompt $^V can be used to determine whether the Perl interpreter executing a  Find answers to Perl script to detect IP, browser name/version, operating system from the expert community at Experts Exchange

Comments
  • Why do you need to know? [There may be a more appropriate answer to your question, depending on what (if anything) it is you're doing that's platform-dependant.]
  • Thanks :) And for other people who may use this answer, cygwin perl returns "cygwin", so there are two possibilities for Windows.
  • More than two; there's was a dos port, and the os2 port used to be able to run on windows. All possible values of $^O are in theory documented in <perldoc.perl.org/perlport.html>.
  • print "$^O\n"; does indeed print "darwin" on my Mac OS X sysem.
  • @ysth typo in your link by the way
  • In case anyone gets temporarily stuck on this because you're getting STDOUT_TOP0 as the output, make sure you're using $^O instead of $^0. I can't tell you how I know this, but trust me...
  • win32 uses a deprecated function GetVersionExA. This does not work vor Version > 6.2
  • @MayraDelgado Indeed, and it looks like there is no direct replacement. Which is why someone who needs this should figure out how to fix it and provide a pull request. I rarely use Windows anymore anyway, so it isn't likely to be me.
  • This would have been a better answer, if only it would support Mac. I prefer the compiler-aided functions, to the maintenance-problem strings.
  • This is probably the best answer out of all of these. Unfortunately it's not voted as popular because no one wants to do any research on their own beyond Stack Overflow. I am going to add to the answer so lazy people don't have to go through all of the hard work of clicking on a link. :P
  • Except if you're running darwin os !
  • Crap! You're right! Making it case sensitive seems to fix it. I've corrected it above. Good catch!
  • the case-sensitive version doesn't detect "cygwin"