How can I use the Environment Modules system in Perl?

前端 未结 3 754
遥遥无期
遥遥无期 2021-01-19 11:50

How can one use the Environment Modules system* in Perl?

Running

system(\"load module \");

does not work,

3条回答
  •  再見小時候
    2021-01-19 12:44

    system("load module foo ; foo bar");
    

    or, if that doesn't work, then

    system("load module foo\nfoo bar");
    

    I'm guessing it makes changes to the environment variables. To change Perl's environment variables, it would have to be executed within the Perl process. That's not going to work since it was surely only designed to be integrated into the shell. (It might not be too hard to port it, though.)

    If you are ok with restarting the script after loading the module, you can use the following workaround:

    use String::ShellQuote qw( shell_quote );
    
    BEGIN {
       if (!@ARGV || $ARGV[0] ne '!!foo_loaded!!') {
          my $perl_cmd = shell_quote($^X, '--', $0, '!!foo_loaded!!', @ARGV);
          exec("load module foo ; $perl_cmd")
             or die $!;
       }
    
       shift(@ARGV);
    }
    

提交回复
热议问题