问题
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:
- Embed a Perl interpreter into your C++ program: http://perldoc.perl.org/perlembed.html
- 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