代码之家  ›  专栏  ›  技术社区  ›  mxcl

如何在Perl中检测操作系统?

  •  54
  • mxcl  · 技术社区  · 16 年前

    编辑:

    10 回复  |  直到 16 年前
        1
  •  86
  •   Community CDub    5 年前

    检查 $^O 包含操作系统名称的变量:

    print "$^O\n";
    

    哪张照片 linux MSWin32 在窗户上。

    $OSNAME 如果你使用 English 模块:

    use English qw' -no_match_vars ';
    print "$OSNAME\n";
    

    根据 perlport , $^O darwin


    您也可以使用 Config

    use Config;
    
    print "$Config{osname}\n";
    print "$Config{archname}\n";
    

    在我的Ubuntu机器上打印:

    linux
    i486-linux-gnu-thread-multi
    

    请注意,此信息基于Perl使用的系统 建造 $^O $OSNAME ); 操作系统可能不会有什么不同,但一些信息,如架构名称,很可能会有所不同。

        2
  •  14
  •   Brad Gilbert    13 年前

    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/;
    }}
    

    源自 sysinfo.t ,这是我写的原始版本。

    如果您需要更详细的信息:

    my ( $osvername, $major, $minor, $id ) = Win32::GetOSVersion();
    
        3
  •  7
  •   Brad Gilbert    13 年前

    Sys::Info::OS 看起来是一个相对干净的潜在解决方案,但目前似乎不支持Mac。不过,添加这一点应该不会太多。

        4
  •  7
  •   tjwrona1992    7 年前

    在源代码内部查找 File::Spec

    文件::规范 每个操作系统都有一个单独的Perl模块文件。 File::Spec::Win32 File::Spec::OS2

    它检查操作系统并将加载相应的 .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");
    
        5
  •  3
  •   Geoglyph    16 年前

    取决于你想要什么,它可能给出你想要的答案,也可能不给出你想要的答案——在我的系统上,它给出了“linux”,而没有说明是哪个发行版。我不太确定它在Windows或MacOS上说了些什么。

        6
  •  2
  •   willasaywhat    16 年前

    Here's 关于如何从Perl查找本地计算机正在运行的操作系统的快速参考。

        7
  •  1
  •   Hawk    12 年前

    my $windows=($^O=~/Win/)?1:0;# Are we running on windows?
    
        8
  •  1
  •   Amer Neely    5 年前

    Mac computers上的FYI$^O现在返回10.13.6(High Sierra)和10.15.4(Catalina)的“darwin”。

        9
  •  0
  •   bcarroll    12 年前
    #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;
    
        10
  •  0
  •   Randall user782220    6 年前

    Perl::OSType

    Module::Build .

        11
  •  -3
  •   GC 13    11 年前

    cat/etc/os版本

    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"