如何在Perl中检测操作系统? [英] How can I detect the operating system in Perl?
问题描述
我在Mac,Windows和Ubuntu上都有Perl.如何从脚本中分辨出哪个是哪个?预先感谢.
I have Perl on Mac, Windows and Ubuntu. How can I tell from within the script which one is which? Thanks in advance.
编辑:有人问我在做什么.它是脚本,是我们跨平台构建系统的一部分.该脚本递归目录并找出要构建的文件.有些文件是特定于平台的,因此,在Linux上,我不想构建以_win.cpp等结尾的文件.
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.
推荐答案
检查 $^O
变量,它将包含操作系统的名称:
Examine the $^O
variable which will contain the name of the operating system:
print "$^O\n";
在Linux上打印linux
,在Windows上打印MSWin32
.
Which prints linux
on Linux and MSWin32
on Windows.
如果您还可以使用名称 $OSNAME
来引用此变量.使用英语模块:
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";
根据 perlport ,您还可以使用 Config 核心模块,该模块可以提供相同的信息(以及还有很多):
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";
在我的Ubuntu机器上打印的内容:
Which on my Ubuntu machine prints:
linux
i486-linux-gnu-thread-multi
请注意,此信息基于Perl是构建的系统,不一定是Perl当前正在运行的系统( $OSNAME
);操作系统可能不会有所不同,但是某些信息(如体系结构名称)可能会有所不同.
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中检测操作系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!