joemaniaci has asked for the wisdom of the Perl Monks concerning the following question:

So I am attempting to call a perl script from C#/asp.net web form. However it isn't entirely calling it correctly and I have two examples

string fullPath = @"Z:\path\test.pl"; string arguments = arg1 + arg 2 Process myProcess = Process.Start(fullPath, arguments); myProcess.WaitForExit(999); if(!myProcess.HasExited) { myProcess.Kill(); throw new Exception("Timed out while running process"); }

Now the perl file I am trying to run accesses files and creates folders named after them. I have already ran this from the command prompt(Win 7/Strawberry Perl) with the same arguments just to double check that all the permissions are there and running perl test.pl works just as I expect it to. However, when I call the script from within asp.net a terminal does open but nothing happens. It just opens and closes, so I experimented by adding a sleep(xxx) to my test.pl and it actually affects when the terminal closes. So that tells me that asp.net is indeed opening a process and executing this perl script, but only the sleep command. However, when I changed the sleep from sleep(3) to sleep(10) I was actually able to see one of the print statements in the perl script print to the terminal for about a tenth of a second before the terminal closed. So I am not sure what is going on.

ProcessStartInfo perlStart = new ProcessStartInfo(@C:\Strawberry\perl\ +bin\perl.exe"); perlStart.Arguments = "Z:\path\test.pl" + argument1 + argument2; perlStart.UseShellExecute = false; perlStart.CreateNoWindow = true; Process perl = new Process(); perl.StartInfo = perlStart; perl.Start(); perl.WaitForExit();

Now this code is suppose to do the same thing, but the sleep commands have no effect on it whatsoever. The terminal barely opens before it closes. EDIT: Actually goofing around with it, it looks like it is is printing the good old file not found error with this one, it's just so fast. Looking for an asp.net process sleep function as we speak. So again, the perl file on it's own in the command prompt does everything flawlessly. sleep(xxx) commands with asp.net are actually being executed and under the right circumstances print statements are also being executed. However, under asp.net, new text files and folders are not being created. The purpose of this program is that I am converting my perl parser that handles text files as large as 30 GB into asp.net so that my tool has a web front end. However, asp.net can only handle files ~2 GB. I looked into creating a file splitter but from what I could tell, I would have used the same classes/methods used to open up the files and therefore would have run into the same 2 GB limit.

asp.net Process Class: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx asp.net ProcessStartInfo Class: http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.aspx

I should mention that having...

sleep(10) print "something";

...or...

sleep(5); print "something"; sleep(5);

...or...

print "something"; sleep(10);

...has no effect on the one print statement I can barely see for a split second.

Replies are listed 'Best First'.
Re: Calling perl in C#
by davido (Cardinal) on Apr 18, 2012 at 20:10 UTC

    Are you Suffering from Buffering?

    If there's no "\n" at the end of what you're printing, and you haven't messed with $|, there's a good chance that the issue with your screen output is simply that the output buffer isn't being flushed to the screen until the Perl script exits.

    That could explain wonky output.

    As for explaining away the fact that files and directories aren't getting created, that probably has more to do with the permissions under which your Perl script is being executed, while running as an external command within the .net framework. If your Perl script is executing commands that create directories and files, surely the return values of those commands would indicate failure if you check them, and if they do indicate failure, $! is going to give you some clues as to what's wrong. Make sure that all your system commands (open, close, etc.) are being checked for success, and upon failure, make sure to dump the results to a logfile. Or if you're unable to get a logfile to be written, dump errors to STDERR inside of a routine that awaits a "hit enter to continue" before proceeding, so you can see what's happening.

    We'll have a hard time telling you for sure what's wrong without seeing the offending code, and without seeing the error messages it's generating. And if you're not testing for failure, you won't see the system call error messages either.


    Dave

      So I was goofing around with my second example and I noticed there was some text but the terminal was closing so fast I couldn't tell. So I spent five minutes clicking my asp.net button to execute the perl script and then trying to capture a screenshot of the terminal being open and I finally got it.

      This

      perlStart.Arguments = "Z:\location\test.pl" + argument1 + argument2;

      Needs to be changed to this.

      perlStart.Arguments = "Z:\location\test.pl" + " " + argument1 + " " + argument2;

      It was looking for a perl file called Z:\location\test.plwhateverthefirstargumentwaswhateverthesecondargumentwas

      It now created the folders and works flawlessly.