How can I detect the operating system in Perl?

后端 未结 11 1482
我在风中等你
我在风中等你 2020-12-04 16:41

I have Perl on Mac, Windows and Ubuntu. How can I tell from within the script which one is which? Thanks in advance.

Edit: I was asked what I am doi

11条回答
  •  盖世英雄少女心
    2020-12-04 17:05

    Look inside the source for File::Spec to see how it loads the right delegate based on the operating system. :)

    File::Spec has a separate Perl module file for each OS. File::Spec::Win32, File::Spec::OS2, etc...

    It checks the operating system and will load the appropriate .pm file at runtime based on OS.

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

提交回复
热议问题