如何判断我的 Perl 脚本是否在 Windows 下运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1423900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:08:00  来源:igfitidea点击:

How can I tell if my Perl script is running under Windows?

windowsperl

提问by knorv

What is the best way to programatically determine if a Perl script is executing on a Windows based system (Win9x, WinXP, Vista, Win7, etc.)?

以编程方式确定 Perl 脚本是否在基于 Windows 的系统(Win9x、WinXP、Vista、Win7 等)上执行的最佳方法是什么?

Fill in the blanks here:

填写此处的空白:

my $running_under_windows = ... ? 1 : 0;

回答by Chris Lutz

From perldoc perlvar:

来自perldoc perlvar

  • $OSNAME
  • $^O

The name of the operating system under which this copy of Perl was built, as determined during the configuration process. The value is identical to $Config{'osname'}. See also Config and the -V command-line switch documented in perlrun.

In Windows platforms, $^Ois not very helpful: since it is always MSWin32, it doesn't tell the difference between 95/98/ME/NT/2000/XP/CE/.NET. Use Win32::GetOSName()or Win32::GetOSVersion()(see Win32and perlport) to distinguish between the variants.

  • $OSNAME
  • $^O

在配置过程中确定的 Perl 副本在其下构建的操作系统的名称。该值与 相同$Config{'osname'}。另请参阅 perlrun 中记录的 Config 和 -V 命令行开关。

在 Windows 平台上,$^O不是很有帮助:因为它总是MSWin32,它不能区分 95/98/ME/NT/2000/XP/CE/.NET 之间的区别。使用Win32::GetOSName()Win32::GetOSVersion()(参见Win32perlport)来区分变体。

回答by hillu

$^O eq 'MSWin32'

(Source: The perlvarmanpage)

(来源:perlvar联机帮助页)

回答by brian d foy

Use Devel::CheckOS. It handles all of the logic and special cases for you. I usually do something like:

使用Devel::CheckOS。它为您处理所有逻辑和特殊情况。我通常做这样的事情:

use Devel::CheckOS qw(die_unsupported os_is);

die "You need Windows to run this program!" unless os_is('MicrosoftWindows');

The 'MicrosoftWindows' families knows about things such as Cygwin, so if you are on Windows but not at the cmd prompt, os_is()will still give you the right answer.

'MicrosoftWindows' 家族知道 Cygwin 之类的东西,所以如果你在 Windows 上但不在 cmd 提示符下,os_is()仍然会给你正确的答案。

回答by DJB55

This is very quick and dirty, and wouldn't bet it's 100% portable, but still useful in a pinch. Check for presence of back slashes in the PATH Env variable, since PATH is common to both Windows and Unix. So - in Perl:

这是非常快速和肮脏的,并且不会打赌它是 100% 便携的,但在紧要关头仍然有用。检查 PATH Env 变量中是否存在反斜杠,因为 PATH 对 Windows 和 Unix 都是通用的。所以 - 在 Perl 中:

if ( $ENV{PATH}=~m{\} ) {
  #Quick and dirty: It's windows!
  print "It's Windows!";
} else {
  print "It's Unix!";
}