Linux 如何确保仅当脚本在Windows上运行时才加载模块?

Linux 如何确保仅当脚本在Windows上运行时才加载模块?,linux,windows,perl,module,perl-module,Linux,Windows,Perl,Module,Perl Module,我有一个需要在Windows和Linux上运行的Perl脚本。问题是我需要使用一个只适用于Windows的Perl模块 我已经尝试了下面的,但它仍然包括这个WindowsStuff包 use strict; if ($^O eq 'MSWin32' ){ use My::WindowsStuff; } use File::Basename; use Getopt::Long; ... ... 因为use在编译时生效,所以它不尊重正在编译的代码的普通流控制。 特别是,在条件的假分支中使用

我有一个需要在Windows和Linux上运行的Perl脚本。问题是我需要使用一个只适用于Windows的Perl模块

我已经尝试了下面的,但它仍然包括这个
WindowsStuff

use strict;
if ($^O eq 'MSWin32' ){
    use My::WindowsStuff;
}
use File::Basename;
use Getopt::Long;
...
...

因为
use
在编译时生效,所以它不尊重正在编译的代码的普通流控制。 特别是,在条件的假分支中使用并不能阻止它被处理

你能做什么

a) 导入(运行时):

b) (编译时):

if( $^O eq 'MSWin32' ) {
   require My::WindowsStuff;
   My::WindowsStuff->import if My::WindowsStuff->can("import");
}
use if $^O eq 'MSWin32', "My::WindowsStuff";