How to call perl from C++?

霸气de小男生 提交于 2019-12-06 13:30:17

问题


I have a perl program for processing the large taxt file in particular format. I have prepared an exe file from that perl which works as Windows Console App. But for academic use the App is needed to be written in Better GUI (C++).
It is impossible for me to rewrite the whole code again in C++ (Due to time constraint).
Is there any way to take file from C++ GUI, use perl App (.pl or .exe) for processing and again use C++ Window to display the output.
Any other better options are welcome.


回答1:


Here's a quick example using Prima to select an input file and run a simple report on it.

Hopefully it illustrates that you do not need to rewrite your entire Perl application to add a simple GUI. The first couple of functions do the real work of processing a file and generating a report. This part of the application doesn't need to know a thing about GUIs.

The last part provides a GUI wrapper around it. That's the only part of the application that needs to deal with 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;
}

If you factored out the first two functions into a module of their own, it would be trivially easy to not just provide this GUI application that calls them, but also provide an alternative text-based UI for command-line usage.




回答2:


You have two choices:

  1. Embed a Perl interpreter into your C++ program: http://perldoc.perl.org/perlembed.html
  2. Have your C++ program invoke Perl as a subprocess. Use system() or popen() or fork()/exec(), depending on how much control you need.


来源:https://stackoverflow.com/questions/24259994/how-to-call-perl-from-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!