views:

5134

answers:

6

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.

+15  A: 

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;
print "$OSNAME\n";

Edit: I don't have access to a Mac at the moment but according to perlport, $^O will be darwin on Mac OS X.

Edit: 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.

Robert Gamble
Thanks :) And for other people who may use this answer, cygwin perl returns "cygwin", so there are two possibilities for Windows.
Max Howell
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 <http://perldoc.perl.org/perlport.html>.
ysth
print "$^O\n"; does indeed print "darwin" on my Mac OS X sysem.
ShreevatsaR
+1  A: 

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.

Abyss Knight
+2  A: 

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.

+7  A: 

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.

Leon Timmermans
This would have been a better answer, if only it would support Mac. I prefer the compiler-aided functions, to the maintenance-problem strings.
Max Howell
http://search.cpan.org/perldoc?Sys::Info::OS
Brad Gilbert
+1  A: 

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

brian d foy
+3  A: 

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();
Brad Gilbert