使用Perl检测和显示操作系统

时间:2013-03-24 19:04:19

标签: perl

我想使用Perl显示计算机的操作系统。我认为以下会做

#!/usr/bin/perl
use strict;
use warnings;

my $os = $^O;
print "$os\n";

但我得到的输出是

linux

我希望输出显示为

Windows XP

怎么做?请帮忙。提前谢谢。

3 个答案:

答案 0 :(得分:6)

使用POSIX module provides for you

uname()功能

答案 1 :(得分:2)

所有Perl发行版都附带Config模块。这个模块有点摇摇晃晃,因为它导入了散列%Config,但它是一种访问各种Perl配置信息的简单方法。

use Config;  # This exports a %Config hash

print "My OS is $Config{osname}\n";
print "My OS version is $Config{osvers}\n";
print "My Architecture Family is $Config{archname}\n";

#
# Print out all of the keys, and see which one looks like one
# you can use. "ld" is actually a rather nice value
#
for my $key (sort keys %Config) {
    no warnings qw(uninitialized); # Some elements have undef values
    print qq($key: "$Config{$key}"\n);
}

答案 2 :(得分:1)

您是否正在寻找

use CGI                 qw( );
use HTTP::BrowserDetect qw( );

my $cgi = CGI->new();
print $cgi->header('text/plain');

my $bd = HTTP::BrowserDetect->new($cgi->user_agent());
print $bd->os_string(), "\n";