简体   繁体   English

执行一个C ++程序并使用Perl复制cmd输出

[英]Execute a C++ program and copy the cmd output using Perl

I am trying to code a perl script which would compile a C++ program and then execute it by passing in the required values. 我正在尝试编写一个Perl脚本,该脚本将编译C ++程序,然后通过传入所需的值来执行它。 After the execution, I need whatever is printed on the cmd for comparison with a sample code. 执行之后,我需要在cmd上打印任何内容,以便与示例代码进行比较。

Right now my code compiles the cpp code perfectly and correctly executes it, but completes without passing in the values. 现在,我的代码完美地编译了cpp代码并正确执行了它,但是在不传递值的情况下完成了代码。 All I am doing right now is use system commands 我现在要做的就是使用系统命令

system("cl E:\\checker\\Perl\\temp.cpp");
system("temp.exe");
system("10");
system("20");
system("30");
system("40");
system("50");

The C++ Code is something like this C ++代码是这样的

cin >> a;
cin >> b;
cin >> c;
cin >> d;
cin >> e;

The code correctly compiles and exectues, but the following values (which are inputs to the C++ code which is using cin) doesn't seem to work 该代码正确地编译和执行,但是以下值(它们是使用cin的C ++代码的输入)似乎不起作用

Please note I am using the Visual Studio compiler. 请注意,我正在使用Visual Studio编译器。 Also can someone tell me how I can extract the information outputted by the C++ code into maybe a perl array for comparison. 也有人可以告诉我如何将C ++代码输出的信息提取到perl数组中进行比较。

You can use IPC::Open2 to establish bidirectional communication between your Perl script and the other program, eg 您可以使用IPC::Open2在您的Perl脚本和其他程序之间建立双向通信,例如

use IPC::Open2;
use autodie;
my ($chld_out, $chld_in);
my $pid = open2(
    $chld_out, 
    $chld_in, 
    q(bc -lq)
);
$chld_in->print("1+1\n");
my $answer = <$chld_out>;
print $answer;
kill 'SIGINT', $pid; # I don't believe waitpid() works on Win32. Possibly with Cygwin.

Unfortunately, buffering can make this approach a lot harder than one would hope. 不幸的是,缓冲会使这种方法变得比人们希望的难得多。 You'll also have to manually wait and reap the child process. 您还必须手动等待并获得子进程。 An alternative would be to use a module like IO::Pty or Expect to create a pseudo-tty environment to simulate user interaction (but I believe these two only work in a Cygwin environment on Windows). 另一种选择是使用类似IO::PtyExpect的模块来创建伪tty环境来模拟用户交互(但是我相信这两个仅在Windows的Cygwin环境中有效)。 There's also IPC::Run , a more fully-featured alternative to IPC::Open2/3 . 还有IPC::Run ,它是IPC::Open2/3的功能更全的替代方案。

See also: perlipc and perlfaq8 . 另请参见: perlipcperlfaq8


The correct syntax for system is either system的正确语法是

system('command', 'arg1', 'arg2', ... , 'argn');

Or all as a single string, which allows shell interpretation (which you may not want): 或全部作为单个字符串,这允许外壳程序解释(您可能不需要):

system('command arg1 arg2');

system does not capture output. system不捕获输出。 Instead, use the backticks operator: 而是使用反引号运算符:

my $command_output = `command args`;

or its generic form qx . 或其通用形式qx (If you assign to an array, the output will be split on $/ and pushed onto the array one line at a time). (如果您分配给一个数组,则输出将在$/上分割,并一次推送到数组中)。

There is also the pipe form of open ( open my $pipeh, '-|', 'command', 'arg1', ..., 'argn') or die $!; 还有open的管道形式( open my $pipeh, '-|', 'command', 'arg1', ..., 'argn') or die $!; ) and the readpipe function. )和readpipe函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM