【问题标题】:How to call perl from C++?如何从 C++ 调用 perl?
【发布时间】:2014-08-07 05:46:17
【问题描述】:

我有一个 perl 程序,用于处理特定格式的大型 taxt 文件。我已经从该 perl 中准备了一个 exe 文件,该文件用作 Windows 控制台应用程序。但对于学术用途,该应用程序需要使用更好的 GUI (C++) 编写。
我不可能用 C++ 再次重写整个代码(由于时间限制)。
有没有办法从 C++ GUI 获取文件,使用 perl App(.pl 或 .exe)进行处理,然后再次使用 C++ Window 显示输出。
欢迎任何其他更好的选择。

【问题讨论】:

  • 您是否考虑过使用类似 TkWxPrima 之类的东西在 Perl 中构建 GUI,而不是在 C++ 中创建 GUI?
  • @tobyink 即使这些也需要重写整个代码。而且时间太长了,时间不允许。我会考虑他们在未来的另一次冒险,而不是这次。

标签: c++ perl dev-c++


【解决方案1】:

这是一个使用Prima 选择输入文件并对其运行简单报告的快速示例。

希望它说明您不需要重写整个 Perl 应用程序来添加一个简单的 GUI。前几个函数完成了处理文件和生成报告的实际工作。应用程序的这一部分不需要了解关于 GUI 的任何事情。

最后一部分提供了一个围绕它的 GUI 包装器。这是应用程序中唯一需要处理 Prima 的部分。

use strict;
use warnings;

# This is the guts of the report.
# It takes a filehandle and does some serious number crunching!
# Just kidding. It counts the occurrences of vowels in a text
# file. But it could be doing any serious reporting work you want.
# 
sub get_data_from_file {
  my ($fh) = @_;
  my %vowels;
  while (<$fh>) {
    $vowels{uc($_)}++ for /([aeiou])/gi;
  }
  return \%vowels;
}

# Format report in Pod because personally I find
# that a bit easier to deal with than Prima::TextView.
#
sub format_data_as_pod {
  my ($data) = @_;
  my $pod = "=pod\n\n";
  $pod .= sprintf("B<%s> = %d\n\n", $_, $data->{$_})
    for sort keys %$data;
  $pod .= "=cut\n\n";
  return $pod;
}

# Here's the GUI...
#
MAIN: {
  use Prima qw( Application Buttons FileDialog PodView );

  my $mw = Prima::MainWindow->new(
    text   => 'Vowel Counter',
    size   => [ 300, 200 ],
  );

  $mw->insert(
    Button => (
      centered  => 1,
      text      => 'Choose file',
      onClick   => sub {
        my $open = Prima::OpenDialog->new(
          filter => [
            [ 'Text files' => '*.txt' ],
            [ 'All files'  => '*' ],
          ],
        );
        if ( $open->execute ) {
          my $filename = $open->fileName;
          open(my $handle, '<', $filename)
            or die("Could not open selected file: $?");

          my $data   = get_data_from_file($handle);
          my $report = format_data_as_pod($data);

          my $report_window = Prima::Window->create(
            text  => "Report for $filename",
            size  => [ 200, 300 ],
          );

          my $pod = $report_window->insert(
            PodView => (
              pack => { expand => 1, fill => 'both' },
            ),
          );
          $pod->open_read;
          $pod->read($report);
          $pod->close_read;
        }
        else {
          die("No file chosen");
        }
      },
    ),
  );

  Prima->run;
}

如果您将前两个函数分解为它们自己的模块,那么不仅提供调用它们的 GUI 应用程序,而且还为命令行使用提供基于文本的替代 UI 将非常容易。

【讨论】:

  • Prima 的好例子。你是对的,tiz非常容易。 +1
【解决方案2】:

你有两个选择:

  1. 在您的 C++ 程序中嵌入 Perl 解释器:http://perldoc.perl.org/perlembed.html
  2. 让您的 C++ 程序将 Perl 作为子进程调用。使用system()popen() or fork()/exec(),具体取决于您需要多少控制。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-23
    • 2011-01-31
    • 2011-04-13
    • 1970-01-01
    相关资源
    最近更新 更多