如何从Perl脚本中运行Perl脚本? [英] How do I run a Perl script from within a Perl script?

查看:221
本文介绍了如何从Perl脚本中运行Perl脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Perl脚本,需要执行另一个Perl脚本.第二个脚本可以直接在命令行上执行,但是我需要在第一个程序中执行它.我需要向它传递一些通常在独立运行时会传入的参数(第一个脚本会定期运行,并在一组特定的系统条件下执行第二个脚本).

I've got a Perl script that needs to execute another Perl script. This second script can be executed directly on the command line, but I need to execute it from within my first program. I'll need to pass it a few parameters that would normally be passed in when it's run standalone (the first script runs periodically, and executes the second script under a certain set of system conditions).

Google的初步搜索建议使用反引号或system()调用.还有其他方法可以运行它吗? (我猜是的,因为这是Perl,我们正在谈论:P)如果我需要捕获被调用程序的输出(并且,如果可能的话,在执行到stdout时将其输出通过管道传递给第二个方法,则首选哪种方法)程序被直接调用)?

Preliminary Google searches suggest using backticks or a system() call. Are there any other ways to run it? (I'm guessing yes, since it's Perl we're talking about :P ) Which method is preferred if I need to capture output from the invoked program (and, if possible, pipe that output as it executes to stdout as though the second program were invoked directly)?

(哦,现在,所以建议了一些相关问题.

( oh, now SO suggests some related questions. This one is close, but not exactly the same as what I'm asking. The second program will likely take an hour or more to run (lots of I/O), so I'm not sure a one-off invocation is the right fit for this.)

推荐答案

当前的perl解释器的位置可以在特殊变量$^X中找到.如果您不在perl中,或者您有多个可用的perl版本,但是要确保您使用的是同一版本,这一点很重要.

The location of your current perl interpreter can be found in the special variable $^X. This is important if perl is not in your path, or if you have multiple perl versions available but which to make sure you're using the same one across the board.

在执行外部命令(包括其他Perl程序)时,确定它们是否实际运行可能非常困难.检查$?可能会留下持久的精神疤痕,因此我更喜欢使用 IPC :: System :: Simple (可从CPAN获得):

When executing external commands, including other Perl programs, determining if they actually ran can be quite difficult. Inspecting $? can leave lasting mental scars, so I prefer to use IPC::System::Simple (available from the CPAN):

use strict;
use warnings;
use IPC::System::Simple qw(system capture);

# Run a command, wait until it finishes, and make sure it works.
# Output from this program goes directly to STDOUT, and it can take input
# from your STDIN if required.
system($^X, "yourscript.pl", @ARGS);

# Run a command, wait until it finishes, and make sure it works.
# The output of this command is captured into $results.
my $results = capture($^X, "yourscript.pl", @ARGS);

在以上两个示例中,您希望传递给外部程序的所有参数都放入@ARGS中.在以上两个示例中都避免使用shell,这给您带来了较小的速度优势,并且避免了涉及shell元字符的任何不必要的交互.上面的代码还希望您的第二个程序返回零退出值以指示成功;如果不是这种情况,则可以指定允许的退出值的其他第一个参数:

In both of the above examples any arguments you wish to pass to your external program go into @ARGS. The shell is also avoided in both of the above examples, which gives you a small speed advantage, and avoids any unwanted interactions involving shell meta-characters. The above code also expects your second program to return a zero exit value to indicate success; if that's not the case, you can specify an additional first argument of allowable exit values:

 # Both of these commands allow an exit value of 0, 1 or 2 to be considered
 # a successful execution of the command.

 system( [0,1,2], $^X, "yourscript.pl", @ARGS );
 # OR
 capture( [0,1,2, $^X, "yourscript.pl", @ARGS );

如果您有一个长期运行的过程,并且想要在生成数据的同时 处理数据,则可能需要通过管道打开,或者是重量更重的IPC模块之一来自CPAN.

If you have a long-running process and you want to process its data while it's being generated, then you're probably going to need a piped open, or one of the more heavyweight IPC modules from the CPAN.

说了这么多,每当您需要从Perl调用另一个Perl程序时,您可能希望考虑使用模块是否是更好的选择.启动另一个程序会带来很多开销,包括启动成本和在流程之间移动数据的I/O成本.这也大大增加了错误处理的难度.如果您可以将外部程序转换为模块,则可能会发现它简化了总体设计.

Having said all that, any time you need to be calling another Perl program from Perl, you may wish to consider if using a module would be a better choice. Starting another program carries quite a few overheads, both in terms of start-up costs, and I/O costs for moving data between processes. It also significantly increases the difficulty of error handling. If you can turn your external program into a module, you may find it simplifies your overall design.

祝一切顺利,

Paul

这篇关于如何从Perl脚本中运行Perl脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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