I have Perl on Mac, Windows and Ubuntu. How can I tell from within the script which one is which? Thanks in advance.
我在Mac,Windows和Ubuntu上安装了Perl。如何在脚本中告诉哪一个是哪个?提前致谢。
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.
编辑:我被问到我在做什么。它是一个脚本,是我们跨平台构建系统的一部分。该脚本会递归目录并确定要构建的文件。有些文件是特定于平台的,因此,在Linux上我不想构建以_win.cpp结尾的文件等。
9 个解决方案
#1
73
Examine the $^O
variable which will contain the name of the operating system:
检查包含操作系统名称的$ ^ O变量:
print "$^O\n";
Which prints linux
on Linux and MSWin32
on Windows.
它在Linux上打印Linux,在Windows上打印MSWin32。
You can also refer to this variable by the name $OSNAME
if you use the English module:
如果使用英语模块,也可以通过名称$ OSNAME引用此变量:
use English qw' -no_match_vars ';
print "$OSNAME\n";
According to perlport, $^O
will be darwin
on Mac OS X.
根据perlport,$ ^ O将是Mac OS X上的darwin。
You can also use the Config core module, which can provide the same information (and a lot more):
您还可以使用Config核心模块,它可以提供相同的信息(以及更多):
use Config;
print "$Config{osname}\n";
print "$Config{archname}\n";
Which on my Ubuntu machine prints:
在我的Ubuntu机器上打印:
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.
请注意,此信息基于构建Perl的系统,这不一定是Perl当前正在运行的系统(对于$ ^ O和$ OSNAME也是如此);操作系统可能不会有所不同,但一些信息,如架构名称,可能很好。
#2
10
If you need more specific information on Windows this may help.
如果您需要有关Windows的更多具体信息,这可能有所帮助。
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.
源自sysinfo.t,我写了原始版本。
If you need more detailed information:
如果您需要更详细的信息:
my ( $osvername, $major, $minor, $id ) = Win32::GetOSVersion();
#3
7
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.
Sys :: Info :: OS看起来像一个相对干净的潜在解决方案,但目前似乎不支持Mac。尽管如此,添加它不应该是太多的工作。
#4
3
The variable $^O (that's a capital 'O', not a zero) holds the name of the operating system.
变量$ ^ O(即大写'O',而不是零)保存操作系统的名称。
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.
根据你的需要,它可能会或可能不会给你想要的答案 - 在我的系统上,它给出了'linux'而不说哪个发行版。我不太确定它在Windows或MacOS上的含义。
#5
3
Look inside the source for File::Spec
to see how it loads the right delegate based on the operating system. :)
查看File :: Spec的源代码,了解它如何根据操作系统加载正确的委托。 :)
File::Spec
has a separate Perl module file for each OS. File::Spec::Win32
, File::Spec::OS2
, etc...
File :: Spec为每个操作系统都有一个单独的Perl模块文件。 File :: Spec :: Win32,File :: Spec :: OS2等......
It checks the operating system and will load the appropriate .pm
file at runtime based on OS.
它会检查操作系统,并在运行时根据操作系统加载相应的.pm文件。
# 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");
#6
2
Here's a quick reference on how to find the OS the local machine is running from Perl.
以下是如何从Perl查找本地计算机运行的操作系统的快速参考。
The $^O variable ($OSTYPE if you use English) contains the operating system that your perl binary was built for.
$ ^ O变量(如果使用英语,则为$ OSTYPE)包含为perl二进制文件构建的操作系统。
#7
1
A classic one-liner:
经典的单线:
my $windows=($^O=~/Win/)?1:0;# Are we running on windows?
#8
0
#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;
#9
-2
yes using Config module can be a good thing. One more possibility is getting the info from /etc/*release files
是的,使用Config模块可能是一件好事。另一种可能性是从/ etc / *发布文件中获取信息
for eg..
例如......
cat /etc/os-release
cat / etc / os-release
NAME="UBUNTU"
VERSION="12.0.2 LTS, Precise Pangolin"
ID="UBUNTU"
ID_LIKE=debian
PRETTY_NAME="Ubuntu precise (12.0.2 LTS)"
VERSION_ID="12.04"
#1
73
Examine the $^O
variable which will contain the name of the operating system:
检查包含操作系统名称的$ ^ O变量:
print "$^O\n";
Which prints linux
on Linux and MSWin32
on Windows.
它在Linux上打印Linux,在Windows上打印MSWin32。
You can also refer to this variable by the name $OSNAME
if you use the English module:
如果使用英语模块,也可以通过名称$ OSNAME引用此变量:
use English qw' -no_match_vars ';
print "$OSNAME\n";
According to perlport, $^O
will be darwin
on Mac OS X.
根据perlport,$ ^ O将是Mac OS X上的darwin。
You can also use the Config core module, which can provide the same information (and a lot more):
您还可以使用Config核心模块,它可以提供相同的信息(以及更多):
use Config;
print "$Config{osname}\n";
print "$Config{archname}\n";
Which on my Ubuntu machine prints:
在我的Ubuntu机器上打印:
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.
请注意,此信息基于构建Perl的系统,这不一定是Perl当前正在运行的系统(对于$ ^ O和$ OSNAME也是如此);操作系统可能不会有所不同,但一些信息,如架构名称,可能很好。
#2
10
If you need more specific information on Windows this may help.
如果您需要有关Windows的更多具体信息,这可能有所帮助。
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.
源自sysinfo.t,我写了原始版本。
If you need more detailed information:
如果您需要更详细的信息:
my ( $osvername, $major, $minor, $id ) = Win32::GetOSVersion();
#3
7
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.
Sys :: Info :: OS看起来像一个相对干净的潜在解决方案,但目前似乎不支持Mac。尽管如此,添加它不应该是太多的工作。
#4
3
The variable $^O (that's a capital 'O', not a zero) holds the name of the operating system.
变量$ ^ O(即大写'O',而不是零)保存操作系统的名称。
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.
根据你的需要,它可能会或可能不会给你想要的答案 - 在我的系统上,它给出了'linux'而不说哪个发行版。我不太确定它在Windows或MacOS上的含义。
#5
3
Look inside the source for File::Spec
to see how it loads the right delegate based on the operating system. :)
查看File :: Spec的源代码,了解它如何根据操作系统加载正确的委托。 :)
File::Spec
has a separate Perl module file for each OS. File::Spec::Win32
, File::Spec::OS2
, etc...
File :: Spec为每个操作系统都有一个单独的Perl模块文件。 File :: Spec :: Win32,File :: Spec :: OS2等......
It checks the operating system and will load the appropriate .pm
file at runtime based on OS.
它会检查操作系统,并在运行时根据操作系统加载相应的.pm文件。
# 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");
#6
2
Here's a quick reference on how to find the OS the local machine is running from Perl.
以下是如何从Perl查找本地计算机运行的操作系统的快速参考。
The $^O variable ($OSTYPE if you use English) contains the operating system that your perl binary was built for.
$ ^ O变量(如果使用英语,则为$ OSTYPE)包含为perl二进制文件构建的操作系统。
#7
1
A classic one-liner:
经典的单线:
my $windows=($^O=~/Win/)?1:0;# Are we running on windows?
#8
0
#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;
#9
-2
yes using Config module can be a good thing. One more possibility is getting the info from /etc/*release files
是的,使用Config模块可能是一件好事。另一种可能性是从/ etc / *发布文件中获取信息
for eg..
例如......
cat /etc/os-release
cat / etc / os-release
NAME="UBUNTU"
VERSION="12.0.2 LTS, Precise Pangolin"
ID="UBUNTU"
ID_LIKE=debian
PRETTY_NAME="Ubuntu precise (12.0.2 LTS)"
VERSION_ID="12.04"