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 doi
FYI on Mac computers $^O now returns 'darwin' for 10.13.6 (High Sierra) and 10.15.4 (Catalina).
A classic one-liner:
my $windows=($^O=~/Win/)?1:0;# Are we running on windows?
#Assign the $home_directory variable the path of the user's home directory
my $home_directory = ($^O eq /Win/) ? $ENV{HOMEPATH} : $ENV{HOME};
#Then you can read/write to files in the home directory
open(FILE, ">$home_directory/my_tmp_file");
print FILE "This is a test\n";
close FILE;
#And/or read the contents of the file
open(FILE, "<$home_directory/my_tmp_file");
while (<FILE>){
print $_;
}
close FILE;
For a generic mapping in a pre-packaged perl module, check out Perl::OSType.
It's used by Module::Build
.
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();