如何在 Perl 中检测操作系统? [英] How can I detect the operating system in Perl?

查看:34
本文介绍了如何在 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
";

在 Linux 上打印 linux,在 Windows 上打印 MSWin32.

Which prints linux on Linux and MSWin32 on Windows.

您也可以通过名称$OSNAME来引用此变量 如果您使用 English 模块:

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
";

根据perlport$^O 在 Mac OS X 上将成为 darwin.

您还可以使用 Config 核心模块,它可以提供相同的信息(以及更多):

You can also use the Config core module, which can provide the same information (and a lot more):

use Config;

print "$Config{osname}
";
print "$Config{archname}
";

在我的 Ubuntu 机器上打印:

Which on my Ubuntu machine prints:

linux
i486-linux-gnu-thread-multi

请注意,此信息基于创建 Perl 的系统,不一定是当前运行 Perl 的系统(对于 $^O$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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆