Perl:从另一个调用 perl 脚本 [英] Perl: Calling a perl script from another

查看:72
本文介绍了Perl:从另一个调用 perl 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个调用另一个脚本的 perl 脚本.我使用反引号调用它并将参数传递给该脚本,它工作正常.

I have a perl script which calls another script. I am calling it using backticks and passing argument to that script and it works fine.

`CQPerl call_script.pl $agr1 $agr2 $arg3`;

但是请建议是否有另一种更好的方法.如何检查脚本是否因为调用脚本或被调用的脚本而出错.我如何从调用脚本本身进行检查?

But please suggest if there is another better way to do so. How can I check if the script errored out because of the calling script or the script that was called. How do I do that check from the calling script itself?

推荐答案

如果您不想进行错误检查,反引号可能是一种错误的方法.您可能想要使用 system 函数.有关错误处理的所有详细信息(包括示例),请参阅文档.

If you wan't to do error checking, backticks may be a wrong approach. You probably want to use the system function. See the documentation for all the details of error handling, examples included.

Perl 有许多执行其他脚本/命令的可能性:

Perl has a number of possibilites to execute other scripts / commands:

  • 反引号/qx{} 当您想在程序终止后立即读取所有输出时
  • exec 当您不想作为另一个程序继续您的进程时 - 如果成功则永远不会返回
  • system 当你只对命令的成功或失败感兴趣时
  • open 当您想通过管道将信息传入或传出命令时
  • dorequire 在这里执行另一个 Perl 脚本.类似于 C 的 #include
  • 有一些模块可以进行三向open,以便您可以访问STDINSTDOUTSTDERR您执行的程序的代码>.有关高级信息,请参阅 perlipc 的相应部分.
  • backticks / qx{} When you want to read all the output at once after the program has terminated
  • exec When you wan't to continue your process as another program — never returns if succesfull
  • system When you are only interested in the success or failure of the command
  • open When you want to pipe information to or from the command
  • do and require Execute another Perl script here. Similar to C's #include
  • There are modules to do a three-way open so that you have access to STDIN, STDOUT and STDERR of the program you executed. See the apropriate parts of perlipc for advanced information.

并且始终使用这些调用的多参数形式来避免 shell 转义(可能很烦人且非常不安全).

And always use the multi-argument forms of these calls to avoid shell escaping (can be annoying and very insecure).

这篇关于Perl:从另一个调用 perl 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆