Re: Running a system command
I discovered that Perl offers a system command which is quite simple to use - e.g.
Code:
@my_command = ( "the_exe_name", "arg1", "arg2", "etc" );
system(@my_command);
In fact, if the exe returns an error status you can even test it like so:-
Code:
system(@my_command);
if ($? != 0) {
print "An error occurred while executing \"the_exe_name\"\n";
}
Re: Running a system command
Bumping this question as a new problem has come to light....
The perl system command seems to work - AND - if the launched program outputs any text, I can see the text in my command prompt (assuming I originally ran my perl script from a command prompt) - BUT - if I attempt to re-direct that output to a file - e.g.
Quote:
fixup.pl > output.txt
it seems to work in Windows 7 - but not in Windows 8 or XP. It doesn't work - even if I attempt to do the redirection from within my perl script - e.g.
Code:
@my_command = ( "the_exe_name", "arg1", "arg2", "etc", ">", "output.txt" );
system(@my_command);
Can anyone suggest a workaround to make redirection work as expected? I'm by no means a perl expert so I'm probably just doing something wrong :cry:
Re: Running a system command
redirection in that way isn't part of the normal application parameters (in fact the application doesn't even know about the redirection per se).
I'm not sure why this would have worked in W7 and not in W8/XP, maybe you had some special execution processor installed that took care of this.
Typically speaking, if you want redirection, you either have to do it "the hard way", by creating the file objects handles, then making the execute process adopt those handles as standard input and standard output.
ALternatively, you can take an easy way out and simply run everyting through the command processor. So the command would then end up being something like:
%comspec% /C the_exe_name arg1 arg2 arg3 > output.txt
you may need to get the "%comspec%" variable from the environment yourself and substitute it directly.
Re: Running a system command
Quote:
Originally Posted by
OReubens
ALternatively, you can take an easy way out and simply run everyting through the command processor.
Hi OReubens. I'm not very familiar with Perl but I assumed that's what its system command did?
I found this new set of perl statements. These ones work in Windows 7 and Windows XP - but not for Windows 8 :cry:
Code:
use IPC::Run3;
my $output;
run3(["the_exe_name", "arg1", "arg2", "etc"], \undef, "output.txt");
It just seems to be impossible to find one set of code that'll work everywhere! Do you happen to know if it's possible to get system information using a Perl script? In particular, is there a way to get the value of WINVER?
Re: Running a system command
As the rest of the project seems to be in c++, why not rewrite the perl script in c++?
Re: Running a system command
Quote:
Originally Posted by
2kaud
As the rest of the project seems to be in c++, why not rewrite the perl script in c++?
Yeah, I must admit I'm seriously contemplating that. If I rewrite it in C++ it'll most likely work first time. And even if it doesn't, at least I know I'll be able to fix it !
I don't understand why programmers keep struggling with all this scripting stuff. It almost never works reliably in my experience - and even if you can make it work on one machine it rarely works the same on somebody else's! :(