Perl调用系统指令模块-IPC::System::Simple
IPC::System::Simple - Run commands simply, with detailed diagnostics该模块主要用于调用Perl外部命令来完成任务的模块,不是有内置的'system,exec'等指令来完成这类操作吗,为什么还要写这个模块呢,会不会是重造轮子吗。原生的外部指令调用用法可以参考这里:perl调用执行外部命令
Calling Perl's in-built system() function is easy, determining if it was successful is hard. Let's face it, $? isn't the nicest variable in the world to play with, and even if you do check it, producing a well-formatted error string takes a lot of work.
用过perl内置原生的调用外部指令语句时,用起来很容易,但要追追踪成功与否或更多结果时,原生的方法就变得不友好了,'$?'也不那么好用了。对调用后的处理结果优雅会节约大量工作。该模块在调用外部指令时还可以决定是否通过shell来完成,通常的函数后有'x'字样(systemx,capturex,runx)。
用例
use IPC::System::Simple qw(system systemx capture capturex);
system("freeoa_command"); # Command succeeds or dies!
system("some_command",@args); # Succeeds or dies, avoids shell if @args
systemx("some_command",@args); # Succeeds or dies, NEVER uses the shell
# Capture the output of a command (just like backticks(``)). Dies on error.用于捕获命令的输出结果
my $output = capture("some_command");
# Just like backticks in list context.Dies on error.
my @output = capture("some_command");
# As above, but avoids the shell if @args is non-empty
my $output = capture("some_command", @args);
# As above, but NEVER invokes the shell.
my $output = capturex("some_command", @args);
IPC::System::Simple provides a subroutine called run, that executes a command using the same semantics is Perl's built-in system:
IPC::System::Simple的提供一个称为run的子程序,用于执行一个命令使用相同的语义的Perl内置system函数:
use IPC::System::Simple qw(run);
run("cat *.txt"); # Execute command via the shell
run("cat","/etc/motd"); # Execute command without shell
The primary difference between Perl's in-built system and the run command is that run will throw an exception on failure, and allows a list of acceptable exit values to be set. See "Exit values" for further information.
它们之间的主要差异是,run遇到执行失败时将抛出一个异常,并返回一个与退出信息相关的列表。有关更多信息,请参见“退出值”。
"runx(), systemx() and capturex()" for a variant of capture() that never invokes the shell, even with a single argument.
“runx(),systemx()和capturex()“是capture()函数的一个变体,不会调用shell,即使有一个参数。
Exit values
Traditionally, system commands return a zero status for success and a non-zero status for failure. IPC::System::Simple will default to throwing an exception if a non-zero exit value is returned.
You may specify a range of values which are considered acceptable exit values by passing an array reference as the first argument. The special constant EXIT_ANY can be used to allow any exit value to be returned.
通常在执行系统命令后会返回一个成功(零)状态或状态为失败(非零)的值。IPC::System::Simple将默认抛出异常如果返回一个非零退出值。
你可以指定一个数值范围被认为是可接受的退出码,该数组引用作为第一个参数传递。可以使用特殊常数EXIT_ANY来表示退出返回码。
use IPC::System::Simple qw(run system capture EXIT_ANY);
run( [0..5], "cat *.txt");# Exit values 0-5 are OK
system( [0..5], "cat *.txt");# This works the same way
my @lines = capture(EXIT_ANY,"cat *.txt"); # Any exit is fine.
The run and replacement system subroutines returns the exit value of the process:
my $exit_value = run([0..5],"cat *.txt");
# OR:
my $exit_value = system([0..5],"cat *.txt");
print "Program exited with value $exit_value\n";
最新版本:1.2
项目主页:https://metacpan.org/release/IPC-System-Simple