嵌入Perl解释器 [英] Embedding Perl Interpreter
问题描述
刚刚下载的ActivePerl。我想嵌入一个C#应用程序Perl解释器(或者至少调用从C#Perl解释器)。我需要能够发送数据从C#发送给Perl,那么接收输出回C#。
just downloaded ActivePerl. I want to embed the perl interpreter in a C# application (or at least call the perl interpreter from C#). I need to be able to send send out data to Perl from C#, then receive the output back into C#.
我刚安装的ActivePerl,并增加了MS的脚本控制1.0作为参考。我发现在互联网上的代码,但我有麻烦它的工作。
I just installed ActivePerl, and added MS Script Control 1.0 as a reference. I found this code on the internet, but am having trouble getting it to work.
MSScriptControl.ScriptControlClass Interpreter = new MSScriptControl.ScriptControlClass();
Interpreter.Language = @"ActivePerl";
string Program = @"reverse 'abcde'";
string Results = (string)Interpreter.Eval(Program);
return Results;
最初,它有PerlScript而不是的ActivePerl,但也为我工作。我不完全知道什么Interpreter.Language的期望。是否需要路径解释?
Originally, it had 'PerlScript' instead of 'ActivePerl', but neither work for me. I'm not entirely sure what Interpreter.Language expects. Does it require the path to the interpreter?
解决......我不知道怎么样,但是当我改变了它回到PerlScript现在的工作。不过,我想知道,如果MSScript控制是使用的ActivePerl或另一种解释。
Solved... I'm not sure how, but when I changed it back to PerlScript it works now. Still, I would like to know if MSScript Control is using ActivePerl or another interpreter.
推荐答案
我不知道有关脚本控制但我已经做了,我必须'嵌入'spamassasin(这是一个Perl程序),类似的事情。我基本上使用的过程来完成这项工作。
I am not sure about the script control but I have done a similar thing where I had to 'embed' spamassasin (which is a Perl program). I basically used the Process to do the job. Something along the lines of:
var proc = new Process
{
StartInfo =
{
FileName = "perl",
WorkingDirectory = HttpRuntime.AppDomainAppPath,
Arguments = " myscript.pl arg1 arg2",
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
UseShellExecute = false
}
};
proc.Start();
proc.StandardInput.BaseStream.Write... // feed STDIN
proc.StandardOutput.Read... // Read program output
var procStdErr = proc.StandardError.ReadToEnd(); // errors
proc.StandardError.Close();
proc.StandardOutput.Close();
proc.WaitForExit(3000);
int exitCode = proc.ExitCode;
proc.Close();
这显然不只是Perl的具体,它有进程创建开销,因此,如果您运行的脚本往往可能是你需要考虑不同的解决方案的。
This obviously not just Perl specific and it has the process creation overhead, so if you are running your script too often probably you need to think of a different solution.
这篇关于嵌入Perl解释器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!