我有一个用于处理特定格式的大型taxt文件的perl程序。我已经从该perl准备了一个exe文件,该文件可用作Windows控制台应用程序。但是对于学术用途,App需要用Better GUI(C ++)编写
我不可能在C ++中重写整个代码(由于时间限制)
有没有办法从C ++ GUI获取文件,使用perl App(.pl或.exe)进行处理,再次使用C ++ Window显示输出。
欢迎任何其他更好的选择。
答案 0 :(得分:2)
以下是使用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,这将非常容易。 / p>
答案 1 :(得分:1)
您有两种选择:
system()
或popen()
or fork()/exec()
,具体取决于您需要多少控制权。