How to call a function (defined in shell script) in a Perl script

后端 未结 3 1005
旧时难觅i
旧时难觅i 2020-12-19 09:33

I have two scripts, namely shell_script.sh and perl_script.pl.

shell_script.sh : It has function definitions which, when invo

3条回答
  •  天涯浪人
    2020-12-19 10:04

    It's overkill for this problem, but Env::Modify provides a way to make shell functions available in Perl. With Env::Modify, you could, say, import the shell functions once and use them over and over again in subsequence system calls.

    use Env::Modify qw(:bash system source qx);
    
    # import the shell functions
    system("source shell_script.sh ; export -f func_1 func_2");
    # alternate: put  "export -f func_1 func_2"  at the end of shell_script.sh and call
    source("shell_script.sh");
    
    # use the shell functions
    system("func_1");
    $output = `func_2`;
    ...
    

提交回复
热议问题