I have Perl on Mac, Windows and Ubuntu. How can I tell from within the script which one is which?
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
$^Ovariable which will contain the name of the operating system:Which prints
linuxon Linux andMSWin32on Windows.You can also refer to this variable by the name
$OSNAMEif you use the English module:According to perlport,
$^Owill bedarwinon Mac OS X.You can also use the Config core module, which can provide the same information (and a lot more):
Which on my Ubuntu machine prints:
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
$^Oand$OSNAME); the OS won’t likely be different but some information, like the architecture name, may very well be.