0

I was following one of the thread to run perl scripts from my c# program.

My c# code is like this:

   private void RunScript(ArrayList selectedScriptFileList)
    {
        foreach (var curScriptFileName in selectedScriptFileList)
        {
            ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("perl.exe");
            myProcessStartInfo.Arguments = (string)(curScriptFileName);
            myProcessStartInfo.UseShellExecute = false;
            myProcessStartInfo.RedirectStandardOutput = true;
            myProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            myProcessStartInfo.CreateNoWindow = true;
            myProcess.StartInfo = myProcessStartInfo;


            myProcess.Start();
            myProcess.WaitForExit();
            string output = myProcess.StandardOutput.ReadToEnd();
            this.ScriptTestResultTextBox.AppendText(output);
        }            
    }

And my perl script requires XML parsing. I can read the print statement before the XML parsing, but not after the parsing starts. The script runs find on DoS shell.

Here is part of my script:

print("\n");
print("****************** test1.pl ***********************\n");
print("\n");

print("1");
print("2");

my $scriptName = 'test1.pl';
my $file = '../../ScriptParamLib.xml';
my $parser = XML::LibXML->new();
my $tree = $parser->parse_file($file);
my $root = $tree->getDocumentElement;
my @species = $root->getElementsByTagName('test_node');

print("Accessing XML Data Base...\n");

The c# testbox only shows the first three print statement but not the last one. Does anybody knows why?

Thanks

8
  • Check standard error; the script might be encountering a problem and stopping in the middle. Commented Dec 2, 2009 at 3:15
  • The script runs fine on Dos using perl x.pl command. I added print statement and looks like if I call the script from c#, the print statement after : my $tree = $parser->parse_file($file); did not get printed. I am trying to find out why. Commented Dec 2, 2009 at 6:50
  • added using warnings; no warnings generated. Commented Dec 2, 2009 at 6:52
  • Have you tried using an absolute path for $file in the Perl script? If that works, then the issue probably has something to do with the working directory of the process that gets spawned from your C# program. Commented Dec 2, 2009 at 7:35
  • 1
    You can use the Cwd module in your Perl script to see what the working directory actually is. If it's not what you expect, try setting it via the WorkingDirectory property of ProcessStartInfo in your C# program. Relative paths should work just fine after that. Commented Dec 2, 2009 at 15:22

2 Answers 2

4

You could add more debugging print statements (e.g. one between every other line of your code) to see how far the execution gets. However, I'm going to go on a hunch and suggest that adding these three lines to your script will either solve the problem outright or lead you closer to a solution:

use strict;
use warnings;
use XML::LibXML;

Please update your question indicating how far execution gets and what errors you see!

Sign up to request clarification or add additional context in comments.

Comments

0

I figured I should roll my comments into an answer since they proved to be helpful:

Since using an absolute path for $file in the Perl script works, the issue most likely has something to do with the working directory of the process that gets spawned from the C# program. You can use the Cwd module in the Perl script to see what the working directory actually is. If it's not what you expect, try setting it via the WorkingDirectory property of ProcessStartInfo in your C# program. Relative paths should work just fine after that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.