如何在不打印结果/输出文本的情况下在 Perl 中使用系统调用? [英] How can I use system call in Perl without printing the result/output texts?

查看:42
本文介绍了如何在不打印结果/输出文本的情况下在 Perl 中使用系统调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows XP 上使用 Cygwin Perl.

I am using Cygwin Perl on Windows XP.

我有这段代码可以运行第三方程序.

I have this code that will run a third party program.

sub main {
    print("Start Running\n");
    @return = run_exe($execApp, $parm1, $parm2);
    my $returncode = pop @return;
    if ($returncode == 0) {
        print("Success\n");
    }
    else {
        print("Error\n");
    }
}

sub run_exe {
    my ($exe, @parm) = @_;
    my (@output, @return_output);

    @output = system($exe, @parm);

    foreach (@output) {
        next if !/\S/;       # white space line
        s/^(\s*)//g;         # trim from the front for $_
        s/(\s*)$//g;         # trim from the end for $_
        push (@return_output, $_);
    }

    push (@return_output, $?>>8);
    @output = ();

    return (@return_output);
}

如果成功,这将打印:

Start Running
Return Code: 0
Success

我想要的不是打印运行 run_exe 子程序的输出(即返回代码:0):

What I want is not to print the output from running run_exe subroutine (i.e Return Code: 0):

Start Running
Success

我怎么能做到这一点?请帮帮我,我在 Google 上搜索过,但什么也没找到.

How could I achieve this? Please help me, I have search in Google but I found nothing.

推荐答案

你在你的程序中使用了 system,它让你正在运行的程序打印到 STDOUT 并返回程序的退出状态(一个数字).

You have used system in your program, which lets the program you are running print to STDOUT and returns the exit status of the program (a number).

因此,您目前拥有的内容没有任何意义,因为 @output 将只包含一个数字,您无法将其解析为正在运行的程序的输出.

So what you have at present makes no sense, as @output will contain just a single number and you cannot parse it as output from the program you are running.

你可能需要的是反引号

`$exe @parm`

或更明确地说,

qx{$exe @parm}

这将返回程序的输出,正如您所期望的那样,并避免将其打印到 STDOUT.

which will return the output from the program, as you seem to be expecting, and avoid it being printed to STDOUT.

注意,你可能需要注意$exe@parm的内容,因为结果字符串在命令行必须是有效的,字段可能需要适当地引用或转义.

Note that you may need to pay attention to the contents of $exe and @parm, as the resulting string must be valid at the command line, and the fields may need quoting or escaping appropriately.

更新

再次查看您的代码,您似乎可能期待system 的退出状态.但是把它放在一个数组中,遍历数组,提取非空元素是非常的!结果与添加到返回数组中的 $? 的值相同.

Looking again at your code again it seems that you may be expecting the exit status from system. But putting it an array, looping over the array, and extracting non-blank elements is very convoluted! The result is the same value as $? that you add to the returned array.

要转移程序的输出,您需要从命令外壳而不是直接运行它,因此要将输出发送到 Windows nul 设备,您的代码将如下所示

To divert the output of a program you need to run it from the command shell instead of directly, so to send the output to the Windows nul device your code would look like

system('cmd /C', $exe, @parm, '>nul');
return $? >> 8;

其实你也可以去掉子程序改行

In fact you may as well drop the subroutine and change the lines

@return = run_exe($execApp,$parm1,$parm2);
my $returncode = pop @return;

system('cmd /C', $execApp, $parm1, $parm2, '>nul');
my $returncode = $? >> 8;

这篇关于如何在不打印结果/输出文本的情况下在 Perl 中使用系统调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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