我有一個perl程序來處理特定格式的大型文本文件。我已經準備了一個從該Perl作爲Windows控制檯應用程序的exe文件。但對於學術用途,應用程序需要用更好的GUI(C++)編寫。
我不可能再次用C++重寫整個代碼(由於時間限制)。
有沒有什麼辦法從C++ GUI取文件,使用perl App(.pl或.exe)進行處理,並再次使用C++ Window來顯示輸出。
歡迎任何其他更好的選擇。如何從C++調用perl?
1
A
回答
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線路使用。
+0
'Prima'的好例子。你說得對,非常容易。 +1 – Miller
1
你有兩個選擇:
- 嵌入一個Perl解釋到你的C++程序:http://perldoc.perl.org/perlembed.html
- 讓你的C++程序調用Perl作爲一個子進程。根據您需要多少控制,使用
system()
或popen()
orfork()/exec()
。
相關問題
- 1. 如何從Java調用Perl?
- 2. C程序如何調用Perl函數?
- 3. 從Objective-C可可調用Perl庫
- 4. 如何從flex調用perl腳本?
- 5. 如何從Perl調用python腳本?
- 6. 如何從Perl調用Java程序?
- 7. 如何從Flex調用Perl子例程?
- 8. 如何從PIG調用perl腳本..?
- 9. 如何從LoadRunner調用Perl代碼?
- 10. 如何從Perl調用PHP腳本?
- 11. 如何從Perl腳本調用shell
- 12. 如何從Perl CGI腳本調用curl?
- 13. 我如何從Perl進行HTTP調用?
- 14. 如何從Perl程序調用C函數?
- 15. 如何從Perl中使用C++類?
- 16. 從java調用perl
- 17. 如何從另一個PL/Perl函數調用PL/Perl函數?
- 18. 如何從另一個perl cgi腳本調用perl cgi腳本
- 19. 如何從Java調用C++?
- 20. 如何調用從C#
- 21. 如何從C#調用MSBuild#
- 22. 如何QML調用從C++
- 23. 如何從C#調用OleCreatePropertyFrameIndirect#
- 24. 如何調用從C++
- 25. 如何從C調用C++函數?
- 26. 如何從C調用C++函數#
- 27. 如何從C調用C++函數?
- 28. 如何從C#調用C .exe文件?
- 29. 如何調試從PHP文件中調用的Perl
- 30. 從vbscript調用perl腳本
您是否考慮過使用[Tk](https://metacpan.org/release/Tk),[Wx](https://metacpan.org/release/Wx)或[Prima](https: //metacpan.org/release/Prima)在Perl中構建GUI,而不是使用C++創建GUI? – tobyink
@tobyink即使這些也需要重寫整個代碼。而且時間太長,以至於不能這樣做。我將在未來考慮他們的另一個冒險,而不是現在。 – BioDeveloper